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.util.*;import java.io.*;import java.math.*;

public class Main
{
    static int find_just_great(int n,ArrayList<Integer> li){
        int len=li.size()-1,ret=0;
        int l=1,r=len;
        while(l<=r){
            int mid=l+(r-l)/2;
            if(li.get(mid)>=n){
                r=mid-1;
                ret=mid;
            }
            else
                l=mid+1;
        }

        return ret;
    }

     static int find_just_less(int n,ArrayList<Integer> li){
        int len=li.size()-1,ret=0;
        int l=1,r=len;
        while(l<=r){
            int mid=l+(r-l)/2;
            if(li.get(mid)<=n){
                l=mid+1;
                ret=mid;
            }
            else
                r=mid-1;
        }

        return ret;
    }

    public static void process()throws IOException
    {
        int n=ni(),arr[]=new int[n+1];
        for(int i=1;i<=n;i++) arr[i]=ni();

        ArrayList<Integer> set[]=new ArrayList[30];
        for(int i=1;i<30;i++) {set[i]=new ArrayList<Integer>();set[i].add(0);}

        for(int i=1;i<=n;i++) set[arr[i]].add(i);
        int res=1;
        for(int k=1;k<=26;k++){
            int len=set[k].size()-1;
            if(len==1 || len==0)
                continue;
            for(int i=1;i<=len/2;i++){

                int l=set[k].get(i),r=0;
                for(int j=i+1;j+i-1<=len;j++){
                    //System.out.println(i+" "+j+" "+res);
                    r=set[k].get(j);
                    //res=Math.max(res, 2*i);
                    for(int p=1;p<=26;p++){
                    if(p==k){
                        //System.out.println(p);
                        //System.out.println("fasd"+" "+k+" "+len);
                        res=Math.max(len,res);
                       // System.out.println(len);
                        continue;
                    }
                        int low=find_just_great(l,set[p]),high=find_just_less(r,set[p]);
                        //System.out.println(l+" "+r+" "+p+" "+low+" "+high);
                        if(high<low || high==0 || low==0){
                            continue;
                            //high=low;
                        }
                        //System.out.println(high+" "+low+" "+k+" "+res+" "+len);
                        //System.out.println(p+" "+k+" "+high+" "+low);
                        res=Math.max(res, high-low+1+2*i);
                    }

                }
            }
        }
        pn(res);
    }


    static AnotherReader sc;
    static PrintWriter out;
    public static void main(String[]args)throws IOException
    {
        out = new PrintWriter(System.out);
        sc=new AnotherReader();
        boolean oj = true;

        oj = System.getProperty("ONLINE_JUDGE") != null;
        if(!oj) sc=new AnotherReader(100);

        long s = System.currentTimeMillis();
        int t=ni();
        while(t-->0)
            process();
        out.flush();
        if(!oj)
            System.out.println(System.currentTimeMillis()-s+"ms");
        System.out.close();  
    }

    static void pn(Object o){out.println(o);}
    static void p(Object o){out.print(o);}
    static void pni(Object o){out.println(o);System.out.flush();}
    static int ni()throws IOException{return sc.nextInt();}
    static long nl()throws IOException{return sc.nextLong();}
    static double nd()throws IOException{return sc.nextDouble();}
    static String nln()throws IOException{return sc.nextLine();}
    static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
    static boolean multipleTC=false;



/////////////////////////////////////////////////////////////////////////////////////////////////////////

    static class AnotherReader{BufferedReader br; StringTokenizer st;
    AnotherReader()throws FileNotFoundException{
    br=new BufferedReader(new InputStreamReader(System.in));}
    AnotherReader(int a)throws FileNotFoundException{
    br = new BufferedReader(new FileReader("input.txt"));}
    String next()throws IOException{
    while (st == null || !st.hasMoreElements()) {try{
    st = new StringTokenizer(br.readLine());}
    catch (IOException  e){ e.printStackTrace(); }}
    return st.nextToken(); } int nextInt() throws IOException{
    return Integer.parseInt(next());}
    long nextLong() throws IOException
    {return Long.parseLong(next());}
    double nextDouble()throws IOException { return Double.parseDouble(next()); }
    String nextLine() throws IOException{ String str = ""; try{
    str = br.readLine();} catch (IOException e){
    e.printStackTrace();} return str;}}
    
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
    
    
    