#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "YES" << en
#define no cout << "NO" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) __builtin_popcountll(a)
#define bithigh(a) 63-__builtin_clzll(a)
#define lg bithigh
ll highpow(ll a) { return 1LL << (ll)lg(a); }

using namespace std;

const ll LINF = 4e18;
const int mxN = 2e5+10, INF = 2e9, mod = (1 ? 1e9+7 : 998244353);
ll n, m, a[mxN];

int f(char c){ return (int)(c - 'a'); }

int Calc(string s){

    n = s.size();
    int cnt[26], tot[26];
    memset(cnt, 0, sizeof(cnt));
    memset(tot, 0, sizeof(tot));
    for (int i = 0; i < n; i++)
        tot[f(s[i])]++;
    int i = 0;
    for (; i < n; i++){
        if (n&1 && i == n / 2){
            if (!(tot[f(s[i])]&1))
                break;
            else
                continue;
        }
        if (i < n / 2) cnt[f(s[i])]++;
        bool succ = 1;
        for (int j = 0; j < 26; j++)
            if (cnt[j] > tot[j] / 2)
                succ = 0;
        if (!succ) break;
        if (i > (n-1) / 2 && s[i] != s[n-i-1])
            break;
    }
    return n - i;
}

void Solve(){

    rs(T);
    int in = 0;
    for (; in < T.size(); in++)
        if (T[in] != T[T.size()-in-1])
            break;
    if (in == T.size()){
        cout << 1 << en;
        return;
    }
    string s = "";
    for (int i = in; i < T.size()-in; i++)
        s += T[i];
    int ans = Calc(s);
    reverse(all(s));
    smin(ans, Calc(s));
    if (!ans) ans = 1;
    cout << ans << en;
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0); cerr.tie(0);
    cout << setprecision(12) << fixed;
    cerr << setprecision(12) << fixed;
    cerr << "Started!" << endl;

    int t = 1;
    cin >> t;
    while (t--)
        Solve();

    return 0;
}