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


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {

	static long sx = 0, sy = 0, mod = (long) (1e9 + 7);

	 static ArrayList<Integer>[] a;
	static long[][][] dp;
	static long[] fa;
	static long[] farr;
	public static PrintWriter out;
	static ArrayList<pair> pa = new ArrayList<>();
	static long[] fact = new long[(int) 1e6];
	static StringBuilder sb = new StringBuilder();
	static boolean cycle = false;
	static long m = 998244353;
	static long[] no, col;
	// static String s;
	static int k = 0, n = 0;
	static int[] c;
	static long ans = 0;
	static HashMap<Integer, Integer> hm;
	static int[] depth;
	static int[] arr;
	static int[][] p;
	static int[][] s;


	public static void main(String[] args) throws IOException {

		// Scanner scn = new Scanner(new BufferedReader(new
		// InputStreamReader(System.in)));

		out = new PrintWriter(System.out);
		Reader scn = new Reader();

		int te = scn.nextInt();

		while (te-- != 0) {
			
			
			
			int n = scn.nextInt();
			
			arr= new int[n+1];
			a = new ArrayList[28];
			
			p = new int[27][n+1];
			
			for(int i=0; i<28; i++)
				 a[i] = new ArrayList<>();
			
			
		    int ans = 0 ;
		    int maxi = 0 ;
		    
		   
		    for(int i=1 ; i<=26 ; i++)
		        for(int j=0 ; j<=n ; j++)
		            p[i][j] = 0 ;
		    
		    
		    HashMap<Integer,Integer> hm = new HashMap<>();
		    
		    
		    for(int i=1 ; i<=n ; i++)
		    {
		        arr[i] = scn.nextInt();
		        
		        hm.put(arr[i], hm.containsKey(arr[i])?hm.get(arr[i])+1:1);
		        
		        maxi = Math.max(maxi,hm.get(arr[i])) ;
		    }
		    ans = maxi ;
		    
		    for(int i=1 ; i<=n ; i++)
		    {
		        p[arr[i]][i]++ ;
		        a[arr[i]].add(i) ;
		    }
		    
		    
		    for(int i=1 ; i<=26 ; i++)
		    {
		        for(int j=2 ; j<=n ; j++)
		            p[i][j] += p[i][j-1] ;
		    }
		    
		    
		    
		    for(int i=1 ; i<=26 ; i++)
		    {
		        if(!hm.containsKey(i) || hm.get(i)<2)
		            continue ;
		        int l = 0 ;
		        int r = a[i].size()-1 ;
		        while(l<r)
		        {
		            int pos_l = a[i].get(l) ;
		            int pos_r = a[i].get(r) ;
		            int t = 0 ;
		            for(int k=1 ; k<=26 ; k++)
		            {
		                if(k == i)
		                    continue ;
		                int tot = p[k][pos_r]-p[k][pos_l] ;
		                t = Math.max(t,tot) ;
		            }
		            ans = Math.max(ans,t+2*(l+1)) ;
		            l++ ;
		            r-- ;
		        }
		    }
		    out.println(ans);
		}

		out.close();

	}

	private static int f(int num) {

		int ans = 0;

		// i-j scnd elemnt

		for (int i = 1; i < n - 1; i++) {

			for (int j = i; j < n - 1; j++) {

				int temp = 0;

				if (i == j) {

					temp = p[n - 1][num];

					
				}

				else {

					int l = p[i - 1][num];
					int r = s[j + 1][num];

					int mid = get(i, j);

					temp = l + r + mid;

				}

				ans = Math.max(ans, temp);
			}
		}

		return ans;
	}

	private static int get(int i, int j) {

		i--;

		int ans = 0;

		for (int val = 1; val <= 26; val++)
			ans = Math.max(ans, p[j][val] - p[i][val]);

		return ans;
	}

	// _________________________TEMPLATE_____________________________________________________________

	// public static long lcm(long x, long y) {
	//
	// return (x * y) / gcd(x, y);
	// }
	//
	// private static long gcd(long x, long y) {
	// if (x == 0)
	// return y;
	//
	// return gcd(y % x, x);
	// }
	//
	// static class comp implements Comparator<pair> {
	//
	// @Override
	// public int compare(pair p1, pair p2) {
	// return p1.i - p2.i;
	// }
	//
	// }
	//
	// public static long pow(long a, long b) {
	//
	// if (b < 0)
	// return 0;
	// if (b == 0 || b == 1)
	// return (long) Math.pow(a, b);
	//
	// if (b % 2 == 0) {
	//
	// long ret = pow(a, b / 2);
	// ret = (ret % mod * ret % mod) % mod;
	// return ret;
	// }
	//
	// else {
	// return ((pow(a, b - 1) % mod) * a % mod) % mod;
	// }
	// }

	private static class pair implements Comparable<pair> {

		long no;
		long s, e;

		pair(long n, long c, long d) {
			no = n;
			s = c;
			e = d;

		}

		@Override
		public int compareTo(pair o) {

			return 1;

		}

		// @Override
		//
		// public int hashCode() {
		// return i;
		// }
		//
		// @Override
		//
		// public boolean equals(Object o) {
		//
		// pair p = (pair) o;
		// return this.i == p.i;
		// }

	}

	private static String reverse(String s) {

		return new StringBuilder(s).reverse().toString();
	}

	public static class Reader {
		final private int BUFFER_SIZE = 1 << 16;
		private DataInputStream din;
		private byte[] buffer;
		private int bufferPointer, bytesRead;

		public Reader() {
			din = new DataInputStream(System.in);
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public Reader(String file_name) throws IOException {
			din = new DataInputStream(new FileInputStream(file_name));
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = bytesRead = 0;
		}

		public String readLine() throws IOException {
			byte[] buf = new byte[1000000 + 1]; // line length
			int cnt = 0, c;
			while ((c = read()) != -1) {
				if (c == '\n')
					break;
				buf[cnt++] = (byte) c;
			}
			return new String(buf, 0, cnt);
		}

		public int nextInt() throws IOException {
			int ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');

			if (neg)
				return -ret;
			return ret;
		}

		public long nextLong() throws IOException {
			long ret = 0;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();
			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');
			if (neg)
				return -ret;
			return ret;
		}

		public double nextDouble() throws IOException {
			double ret = 0, div = 1;
			byte c = read();
			while (c <= ' ')
				c = read();
			boolean neg = (c == '-');
			if (neg)
				c = read();

			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');

			if (c == '.') {
				while ((c = read()) >= '0' && c <= '9') {
					ret += (c - '0') / (div *= 10);
				}
			}

			if (neg)
				return -ret;
			return ret;
		}

		private void fillBuffer() throws IOException {
			bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
			if (bytesRead == -1)
				buffer[0] = -1;
		}

		private byte read() throws IOException {
			if (bufferPointer == bytesRead)
				fillBuffer();
			return buffer[bufferPointer++];
		}

		public void close() throws IOException {
			if (din == null)
				return;
			din.close();
		}

		public int[] nextIntArray(int n) throws IOException {
			int[] arr = new int[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextInt();
			}
			return arr;
		}

		public long[] nextLongArray(int n) throws IOException {
			long[] arr = new long[n];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLong();
			}
			return arr;
		}

		public int[][] nextInt2DArray(int m, int n) throws IOException {
			int[][] arr = new int[m][n];
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++)
					arr[i][j] = nextInt();
			}
			return arr;
		}

		public long[][] nextInt2DArrayL(int m, int n) throws IOException {
			long[][] arr = new long[m][n];
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++)
					arr[i][j] = nextInt();
			}
			return arr;
		}
		// kickstart - Solution
		// atcoder - Main
	}

}