#include <bits/stdc++.h>
char s[5010];
int lcp[5010][5010], f[5010];
int max(int a, int b) { return a > b ? a : b; }
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    for (int i = 0; i <= n; i++) {
      f[i] = 0;
      for (int j = 0; j <= n; j++) lcp[i][j] = 0;
    }
    scanf("%s", s + 1);
    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 >= 1; j--) {
        if (i == j) continue;
        if (s[i] == s[j]) {
          lcp[i][j] = lcp[i + 1][j + 1] + 1;
        }
      }
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      f[i] = n - i + 1;
      for (int j = 1; j < i; j++) {
        if (s[i + lcp[i][j]] > s[j + lcp[i][j]]) {
          f[i] = max(f[i], f[j] + n - i - lcp[i][j] + 1);
        }
      }
      ans = max(ans, f[i]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
