#include using namespace std; //* ATCODER #include using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template T max(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template T min(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template T sum(vector &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } // isprime bool isprime(ll n){ if (n == 1) return false; for (ll i = 2; i * i <= n; i++){ if (n % i == 0) return false; } return true; } // importrandom // don't forget randomjumon // and also isprime ll RandomMod(ll l, ll r){ ll ret = l + rand() % (r - l); while (!isprime(ret)) ret = l + rand() % (r - l); return ret; } ll randrange(ll l, ll r){ return l + rand() % (r - l); } // ----- int main(){ srand((unsigned)time(NULL)); // Rolling Hash // don't forget randomjumon and importrandom typedef dynamic_modint<0> mint1; typedef dynamic_modint<1> mint2; int mod1 = RandomMod(7e8, 1e9); int mod2; do{ mod2 = RandomMod(7e8, 1e9); }while(mod1 == mod2); mint1::set_mod(mod1); mint2::set_mod(mod2); mint1 b1 = randrange(100, 200); mint2 b2 = randrange(100, 200); int MAX_m = 800000; vector b1l(MAX_m+1); b1l[0] = 1; for (int i=0; i b2l(MAX_m+1); b2l[0] = 1; for (int i=0; i> CASES; while(CASES--){ int n; cin >> n; string s; cin >> s; int ans = 0; vector t1(n+1); vector t2(n+1); rep(i,0,n){ t1[i+1] = t1[i] * b1 + s[i] - 'a' + 1; t2[i+1] = t2[i] * b2 + s[i] - 'a' + 1; } vector sa = suffix_array(s); vector q(n); rep(i,0,n){ q[sa[i]] = i; } rep(i,1,n){ int v = min(i, n-i); mint1 x1 = t1[v] - t1[0] * b1l[v]; mint1 y1 = t1[i+v] - t1[i] * b1l[v]; mint2 x2 = t2[v] - t2[0] * b2l[v]; mint2 y2 = t2[i+v] - t2[i] * b2l[v]; if (x2 == y2 && y1 == x1){ if (i < n-i){ ans++; } }else{ if (q[0] < q[i]){ ans++; } } } cout << ans << '\n'; } }