// CPP program to find largest subarray
// having sum greater than k.
#include <bits/stdc++.h>
using namespace std;
// Comparison function used to sort preSum vector.
bool compare(const pair<int, int>& a,
const pair<int, int>& b)
{
if (a.first == b.first)
return a.second < b.second;
return a.first < b.first;
}
// Function to find index in preSum vector upto which
// all prefix sum values are less than or equal to val.
int findInd(vector<pair<int, int> >& preSum, int n,
int val)
{
// Starting and ending index of search space.
int l = 0;
int h = n - 1;
int mid;
// To store required index value.
int ans = -1;
// If middle value is less than or equal to
// val then index can lie in mid+1..n
// else it lies in 0..mid-1.
while (l <= h) {
mid = (l + h) / 2;
if (preSum[mid].first <= val) {
ans = mid;
l = mid + 1;
}
else
h = mid - 1;
}
return ans;
}
// Function to find largest subarray having sum
// greater than or equal to k.
int largestSub(int arr[], int n, int k)
{
int i;
// Length of largest subarray.
int maxlen = 0;
// Vector to store pair of prefix sum
// and corresponding ending index value.
vector<pair<int, int> > preSum;
// To store current value of prefix sum.
int sum = 0;
// To store minimum index value in range
// 0..i of preSum vector.
int minInd[n];
// Insert values in preSum vector.
for (i = 0; i < n; i++) {
sum = sum + arr[i];
preSum.push_back({ sum, i });
}
sort(preSum.begin(), preSum.end(), compare);
// Update minInd array.
minInd[0] = preSum[0].second;
for (i = 1; i < n; i++) {
minInd[i] = min(minInd[i - 1], preSum[i].second);
}
sum = 0;
for (i = 0; i < n; i++) {
sum = sum + arr[i];
// If sum is greater than k, then answer
// is i+1.
if (sum > k)
maxlen = i + 1;
// If sum is less than or equal to k, then
// find if there is a prefix array having sum
// that needs to be added to current sum to
// make its value greater than k. If yes, then
// compare length of updated subarray with
// maximum length found so far.
else {
int ind = findInd(preSum, n, sum - k - 1);
if (ind != -1 && minInd[ind] < i)
maxlen = max(maxlen, i - minInd[ind]);
}
}
return maxlen;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int f=-1;
int n;cin>>n;
string s;
cin>>s;
char c='a';
int i=0;
while(i<26)
{
int arr[n];
int j=0;
while(j<n)
{
if(s[j]==c)
{
arr[j]=1;
}
else
{
arr[j]=-1;
}
// cout<<s[j]<<" ";
j++;}
//cout<<"\n"; j=1;
int k=-2;
/* int a[]={-1,1,-1,1,1,1,-1,-1,1,-1,1,-1,-1,-1};
*/
int g=largestSub(arr, n, k);
// cout<<g;
if(g>f)
{
f=g;
}
// cout<<"\n";
c++;i++;}
cout<<f<<"\n";
}
return 0;
}
Ly8gQ1BQIHByb2dyYW0gdG8gZmluZCBsYXJnZXN0IHN1YmFycmF5IAovLyBoYXZpbmcgc3VtIGdyZWF0ZXIgdGhhbiBrLiAKI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+IAp1c2luZyBuYW1lc3BhY2Ugc3RkOyAKICAKLy8gQ29tcGFyaXNvbiBmdW5jdGlvbiB1c2VkIHRvIHNvcnQgcHJlU3VtIHZlY3Rvci4gCmJvb2wgY29tcGFyZShjb25zdCBwYWlyPGludCwgaW50PiYgYSwgIAogICAgICAgICAgICAgY29uc3QgcGFpcjxpbnQsIGludD4mIGIpIAp7IAogICAgaWYgKGEuZmlyc3QgPT0gYi5maXJzdCkgCiAgICAgICAgcmV0dXJuIGEuc2Vjb25kIDwgYi5zZWNvbmQ7IAogIAogICAgcmV0dXJuIGEuZmlyc3QgPCBiLmZpcnN0OyAKfSAKICAKLy8gRnVuY3Rpb24gdG8gZmluZCBpbmRleCBpbiBwcmVTdW0gdmVjdG9yIHVwdG8gd2hpY2ggCi8vIGFsbCBwcmVmaXggc3VtIHZhbHVlcyBhcmUgbGVzcyB0aGFuIG9yIGVxdWFsIHRvIHZhbC4gCmludCBmaW5kSW5kKHZlY3RvcjxwYWlyPGludCwgaW50PiA+JiBwcmVTdW0sIGludCBuLCAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpbnQgdmFsKSAKeyAKICAKICAgIC8vIFN0YXJ0aW5nIGFuZCBlbmRpbmcgaW5kZXggb2Ygc2VhcmNoIHNwYWNlLiAKICAgIGludCBsID0gMDsgCiAgICBpbnQgaCA9IG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y8gbWFrZSBpdHMgdmFsdWUgZ3JlYXRlciB0aGFuIGsuIElmIHllcywgdGhlbiAKICAgICAgICAvLyBjb21wYXJlIGxlbmd0aCBvZiB1cGRhdGVkIHN1YmFycmF5IHdpdGggCiAgICAgICAgLy8gbWF4aW11bSBsZW5ndGggZm91bmQgc28gZmFyLiAKICAgICAgICBlbHNlIHsgCiAgICAgICAgICAgIGludCBpbmQgPSBmaW5kSW5kKHByZVN1bSwgbiwgc3VtIC0gayAtIDEpOyAKICAgICAgICAgICAgaWYgKGluZCAhPSAtMSAmJiBtaW5JbmRbaW5kXSA8IGkpIAogICAgICAgICAgICAgICAgbWF4bGVuID0gbWF4KG1heGxlbiwgaSAtIG1pbkluZFtpbmRdKTsgCiAgICAgICAgfSAKICAgIH0gCiAgCiAgICByZXR1cm4gbWF4bGVuOyAKfSAKICAKIAppbnQgbWFpbigpCnsKCWludCB0OwoJY2luPj50OwoJd2hpbGUodC0tKQoJeyAgIAoJICAgIGludCBmPS0xOwoJICAgIGludCBuO2Npbj4+bjsKCSAgICBzdHJpbmcgczsKCSAgICBjaW4+PnM7CgkgICAgIGNoYXIgYz0nYSc7CgkgICAgaW50IGk9MDsKCSAgICB3aGlsZShpPDI2KQoJICAgIHsKCSAgICAgIGludCBhcnJbbl07CgkgICAgICAgIAoJICAgICAgIGludCBqPTA7CgkgICAgICAgIHdoaWxlKGo8bikKCSAgICAgICAgewoJICAgICAgICAgICAgaWYoc1tqXT09YykKCSAgICAgICAgICAgIHsKCSAgICAgICAgICAgICAgICBhcnJbal09MTsKCSAgICAgICAgICAgIH0KCSAgICAgICAgICAgIGVsc2UKCSAgICAgICAgICAgIHsgICAgICAgICAgICAKCSAgICAgICAgICAgICAgICAKCSAgICAgICAgICAgICAgICBhcnJbal09LTE7CgkgICAgICAgICAgICAgICAgCgkgICAgICAgICAgICB9CgkgICAgICAgICAvLyAgY291dDw8c1tqXTw8IiAiOwoJICAgICAgICAgICAgaisrO30KLy9jb3V0PDwiXG4iOwkgICAgICAgIGo9MTsKCSAgICAgICAgCgkgICAgIAoJICAgICBpbnQgaz0tMjsKCSAgICAgCgkgICAgIAoJICAgIC8qIGludCBhW109ey0xLDEsLTEsMSwxLDEsLTEsLTEsMSwtMSwxLC0xLC0xLC0xfTsKCSAgICAgKi8KCSAgICAgCgkgICAgIAoJICAgICAKCSAgICAgICAgICBpbnQgZz1sYXJnZXN0U3ViKGFyciwgbiwgayk7IAoJICAgICAgLy8gIGNvdXQ8PGc7CgkgICAgICAgIGlmKGc+ZikKCSAgICAgICAgewoJICAgICAgICAgICAgZj1nOwoJICAgICAgICB9CgkgICAgICAvLyAgY291dDw8IlxuIjsKCSAgICAgICAgCgkgICAgICAgIAoJICAgICAgIGMrKztpKys7fQoJICAgIAoJICAgIAoJICAgIGNvdXQ8PGY8PCJcbiI7CgkgICAgCgkgICAgCgl9CnJldHVybiAwOwp9