The only difference between easy and hard versions is constraints.

You are given a sequence a consisting of n positive integers.

Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are a and b, a can be equal b) and is as follows: [\underbrace{a, a, ..., a}_{x}, \underbrace{b, b, ..., b}_{y}, \underbrace{a, a, ..., a}_{x}]. There x, y are integers greater than or equal to 0. For example, sequences [], [2], [1, 1], [1, 2, 1], [1, 2, 2, 1] and [1, 1, 2, 1, 1] are three block palindromes but [1, 2, 3, 2, 1], [1, 2, 1, 2, 1] and [1, 2] are not.

Your task is to choose the maximum by length subsequence of a that is a three blocks palindrome.

You have to answer t independent test cases.

Recall that the sequence t is a a subsequence of the sequence s if t can be derived from s by removing zero or more elements without changing the order of the remaining elements. For example, if s=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [1, 1, 1, 1, 2].

Input

The first line of the input contains one integer t (1 ≤ t ≤ 2000) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1 ≤ n ≤ 2000) — the length of a. The second line of the test case contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 26), where a_i is the i-th element of a. Note that the maximum value of a_i can be up to 26.

It is guaranteed that the sum of n over all test cases does not exceed 2000 (∑ n ≤ 2000).

Output

For each test case, print the answer — the maximum possible length of some subsequence of a that is a three blocks palindrome.

Example

Input


6
8
1 1 2 2 3 2 1 1
3
1 3 3
4
1 10 10 1
1
26
2
2 1
3
1 1 1


Output


7
2
4
1
1
3
// package name;
import java.io.*;
import java.util.*;

public class Main {
	static final long mod = 1000000007;
	static final long lmx = 9223372036854775807l, lmn = -9223372036854775808l;
	static final int imx = 2147483647, imn = -2147483648;
	static final String Y = "YES", NO = "NO", NL = "\n", SPC = " ";
	static Scanner s = new Scanner(System.in);
	//static Reader s = new Reader();
	static PrintWriter out = new PrintWriter(System.out);
	static StringBuffer sb = new StringBuffer();
	public static void main(String[] args) throws IOException {
		int t = 1;
		t = s.nextInt();
		String xy = slinp();
		while (t > 0) {
			t--;
			int n = iinp();
			int a[] = iinp(n, 0, n);
			int dp[][] = new int[n][27];
			dp[0][a[0]]++;
			for(int i = 1; i < n; i++) {
				for(int j = 1; j < 27; j++) {
					if(a[i] == j) dp[i][j] = dp[i - 1][j] + 1;
					else dp[i][j] = dp[i - 1][j];
				}
			}
			long ans = 0;
			for(int i = 0; i < n; i++) {
				for(int j = i; j < n; j++) {
					long max = 0, smax = 0;
					for(int k = 1; k < 27; k++) {
						int left = 0;
						if(i > 0) left = dp[i - 1][k];
						max = max(max, dp[j][k] - left);
						smax = max(smax, min(left, dp[n - 1][k] - dp[j][k]));
					}
					//if(i == 2 && j == 5) System.out.println(max + SPC + smax);
					ans = max(ans, max + 2 * smax);
				}
			}
			sb.append(ans + NL);
		}
		out.print(sb);
		out.flush();
	}

	static int iinp() throws IOException {
		return s.nextInt();
	}

	static long linp() throws IOException {
		return s.nextLong();
	}

	static double dinp() throws IOException {
		return s.nextDouble();
	}

	static String sinp() throws IOException {
		return s.next();
	}

	static String slinp() throws IOException {
		return s.nextLine();
	}

	static int[] iarr(int n) {
		return new int[n];
	}

	static long[] larr(int n) {
		return new long[n];
	}

	static double[] darr(int n) {
		return new double[n];
	}

	static boolean[] barr(int n) {
		return new boolean[n];
	}

	static String[] sarr(int n) {
		return new String[n];
	}

	static int[][] iarr(int n, int m) {
		return new int[n][m];
	}

	static long[][] larr(int n, int m) {
		return new long[n][m];
	}

	static int[] iinp(int n, int st, int size) throws IOException {
		int a[] = iarr(n);
		for (int i = st; i < st + size; i++)
			a[i] = iinp();
		return a;
	}

	static long[] linp(int n, int st, int size) throws IOException {
		long a[] = larr(n);
		for (int i = st; i < st + size; i++)
			a[i] = linp();
		return a;
	}

	static String[] sinp(int n, int st, int size, String str) throws IOException {
		String a[] = sarr(n);
		for (int i = st; i < st + size; i++)
			if (str.equalsIgnoreCase(SPC))
				a[i] = sinp();
			else
				a[i] = slinp();
		return a;
	}

	static int[][] iinp(int n, int m, int row, int rows, int col, int cols) throws IOException {
		int a[][] = iarr(n, m);
		for (int i = row; i < row + rows; i++)
			for (int j = col; j < col + cols; j++)
				a[i][j] = iinp();
		return a;
	}

	static long[][] linp(int n, int m, int row, int rows, int col, int cols) throws IOException {
		long a[][] = larr(n, m);
		for (int i = row; i < row + rows; i++)
			for (int j = col; j < col + cols; j++)
				a[i][j] = linp();
		return a;
	}

	static void ginp(ArrayList<Integer> g[], int m) throws IOException {
		for (int i = 0; i < m; i++) {
			int u = iinp(), v = iinp();
			g[u].add(u);
			g[v].add(u);
		}
	}

	static long min(long... a) {
		long min = lmx;//Long.MAX_VALUE;
		for (long i : a)
			if (i < min)
				min = i;
		return min;
	}

	static long max(long... a) {
		long max = lmn;//Long.MIN_VALUE;
		for (long i : a)
			if (i > max)
				max = i;
		return max;
	}

	static long gcd(long a, long b) {
		if (b == 0)
			return a;
		return gcd(b, a % b);
	}

	static void sort(int a[]) {
		Arrays.sort(a);
	};

	static void sort(long a[]) {
		Arrays.sort(a);
	};

	static void sort(String a[]) {
		Arrays.sort(a);
	};

	static void sort(int a[], int st, int e) {
		Arrays.sort(a, st, e);
	};

	static void sort(long a[], int st, int e) {
		Arrays.sort(a, st, e);
	};

	static void sort(String a[], int st, int e) {
		Arrays.sort(a, st, e);
	};

	static void sortbycol(int arr[][]) {
		Arrays.sort(arr, new Comparator<int[]>() {
			public int compare(int[] x, int[] y) {
				if (x[1] == y[1])
					return x[2] - y[2];
				return x[1] - y[1]; // sort in increasing order on the basis of first column
			}
		});
	}

	static StringBuffer printarr(long a[], String str) {
		StringBuffer ans = new StringBuffer();
		for (long i : a)
			ans.append(i + str);
		return ans;
	}

}

class Pair implements Comparator<Pair> {
	long a, b;

	Pair(long x, long y) {
		a = x;
		b = y;
	}

	public int compare(Pair p1, Pair p2) {
		if (p1.a != p2.a)
			return (p1.a - p2.a > 0) ? 1 : -1;
		return (p1.b - p2.b > 0) ? 1 : -1;
	}
}