#ifndef LOCAL
// #pragma GCC optimize("Ofast,unroll-loops")
#endif
#include <climits>
#include <unordered_map>
#include <random>
#include <chrono>
#include <numeric>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <functional>
#include <bitset>
#include <string>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <list>
#include <forward_list>
#include <set>
#include <unordered_set>
#include <cstdint>
#include <ext/pb_ds/assoc_container.hpp>
#ifndef LOCAL
// #pragma GCC target("avx,avx2,fma")
#endif
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(x) begin(x), end(x)
#define isz(x) ((int)size(x))
#define X first
#define Y second
bool is_pal(const string& s, int l, int r) {
vector<int> cnt(26);
while (l <= r) {
cnt[s[l++] - 'a'] ^= 1;
}
return count(all(cnt), 1) < 2;
}
int check(const string& s, int l, int r) {
int ans = 0;
for (int i = l; i <= r; ++i) {
for (int j = l; j <= i; ++j) {
if (!is_pal(s, j, i))
ans = max(ans, i - j + 1);
}
}
return ans;
}
void solve() {
int n;
cin >> n;
string s;
cin >> s;
int q;
cin >> q;
while (q --> 0) {
int l, r;
cin >> l >> r;
cout << check(s, l - 1, r - 1) << endl;
}
}
signed main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// freopen("in.txt", "w", stdout); /* для стресс-ввода */
#endif
// cin.tie(0)->sync_with_stdio(0);
int t = 1;
cin >> t;
while (t --> 0) {
solve();
}
}
I2lmbmRlZiBMT0NBTAogIC8vICNwcmFnbWEgR0NDIG9wdGltaXplKCJPZmFzdCx1bnJvbGwtbG9vcHMiKQojZW5kaWYKI2luY2x1ZGUgPGNsaW1pdHM+CiNpbmNsdWRlIDx1bm9yZGVyZWRfbWFwPgojaW5jbHVkZSA8cmFuZG9tPgojaW5jbHVkZSA8Y2hyb25vPgojaW5jbHVkZSA8bnVtZXJpYz4KI2luY2x1Z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YWFiYmFhYmJhYWJiYWFiYmFhYmJhYWJiYWFiYmFhYmJhYWJiYWFiYmFhYmJhYWJiCjEgMSAxMjA=
1
120
aabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabb
1 1 120