//412_4
#include <iostream>
#include <algorithm>
#include <stdio.h>

#define min(a, b) (((a)<(b))?(a):(b))
#define max(a, b) (((a)>(b))?(a):(b))

using namespace std;

const int N = 2e5+5;
const int T = N*2;

int t[T];
int n;

int sum(int i){
    int s=0;
    for(;i>=0; i=(i&(i+1))-1){
        s+=t[i];
    }
    return s;
}

int sum(int l, int r){
    return sum(r)-sum(l-1);
}

void upd(int i){
    for(; i<=n; i|=i+1) t[i]++;
}

int a[N];
pair<int,int> p[N];

int main()
{
    freopen("LADDER.IN","r", stdin);
    //freopen("input.txt", "r", stdin);



    int tt;
    scanf("%d",&tt);
    while(tt--){
        int K;
        scanf("%d%d",&n,&K);

        for(int i=1;i<=n;++i){
            scanf("%d",&a[i]);
            p[i-1] = make_pair(a[i], -i);
        }

        for(int i=0;i<=n;++i) t[i]=0;

        upd(0);

        sort(p,p+n);
        for(int k=0;k<n;++k){
            int i = -p[k].second;
            int l = max(0, i-K);
            int r = max(0, i-1);


            int s = sum(l,r);
            if(s) upd(i);
            //printf("!!!%d %d %d %d   %d\n",i,a[i],l,r,s);
        }

        int ans = 0;
        //for(int i=1;i<=n;++i) printf("%d ",sum(i,i)); printf("\n");
        for(ans=n; ans>=0; --ans) if(sum(ans,ans)==1) break;

        printf("%d\n",ans);

    }

    return 0;
}

