#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int MX = 5005;
const long long MOD = 1000000007;
int lcp[MX][MX];
int dp[MX];
void solve() {
  int n;
  string s;
  cin >> n;
  for (int i = 1; i <= n + 1; i++) {
    for (int j = 1; j <= n + 1; j++) lcp[i][j] = 0;
  }
  cin >> s;
  s = "$" + s;
  for (int i = 1; i <= n; i++) lcp[i][i] = n - i + 1;
  for (int i = n; i >= 1; i--) {
    for (int j = n; j > i; j--) {
      lcp[i][j] = (s[i] == s[j]) ? 1 + lcp[i + 1][j + 1] : 0;
    }
  }
  int sol = 0;
  for (int i = n; i >= 1; i--) {
    dp[i] = n - i + 1;
    for (int j = i + 1; j <= n; j++) {
      int x = lcp[i][j];
      if (j + x <= n && s[i + x] < s[j + x]) {
        dp[i] = max(dp[i], dp[j] + n - i + 1 - x);
      }
    }
    sol = max(sol, dp[i]);
  }
  cout << sol << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
