#include<bits/stdc++.h> usingnamespacestd; typedeflonglong ll; #ifndef ONLINE_JUDGE #define dbg(x...) do{cout << "\033[33;1m" << #x << "->" ; err(x);} while (0) voiderr(){cout << "\033[39;0m" << endl;} template<template<typename...> classT, typenamet, typename... A> voiderr(T<t> a, A... x){for (auto v: a) cout << v << ' '; err(x...);} template<typename T, typename... A> voiderr(T a, A... x){cout << a << ' '; err(x...);} #else #define dbg(...) #endif #define inf 1ll << 50
constint N = 5e3 + 5; char s[N];
intmain() { int T; scanf("%d", &T); while (T--) { int m; scanf("%d", &m); int ans = 0; scanf("%s", s); int len = strlen(s); for (int i = 0; i < len; i++) { int sum = 0; int head = i, tail = i - 1; while (tail <= len - 2 && 2 * i - tail - 1 > 0) { tail++; sum += abs(s[tail] - s[2 * i - tail - 1]); while (head <= tail && sum > m) { sum -= abs(s[head] - s[2 * i - head - 1]); head++; } ans = max(ans, tail - head + 1); } } for (int i = 1; i < len - 1; i++) { int sum = 0; int head = i + 1, tail = i; while (tail <= len - 2 && 2 * i - tail > 0) { tail++; sum += abs(s[tail] - s[2 * i - tail]); while (head <= tail && sum > m) { sum -= abs(s[head] - s[2 * i - head]); head++; } ans = max(ans, tail - head + 1); } } printf("%d\n", ans); } return0; }