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.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;

public class CFB {
    public static void main(String[] args) throws FileNotFoundException, InterruptedException {
        InputStream stream = System.in;
        Logger logger = new Logger();
        if (args.length>1 && args[0].equals("LOCAL") && args[1].equals("YES")) {
            File inputFile = new File("resources/input.in");
            stream = new DataInputStream(new FileInputStream(inputFile));
            logger.enableLogging(true, "resources/logs.txt");
        }
        InputReader in = new InputReader(stream);
        PrintWriter out = new PrintWriter(System.out);
        Thread th = new Thread(null, () -> new Task1().solve(in, out, logger), "Task1", 1 << 24);
        th.start();
        th.join();
        out.close();
        logger.close();
    }

    static class Task1 {
        int n;
        int[] a;
        int[][] sum;
        void solve(InputReader in, PrintWriter out, Logger logger) {
            int t = in.nextInt();
            while(t-- > 0) {
                n = in.nextInt();
                a = new int[n];
                sum = new int[26][n+1];

                for (int i=0; i<n; i++) {
                    a[i] = in.nextInt()-1;

                    for (int j=0; j<26; j++) {
                        sum[j][i+1] = sum[j][i];
                    }
                    sum[a[i]][i+1]++;
                }

                // a in question = ft
                int ans = 0;
                for (int ft=0; ft<26; ft++) {
                    ans = Math.max(ans, determineMax(ft));
                }
                out.println(ans);
            }
        }

        int determineMax(int ft) {
            int i=0, j=n-1, count=0;
            int bestAns = 0;
            while(i<=j) {
                while (i<n && a[i]!=ft) { i++; }
                while (j>=0 && a[j]!=ft) { j--; } // move one step
                if (i>j || i>=n || j<0) break;
                count++;
                int ans = 2*count;
                if (i == j){
                    ans--;
                } else if (i < j) {
                    ans += maxSum(i+1, j-1);
                }
                bestAns = Math.max(bestAns, ans);
                i++;
                j--;
            }
            return bestAns;
        }

        int maxSum(int start, int end) {
            int ans = 0;
            for (int i=0; i<26; i++) {
                ans = Math.max(ans, sum[i][end+1] - sum[i][start]);
            }
            return ans;
        }


        int lowerBound(long[] a, int lo, long key) { // first element not less than key
            int hi=a.length-1;
            while(hi>=lo) {
                int mid = (lo+hi)>>1;
                if(a[mid] < key) {
                    lo = mid + 1;
                } else {
                    hi = mid - 1;
                }
            }
            return lo;
        }
    }

    static class Logger {
        private PrintWriter logger;
        private boolean loggingEnabled;

        void enableLogging(boolean enable, String filename) {
            this.loggingEnabled = enable;
            try {
                if (loggingEnabled) this.logger = new PrintWriter(filename);
            } catch (FileNotFoundException ignored) {
            }
        }

        void log(Object s) {
            if (loggingEnabled) {
                logger.print(s.toString());
            }
        }

        void logln(Object s) {
            if (loggingEnabled) {
                logger.println(s.toString());
            }
        }

        void close() {
            if (loggingEnabled) logger.close();
        }
    }

    static class InputReader {
        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, numChars;
        private SpaceCharFilter filter;

        InputReader(InputStream stream) {
            this.stream = stream;
        }

        int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        int nextInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        String next() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        interface SpaceCharFilter {
            boolean isSpaceChar(int ch);
        }
    }
}
