#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e3 + 10;
const int mod = 1e9 + 7;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
int T, n, g[maxn][maxn], f[maxn], ans;
char s[maxn];
int main() {
  T = read();
  while (T--) {
    n = read();
    ans = 0;
    scanf("%s", s + 1);
    for (int i = n + 1; i >= 1; i--)
      for (int j = n + 1; j >= 1; j--)
        if (s[i] == s[j])
          g[i][j] = 1 + g[i + 1][j + 1];
        else
          g[i][j] = 0;
    for (int i = 1; i <= n; i++) {
      f[i] = n - i + 1;
      for (int j = 1; j < i; j++)
        if (s[i + g[i][j]] > s[j + g[i][j]])
          f[i] = max(f[i], f[j] + n - i - g[i][j] + 1);
      ans = max(ans, f[i]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
