#include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } vector z_algorithm(string s){ int n = s.size(); vector ans(n); ans[0] = n; int i = 1, j = 0; while(i < s.size()){ while(i+j < s.size() && s[i+j] == s[j]) j++; if(j == 0) { i++; continue; } ans[i] = j; int k = 1; while(k < j && ans[k]+k < j){ ans[i+k] = ans[k]; k++; } i += k; j -= k; } return ans; } void solve(){ int n; cin >> n; string s; cin >> s; auto z = z_algorithm(s); int ans = 0; for(int i = 1; i < n; i++){ int tail_len = n-i; int head_len = i; if(z[i] > head_len){ ans++; continue; } if(z[i] < tail_len){ if(s[z[i]] < s[i+z[i]]) ans++; } } cout << ans << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int t; cin >> t; while(t--) solve(); }