#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &x) {
  bool neg = false;
  char ch;
  for (ch = getchar(); !isdigit(ch); ch = getchar()) neg |= ch == '-';
  x = ch - '0';
  for (ch = getchar(); isdigit(ch); ch = getchar()) (x *= 10) += ch - '0';
  x *= neg ? -1 : 1;
}
template <class T>
void write(T x) {
  if (x < 0) {
    putchar('-');
    write(-x);
    return;
  }
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
const int N = 2e5 + 10;
int n, m, res;
int a[N];
void compress() {
  vector<int> rar;
  for (int i = 0; i < n; i++) rar.push_back(a[i]);
  sort(rar.begin(), rar.end());
  rar.resize(unique(rar.begin(), rar.end()) - rar.begin());
  for (int i = 0; i < n; i++)
    a[i] = lower_bound(rar.begin(), rar.end(), a[i]) - rar.begin();
}
int pos[N], prefix[N], suffix[N];
vector<pair<pair<int, int>, int> > range;
void find_range() {
  range.clear();
  for (int i = 0; i < n; i++) {
    pos[a[i]] = n - 1;
    suffix[a[i]] = prefix[a[i]] = 0;
  }
  for (int i = 0; i < n; i++) {
    suffix[a[i]]++;
    prefix[a[i]]++;
  }
  for (int i = 0; i < n; i++) {
    int id = a[i];
    while (a[pos[id]] != id) pos[id]--;
    if (pos[id] < i) {
      res = max(res, suffix[id] * 2);
      if (pos[id] + 1 < i)
        range.push_back(
            make_pair(make_pair(pos[id] + 1, i - 1), suffix[id] * 2));
    }
    suffix[id]--;
    prefix[id]--;
    pos[id]--;
  }
}
int res_range;
int freq[N], cnt[N];
void add(int x) {
  cnt[a[x]]++;
  freq[cnt[a[x]]]++;
  if (freq[res_range + 1] > 0) res_range++;
}
void sub(int x) {
  freq[cnt[a[x]]]--;
  cnt[a[x]]--;
  if (!freq[res_range]) res_range--;
}
void solve() {
  res = 1;
  res_range = 0;
  find_range();
  sort(range.begin(), range.end(),
       [](const pair<pair<int, int>, int> &itl,
          const pair<pair<int, int>, int> &itr) {
         return itl.first.second / m == itr.first.second / m
                    ? itl.first.first < itr.first.first
                    : itl.first.second < itr.first.second;
       });
  int prel = 0, prer = -1;
  fill(freq, freq + n, 0);
  fill(cnt, cnt + n, 0);
  for (int i = 0; i < range.size(); i++) {
    for (int j = range[i].first.first; j <= prel - 1; j++) add(j);
    for (int j = prel; j <= range[i].first.first - 1; j++) sub(j);
    for (int j = prer + 1; j <= range[i].first.second; j++) add(j);
    for (int j = range[i].first.second + 1; j <= prer; j++) sub(j);
    prel = range[i].first.first;
    prer = range[i].first.second;
    res = max(res, res_range + range[i].second);
  }
  write(res);
  putchar('\n');
}
int main() {
  int tc;
  read(tc);
  while (tc--) {
    read(n);
    m = sqrt(n);
    for (int i = 0; i < n; i++) read(a[i]);
    compress();
    solve();
  }
}
