#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 1000000007;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
string gh = "here";
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> v1(n + 2, 0);
    vector<vector<int> > seda(n + 2, vector<int>(27, 0)),
        teda(n + 2, vector<int>(27, 0));
    for (int i = 1; i <= n; i++) {
      cin >> v1[i];
    }
    for (int i = 1; i <= n; i++) {
      int aa = v1[i];
      seda[i] = seda[i - 1];
      seda[i][aa] = seda[i - 1][aa] + 1;
    }
    for (int i = n; i >= 1; i--) {
      int aa = v1[i];
      teda[i] = teda[i + 1];
      teda[i][aa] = teda[i + 1][aa] + 1;
    }
    int ans = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = n; j >= i && j >= 1; j--) {
        int ml = 0, mbech = 0, il = -1, ibech = -1;
        for (int k = 1; k < 27; k++) {
          if (min(seda[i][k], teda[j + 1][k]) > ml) {
            ml = min(seda[i][k], teda[j + 1][k]);
            il = k;
          }
        }
        for (int k = 1; k < 27; k++) {
          if (seda[j][k] - seda[i][k] > mbech) {
            mbech = seda[j][k] - seda[i][k];
            ibech = k;
          }
        }
        ans = max(ans, 2 * ml + mbech);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
