#if __INCLUDE_LEVEL__ == 0 #include using namespace std; #include __BASE_FILE__ namespace { void solve() { int n; cin >> n; string s; cin >> s; int ans = 0; RollingHash rh(s.begin(), s.end()); for (int i : rep(1, n)) { ans += strcmp(rh, 0, i, rh, i, n) < 0; } print(ans); } } // namespace int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { solve(); } } #else // __INCLUDE_LEVEL__ template bool chmin(T& x, U&& y) { return y < x && (x = forward(y), true); } template bool chmax(T& x, U&& y) { return x < y && (x = forward(y), true); } namespace std { template istream& operator>>(istream& is, pair& p) { return is >> p.first >> p.second; } template istream& operator>>(istream& is, tuple& t) { return apply([&is](auto&... xs) -> istream& { return (is >> ... >> xs); }, t); } template istream& operator>>(istream& is, tuple&& t) { return is >> t; } template >* = nullptr> auto operator>>(istream& is, R&& r) -> decltype(is >> *begin(r)) { for (auto&& e : r) { is >> e; } return is; } template ostream& operator<<(ostream& os, const pair& p) { return os << p.first << ' ' << p.second; } template ostream& operator<<(ostream& os, const tuple& t) { auto f = [&os](const auto&... xs) -> ostream& { [[maybe_unused]] auto sep = ""; ((os << exchange(sep, " ") << xs), ...); return os; }; return apply(f, t); } template >* = nullptr> auto operator<<(ostream& os, R&& r) -> decltype(os << *begin(r)) { auto sep = ""; for (auto&& e : r) { os << exchange(sep, " ") << e; } return os; } } // namespace std template void print(Ts&&... xs) { cout << tie(xs...) << '\n'; } inline auto rep(int l, int r) { return views::iota(min(l, r), r); } inline auto rep(int n) { return rep(0, n); } inline auto rep1(int l, int r) { return rep(l, r + 1); } inline auto rep1(int n) { return rep(1, n + 1); } inline auto per(int l, int r) { return rep(l, r) | views::reverse; } inline auto per(int n) { return per(0, n); } inline auto per1(int l, int r) { return per(l, r + 1); } inline auto per1(int n) { return per(1, n + 1); } template struct RollingHash { static inline std::mt19937 rng_{ uint32_t(std::chrono::steady_clock::now().time_since_epoch().count())}; static inline std::array base{std::vector{1, rng_() % P}, std::vector{1, rng_() % P}}; std::array, 2> h; RollingHash() { h[0] = h[1] = {0}; } template RollingHash(Iterator first, Iterator last) { int n = std::distance(first, last); for (int z : {0, 1}) base[z].reserve(n + 1), h[z].reserve(n + 1); for (h[0] = h[1] = {0}; first != last;) push_back(*first++); } template void push_back(const T& a) { for (int z : {0, 1}) { h[z].push_back((uint64_t(h[z].back()) * base[z][1] + a) % P); while (base[z].size() < h[z].size()) base[z].push_back(uint64_t(base[z].back()) * base[z][1] % P); } } void pop_back() { for (int z : {0, 1}) h[z].pop_back(); } int get(int l, int r, int z) const { return (h[z][r] + uint64_t(P - h[z][l]) * base[z][r - l]) % P; } std::array get(int l, int r) const { return {get(l, r, 0), get(l, r, 1)}; } friend int lcp(const RollingHash& ha, int la, int ra, const RollingHash& hb, int lb, int rb) { int ok = 0, ng = std::min(ra - la, rb - lb) + 1; while (ng - ok > 1) { int mid = (ok + ng) / 2; (ha.get(la, la + mid) == hb.get(lb, lb + mid) ? ok : ng) = mid; } return ok; } friend int strcmp(const RollingHash& ha, int la, int ra, const RollingHash& hb, int lb, int rb) { int m = lcp(ha, la, ra, hb, lb, rb); if (la + m == ra && lb + m == rb) return 0; if (la + m == ra) return -1; if (lb + m == rb) return +1; return ha.get(la + m, la + m + 1, 0) - hb.get(lb + m, lb + m + 1, 0); } }; #endif // __INCLUDE_LEVEL__