#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll e=0;
int main() {
int t;
cin >> t;
while (t--) {
ll n,ind,ans;
cin >> n>>ind;vector<ll>a(n+1,0);
for(int i=1;i<n+1;i++){cin>>a[i];}
auto it = upper_bound(a.begin(), a.end(), a[ind]);
if (it != a.end()) {
ll x=distance(a.begin(),it);
if(x>ind){ans=x-2;}
else{
iter_swap(a.begin() + ind, a.begin() + x);
ll temp =distance(a.begin()+x,upper_bound(a.begin(), a.end(), a[x]));
if(x==1){temp--;}
ans =max(max(x-2,e),temp);
}
} else {
ans=n-1;goto output;
}
output:
cout<<ans<<endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nCmxsIGU9MDsKaW50IG1haW4oKSB7CiAgICBpbnQgdDsKICAgIGNpbiA+PiB0OwogICAgd2hpbGUgKHQtLSkgewogICAgICAgIGxsIG4saW5kLGFuczsKICAgICAgICBjaW4gPj4gbj4+aW5kO3ZlY3RvcjxsbD5hKG4rMSwwKTsKICAgICAgICBmb3IoaW50IGk9MTtpPG4rMTtpKyspe2Npbj4+YVtpXTt9CiAgICAgICAKCiAgICBhdXRvIGl0ID0gdXBwZXJfYm91bmQoYS5iZWdpbigpLCBhLmVuZCgpLCBhW2luZF0pOwoKICAgIGlmIChpdCAhPSBhLmVuZCgpKSB7CiAgICAgICAgbGwgeD1kaXN0YW5jZShhLmJlZ2luKCksaXQpOwogICAgICAgIGlmKHg+aW5kKXthbnM9eC0yO30KICAgICAgICBlbHNlewogICAgICAgICAgICBpdGVyX3N3YXAoYS5iZWdpbigpICsgaW5kLCBhLmJlZ2luKCkgKyB4KTsKICAgICAgICAgICBsbCB0ZW1wID1kaXN0YW5jZShhLmJlZ2luKCkreCx1cHBlcl9ib3VuZChhLmJlZ2luKCksIGEuZW5kKCksIGFbeF0pKTsKICAgICAgICAgICBpZih4PT0xKXt0ZW1wLS07fQogICAgICAgICAgIGFucyA9bWF4KG1heCh4LTIsZSksdGVtcCk7CiAgICAgICAgfQogICAgfSBlbHNlIHsKICAgICAgICBhbnM9bi0xO2dvdG8gb3V0cHV0OwogICAgfQoKICAgIG91dHB1dDoKICAgICAgICBjb3V0PDxhbnM8PGVuZGw7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0K