#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops")
using namespace std;
const long long inf = (1ll << 60);
const long long iinf = 2147483647;
const long long mod = 1e9 + 7;
const long long maxn = 8000;
const double PI = acos(-1);
long long pw(long long x, long long p) {
  long long ret = 1;
  while (p > 0) {
    if (p & 1) {
      ret *= x;
      ret %= mod;
    }
    x *= x;
    x %= mod;
    p >>= 1;
  }
  return ret;
}
long long inv(long long a) { return pw(a, mod - 2); }
long long cp[5005][5005];
bool leq[5005][5005];
long long dp[5005];
signed main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    string second;
    cin >> second;
    for (long long i = (0); i < (n + 2); i++)
      for (long long j = (0); j < (n + 2); j++) cp[i][j] = 0, leq[i][j] = 1;
    for (long long i = n - 1; i >= 0; --i) {
      for (long long j = n - 1; j >= 0; --j) {
        if (second[i] == second[j]) {
          cp[i][j] = cp[i + 1][j + 1] + 1;
          leq[i][j] = leq[i + 1][j + 1];
        } else {
          cp[i][j] = 0;
          leq[i][j] = second[i] < second[j];
        }
      }
    };
    ;
    long long ret = 0;
    for (long long i = 1; i <= n; ++i) {
      dp[i] = 0;
      for (long long j = 0; j < i; ++j) {
        if (j == 0 || leq[j - 1][i - 1])
          dp[i] =
              max(dp[i], n - i + 1 + dp[j] - (j == 0 ? 0 : cp[i - 1][j - 1]));
      }
      ret = max(ret, dp[i]);
      ;
      ;
    }
    cout << ret << '\n';
  }
}
