#include <bits/stdc++.h>
using namespace std;
int t, n, i, j, c[5005][5005], l[5005], a;
char s[5005];
int main() {
  cin >> t;
  while (t--) {
    cin >> n >> s;
    for (i = n; ~i; --i)
      for (j = n; ~j; --j) c[i][j] = 0;
    for (i = n - 1; ~i; --i) {
      l[i] = 0;
      for (j = n - 1; ~j; --j)
        if (s[i] == s[j]) c[i][j] = c[i + 1][j + 1] + 1;
    }
    for (a = i = 0; i < n; ++i) {
      for (j = 0; j < n; ++j)
        if (l[j] - c[i][j] > l[i] && s[j + c[i][j]] < s[i + c[i][j]])
          l[i] = l[j] - c[i][j];
      if ((l[i] += n - i) > a) a = l[i];
    }
    cout << a << endl;
  }
}
