#pragma region Macros #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define ll long long #define OVERLOAD(e1, e2, e3, e4, NAME, ...) NAME #define _rep1(i, n) for (long long i = 0; i < n; i++) #define _rep2(i, a, b) for (long long i = a; i < b; ++i) #define _rep3(i, a, b, t) \ for (long long i = a; i * (t / abs(t)) < b * (t / abs(t)); i += t) #define rep(...) OVERLOAD(__VA_ARGS__, _rep3, _rep2, _rep1, _)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define sz(x) (ll) x.size() #define fi first #define se second #define pb push_back #define eb emplace_back #define mp make_pair #define pcnt __builtin_popcountll #define SORT(v) sort(all(v)) #define UNIQUE(v) \ SORT(v); \ v.erase(unique(v.begin(), v.end()), v.end()); #define COPY(A, B) copy(all(A), B.begin()); #define REV(v) reverse(all(v)) #define MAX(x) *max_element(all(x)) #define MIN(x) *min_element(all(x)) #ifdef LOCAL #define dbg(x) \ cout << __LINE__ << " : " << #x << " = "; \ print(x); #else #define dbg(x) true #endif using namespace std; template using vc = vector; template using vvc = vector>; template using vvvc = vector>; template bool chmin(T &k, T m) { bool ret = k > m; k = min(k, m); return ret; } template bool chmax(T &k, T m) { bool ret = k < m; k = max(k, m); return ret; } template inline void print(const vector &v, string s = " ") { for (ll i = 0; i < (ll)v.size(); i++) cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n"); } template inline void print(const pair &p) { cout << p.first << " " << p.second << endl; } template inline void print(const T &x) { cout << x << "\n"; } template inline void print(const vector> &v) { for (auto &&p : v) print(p); } void yes(bool a) { cout << (a ? "yes" : "no") << endl; } void YES(bool a) { cout << (a ? "YES" : "NO") << endl; } void Yes(bool a) { cout << (a ? "Yes" : "No") << endl; } template T SUM(vc As) { T ret = 0; for (T a : As) ret += a; return ret; } #pragma endregion const ll INF = numeric_limits::max(); template struct ZAlgorithm { private: vector s; vector deleted; vector> delete_hist; vector z; queue cur; public: ZAlgorithm() : delete_hist{{}} {} template ZAlgorithm(const S &s) : ZAlgorithm() { for (auto &c : s) add(c); } void add(const T &c) { s.emplace_back(c); delete_hist.emplace_back(); deleted.emplace_back(0); z.emplace_back(0); z[0]++; int len = (int)s.size(); if (len == 1) return; if (s[0] == c) cur.emplace(len - 1); else deleted[len - 1] = 1; auto set = [&](int t, int len) { deleted[t] = 1; delete_hist[len].emplace_back(t); z[t] = len - t - 1; }; while (not cur.empty()) { int t = cur.front(); if (deleted[t]) { cur.pop(); } else if (s[len - t - 1] == s.back()) { break; } else { set(t, len); cur.pop(); } } if (not cur.empty()) { int t = cur.front(); for (auto &p : delete_hist[len - t]) { set(p + t, len); } } } int get(int k) const { return deleted[k] ? z[k] : (int)s.size() - k; } int operator[](int k) const { return get(k); } vector get() { vector ret(s.size()); for (int i = 0; i < (int)s.size(); i++) { ret[i] = get(i); } return ret; } }; void solve() { ll N; cin >> N; string S; cin >> S; ZAlgorithm zal(S); ll ans = 0; rep(i, 1, N) { ll k = zal.get(i); if (k > i) ans++; else if (i + k >= N) continue; else { if (i == k) continue; if (S[k] < S[i + k]) ans++; } } print(ans); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); ll T; cin >> T; rep(_, T) solve(); }