#include <bits/stdc++.h>
using namespace std;
int L[2005][35], R[2005][35];
int main() {
  int T;
  scanf("%d", &(T));
  while (T--) {
    int n;
    scanf("%d", &(n));
    int arr[n + 2];
    for (int i = 0; i <= n + 1; i++) {
      for (int j = 1; j <= 30; j++) {
        L[i][j] = 0;
        R[i][j] = 0;
      }
    }
    for (int i = 1; i <= n; i++) scanf("%d", &(arr[i]));
    for (int i = 1; i <= n; i++) {
      int x;
      x = arr[i];
      for (int j = 1; j <= 30; j++) {
        L[i][j] = L[i - 1][j];
      }
      L[i][x]++;
    }
    for (int i = n; i >= 1; i--) {
      int x;
      x = arr[i];
      for (int j = 1; j <= 30; j++) {
        R[i][j] = R[i + 1][j];
      }
      R[i][x]++;
    }
    int ans = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = i + 1; j <= n + 1; j++) {
        int mx1 = 0, mx2 = 0;
        for (int x = 1; x <= 30; x++) {
          mx1 = max(mx1, L[j - 1][x] - L[i][x]);
          mx2 = max(mx2, min(L[i][x], R[j][x]));
        }
        ans = max(ans, mx1 + mx2 + mx2);
      }
    }
    printf("%d\n", ans);
  }
}
