import sys input = sys.stdin.readline def Z_algorithm(s): n = len(s) A = [n] + [0] * (n-1) i, j = 1, 0 while i < n: while i + j < n and s[j] == s[i+j]: j += 1 A[i] = j if j == 0: i += 1 continue k = 1 while i + k < n and k + A[k] < j: A[i+k] = A[k] k += 1 i += k j -= k return A T = int(input()) for _ in range(T): N = int(input()) S = list(input().rstrip()) S = [ord(s) - ord("a") for s in S] L = Z_algorithm(S) ans = 0 for i in range(1, N): L[i] = min(i, L[i]) if i == L[i] and 2 * i < N: ans += 1 else: if i + L[i] <= N - 1: if S[L[i]] < S[i + L[i]]: ans += 1 print(ans)