#include <bits/stdc++.h>
using namespace std;
template <class T>
using v2d = vector<vector<T> >;
template <class T>
bool uin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int maxN = 5e3 + 10;
int n, dp[maxN], lcp[maxN][maxN];
char s[maxN];
void solve() {
  cin >> n >> (s + 1);
  for (int i = 1; i <= (int)(n); ++i) {
    lcp[n + 1][i] = 0;
  }
  for (int i = n; i; i--) {
    for (int j = 1; j <= (int)(i - 1); ++j) {
      lcp[i][j] = 0;
      if (s[i] == s[j]) {
        lcp[i][j] = lcp[i + 1][j + 1] + 1;
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= (int)(n); ++i) {
    dp[i] = n - i + 1;
    for (int j = 1; j <= (int)(i - 1); ++j) {
      int c = lcp[i][j];
      if (i + c <= n) {
        if (s[i + c] > s[j + c]) {
          uax(dp[i], dp[j] + n - i - c + 1);
        }
      }
    }
    uax(ans, dp[i]);
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
