#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7, siz = 1e6 + 5;
long long t, n, m, k, a[siz], dp[5002][5002], dp2[5002];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    string s;
    cin >> s;
    s = "0" + s;
    for (long long i = 1; i <= n + 1; i++)
      for (long long j = i + 1; j <= n + 1; j++) dp[i][j] = 0;
    for (long long i = n; i >= 1; i--) {
      for (long long j = i - 1; j >= 1; j--) {
        if (s[i] == s[j]) {
          dp[j][i] = 1 + dp[j + 1][i + 1];
        }
      }
    }
    long long ans = 0;
    for (long long i = 1; i <= n; i++) {
      dp2[i] = n - i + 1;
      for (long long j = 1; j < i; j++) {
        if (s[i] > s[j])
          dp2[i] = max(dp2[j] + n - i + 1, dp2[i]);
        else if (s[i] == s[j]) {
          if (s[i + dp[j][i]] > s[j + dp[j][i]]) {
            dp2[i] = max(dp2[i], dp2[j] + n - (i + dp[j][i]) + 1);
          }
        }
      }
      ans = max(ans, dp2[i]);
    }
    cout << ans << endl;
  }
}
