#include <bits/stdc++.h>

using namespace std;
const int N = 10100;
char s[N];
int a[N];

int main() {
    int T, m;
    scanf("%d", &T);
    while (T--) {
        scanf("%d%s", &m, s);
        int len = strlen(s);
        int ans = 0;
        for (int i = 0; i < len; i++) {
            int r = min(i, len - i - 1);
            for (int j = 1; j <= r; j++)
                a[j] = a[j - 1] + abs(s[i - j] - s[i + j]);
            for (int k = 0, j = 0, d = 0; k <= r; k++) {
                d = a[k] - a[j];
                while (d > m) d = a[k] - a[++j];
                ans = max(ans, k - j);
            }
        }
        for (int i = 1; i < len; i++) {
            int r = min(i, len - i);
            for (int j = 1; j <= r; j++)
                a[j] = a[j - 1] + abs(s[i - j] - s[i + j - 1]);
            for (int k = 0, j = 0, d = 0; k <= r; k++) {
                d = a[k] - a[j];
                while (d > m) d = a[k] - a[++j];
                ans = max(ans, k - j);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}