#include <bits/stdc++.h>
#define MAX 1000000008
#define FAST ios_base::sync_with_stdio(0)
using namespace std;
int main(){
FAST;
int t, n, k;
cin >> t;
while(t--){
cin >> n >> k;
vector<int> height(n+5);
for(int i = 0; i < n; i++)
cin >> height[i];
sort(height.begin(), height.end(), greater<int>());
int mini = MAX;
if ( k == 1 ) mini = 0;
else{
for( int i = 0; i <= n-k; i++){
mini = min(height[i]-height[i+k-1], mini);
}
}
cout << mini << '\n';
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgTUFYICAgICAxMDAwMDAwMDA4CiNkZWZpbmUgRkFTVCBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpewoJRkFTVDsKICAgIGludCB0LCBuLCBrOwogICAgY2luID4+IHQ7CiAgICB3aGlsZSh0LS0pewogICAgICAgIGNpbiA+PiBuID4+IGs7CgkgICAgdmVjdG9yPGludD4gaGVpZ2h0KG4rNSk7CiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKykgCiAgICAgICAgCWNpbiA+PiBoZWlnaHRbaV07CiAgICAgICAgCQoJICAJc29ydChoZWlnaHQuYmVnaW4oKSwgaGVpZ2h0LmVuZCgpLCBncmVhdGVyPGludD4oKSk7CgkgIAkKCSAgCWludCBtaW5pID0gTUFYOwoJICAJaWYgKCBrID09IDEgKSBtaW5pID0gMDsKCSAgCWVsc2V7CgkgIAkJZm9yKCBpbnQgaSA9IDA7IGkgPD0gbi1rOyBpKyspewoJICAJCQltaW5pID0gbWluKGhlaWdodFtpXS1oZWlnaHRbaStrLTFdLCBtaW5pKTsKCSAgCQl9CgkgIAl9CgkgIAljb3V0IDw8IG1pbmkgPDwgJ1xuJzsKCiAgICB9CiAgICAgICAgCiAgICByZXR1cm4gMDsKfQ==