#include <bits/stdc++.h>
#define ll long long
#define endll '\n';
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;
const int mod = 1e9 + 7, N = 1e6;
int ar[26][N + 3];
void solve()
{
int n, k;
cin >> n >> k;
string s;
cin >> s;
int starting = 0;
for (int i = 0; i <= 25; i++)
{
starting = i;
for (int j = 1; j <= n; j++)
{
// cout << char(starting + 'a') << " = " << ar[i][j] <<", ";
if (s[j - 1] - 'a' == starting)
{
ar[i][j] = 0;
}
else
ar[i][j] = 1;
starting++;
if (starting > 25)
starting = 0;
}
// cout << endll;
}
//
// prefix
for (int i = 0; i <= 25; i++)
{
for (int j = 1; j <= n; j++)
{
ar[i][j] += ar[i][j - 1];
}
// cout << endll;
}
for (int i = 0; i <= 25; i++)
{
for (int j = 1; j <= n; j++)
{
// cout << ar[i][j] << " ";
}
// cout << endll;
}
int ans = 0;
int left = 1, right = n;
for (int i = 0; i <= 25; i++)
{
for (int j = 1; j <= n; j++)
{
// cout << ar[i][j] << " ";
}
// cout << endll;
}
// cout << endll;
while (left <= right)
{
int flg = 0;
int mid = (left + right) / 2;
for (int i = 0; i <= 25; i++)
{
for (int j = mid; j <= n; j++)
{
int cst = ar[i][j] - ar[i][j - mid];
// cout << cst << " " << mid << " < " << j<< endll;
if (cst <= k)
{
flg = 1;
// left = mid + 1;
// cout << " found = " << mid << endll;
break;
}
}
if(flg) break;
}
if(flg) {
ans = max (ans, mid);
left = mid + 1;
}
else right = mid - 1;
}
cout << ans << endl;
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}