#include <bits/stdc++.h>
using namespace std;
const int inf = 2e18 + 100;
const int maxn = 1e6 + 100;
int a[maxn], b[maxn];
char s[maxn];
int f[5010][5010];
int dp[maxn];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    memset(dp, 0, sizeof(int) * (n + 10));
    cin >> (s + 1);
    for (int i = n; i >= 1; i--) {
      for (int j = n; j >= 1; j--) {
        f[i][j] = 0;
      }
    }
    for (int i = n; i >= 1; i--) {
      for (int j = n; j >= 1; j--) {
        if (s[i] == s[j]) {
          f[i][j] = f[i + 1][j + 1] + 1;
        } else
          f[i][j] = 0;
      }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      dp[i] = n - i + 1;
      for (int j = 1; j < i; j++) {
        if (s[i + f[i][j]] > s[j + f[i][j]]) {
          dp[i] = max(dp[i], dp[j] + n - i + 1 - f[i][j]);
        }
      }
      ans = max(ans, dp[i]);
    }
    cout << ans << "\n";
  }
}
