#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int tt, qq = 0;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    vector<int> A(n);
    int N = 1;
    for (int i = 0; i < n; i++) {
      cin >> A[i];
      A[i]--;
      N = max(N, A[i] + 1);
    }
    vector<vector<int>> B(n + 1, vector<int>(N));
    vector<int> C(N);
    for (int i = 0; i < n; i++) {
      C[A[i]]++;
      B[i + 1] = C;
    }
    vector<vector<int>> T(N);
    for (int i = 0; i < n; i++) {
      T[A[i]].emplace_back(i);
    }
    int ans = 0;
    (T);
    for (int i = 0; i < N; i++) {
      int sz = (int)T[i].size();
      ans = max(sz, ans);
      int m = sz / 2;
      int l = 0, r = n;
      for (int k = 1; k <= m; k++) {
        l = T[i][k - 1];
        r = T[i][sz - k];
        int mx = 0;
        for (int j = 0; j < N; j++) {
          if (i == j) continue;
          mx = max(mx, B[r][j] - B[l + 1][j]);
        }
        int res = k * 2 + mx;
        ans = max(ans, res);
        (i, res, k, mx);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
