#include #include using namespace std; using ll = long long; #define REP(i,n) for(int i=0;i void chmin(T& a, T b) { a = min(a, b); } template void chmax(T& a, T b) { a = max(a, b); } #define PR(x) cerr << #x << "=" << x << endl using i128 = __int128_t; int main() { int t; cin >> t; while(t--) { int n; string s; cin >> n >> s; vector sa = suffix_array(s); vector lcpa = lcp_array(s, sa); int ans = 0; REP(i, n) { if(sa[i] == 0) { ans = n - i - 1; int ni = i; int lcp = n; while(ni > 0) { ni--; chmin(lcp, lcpa[ni]); if(lcp >= sa[ni]) ans++; } break; } } cout << ans << endl; } return 0; }