#include <bits/stdc++.h>
using namespace std;
using ivector = vector<int>;
using imatrix = vector<ivector>;
const auto first = 'a', last = 'z';
const int M = last-first, U = M+1;
auto nextInt(istream& cin) { int num; cin >> num; return num; }
auto nextStr(istream& cin) { string str; cin >> str; return str; }
auto randomStr(mt19937& random, int n) {
uniform_int_distribution<int> uniform_letter(0,M);
string s;
while (n--)
s += char(first+uniform_letter(random));
return s; }
struct problem {
const int n;
const string s;
problem(istream& stream): n(nextInt(stream)), s(nextStr(stream)) {}
problem(mt19937& random, int size) : n(size), s(randomStr(random,n)) {}
ostream& write(ostream& stream) const {
return stream << n << endl << s << endl; }
friend ostream& operator<<(ostream& stream, const problem& prob) {
return prob.write(stream); } };
struct solution: problem {
int m, min_length = numeric_limits<int>::max();
imatrix prefix;
auto is_palindrome_without(int l, int r) {
int odd = 0;
for (int i = 0; i < U; ++i)
odd += (prefix[l][i]+(prefix[n][i]-prefix[r][i]))&1;
return odd < 2; }
solution(const problem& prob) :
problem(prob), m(n+1), prefix(m,ivector(U)) {
for (int l = 0, r = 1; l < n; l = r++)
prefix[r] = prefix[l], ++prefix[r][s[l]-first];
if (is_palindrome_without(0,0))
min_length = 0; } };
struct brute_force: solution {
brute_force(const problem& prob): solution(prob) {
if (min_length > 0)
for (min_length = 1; min_length < n; ++min_length)
for (int r = min_length; r <= n; ++r)
if (is_palindrome_without(r-min_length,r))
return; } };
struct map_based: solution {
ivector g;
unordered_map<int,ivector> h;
auto update(int l, const ivector &f) {
const auto b = f.begin(), e = f.end(), c = upper_bound(b,e,l);
if (c != e)
min_length = min(min_length,*c-l); }
auto check(int l, int key) {
const auto it = h.find(key);
if (it != h.end())
update(l,it->second); }
map_based(const problem& prob): solution(prob), g(m) {
if (min_length == 0)
return;
for (int i = 0; i < m; ++i)
for (int b = 1, j = 0; j < U; ++j, b <<= 1)
if (prefix[i][j]&1)
g[i] |= b;
for (int x = g[n], r = 1; r <= n; ++r)
h[g[r]^x].push_back(r);
for (int l = 0; l < n; ++l) {
const int x = g[l];
check(l,x);
for (int b = 1, j = 0; j < U; ++j, b <<= 1)
check(l,x^b); } } };
auto test_main() {
random_device device;
mt19937 random(device());
int T, size;
cin >> T >> size;
for (int test = 1; test <= T; ++test) {
const problem prob(random,size);
const auto b = brute_force(prob).min_length;
const auto m = map_based(prob).min_length;
if (m != b) {
cout << "In test " << test << endl;
cout << prob;
cout << "computed = " << m << endl;
cout << "expected = " << b << endl;
return 0; } }
cout << "passed";
return 0; }
auto submission_main() {
cin.tie(nullptr)->sync_with_stdio(false);
for (int t = nextInt(cin); t--; )
cout << map_based(problem(cin)).min_length << endl;
return 0; }
int main() {
return test_main(); }