#include<stdio.h>

int main()

{

int n,k,T,i,x,x1,p,y;

long long int arr1[2000],arr2[2000],min,t;

scanf("%d",&T);

for(x=0;x<T;x++)

{

i=0;

scanf("%d%d",&n,&k);

for(x1=0;x1<n;x1++)

scanf("%lld",&arr1[x1]);

```
//sort
for(x1=0;x1<n;x1++)
{
p=x1;
for(y=x1+1;y<n;y++)
{
if(arr1[y]<arr1[p])
p=y;
}
t=arr1[x1];
arr1[x1]=arr1[p];
arr1[p]=t;
}
t=0;
while((i+k-1)<n){
arr2[i]=arr1[i+k-1]-arr1[i];
t++;
i++;
}
min=arr2[0];
for(x1=0;x1<t;x1++)
{
if(arr2[x1]<min)
min=arr2[x1];
}
printf("%lld\n",min);
}
return 0;
}
```