#include <bits/stdc++.h>
using namespace std;
void write(vector<int> &v) {
  for (auto i : v) cout << i << " ";
  cout << "\n";
}
void read(vector<int> &v) {
  for (auto &i : v) cin >> i;
}
const int INF = 1e9;
const int64_t INFF = 1e18;
const int N = 2e3 + 69;
void solve() {
  int n;
  cin >> n;
  vector<int> v(n);
  read(v);
  int ans = 0;
  vector<int> total_cnt(N, 0);
  for (int i = 0; i < n; i++) {
    total_cnt[v[i]]++;
    ans = max(ans, total_cnt[v[i]]);
  }
  vector<int> run_cnt(N, 0);
  for (int i = 0; i < n; i++) {
    int cnt = ++run_cnt[v[i]];
    int val = v[i];
    if (total_cnt[v[i]] - run_cnt[v[i]] < 0) continue;
    for (int j = n - 1; j >= i + 1; j--) {
      if (v[j] == val) {
        cnt--;
        if (cnt == 0) {
          vector<int> mid_cnt(N, 0);
          for (int k = i + 1; k < j; k++) {
            mid_cnt[v[k]]++;
            ans = max(ans, run_cnt[v[i]] * 2 + mid_cnt[v[k]]);
          }
        }
      }
      if (cnt == 0) break;
    }
  }
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
