#d2711. 三回文序列

三回文序列

Triple Palindrome Sequence

Problem Description

You are given a sequence aa of length nn.

We define a triple palindrome sequence as a sequence of the form $\underbrace{a\dots a}_{k_1}\ \underbrace{b\dots b}_{k_2}\ \underbrace{a\dots a}_{k_1}$. For example: [1,1,1,2,2,1,1,1][1,1,1,2,2,1,1,1] is a triple palindrome sequence, while [1,1,1,2,2,2,3,3,3][1,1,1,2,2,2,3,3,3] and [1,1,2,2,1][1,1,2,2,1] are not.

Now, find the length of the longest triple palindrome subsequence in the sequence aa.

Note that k1,k2k_1, k_2 may be 00.

Input Format

The first line gives an integer TT, the number of test cases.

For each test case:

The first line gives the length nn of the sequence.

The second line gives a1,a2,a3,…,ana_1, a_2, a_3, \dots, a_n.

Output Format

For each test case, output the length of the longest triple palindrome subsequence.

Sample 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

Sample Output

7
2
4
1
1
3

Constraints

1≤t≤20001\le t\le 2000

1≤∑n≤2⋅105, 1≤ai≤261\le \sum n\le 2\cdot 10^5,\ 1\le a_i \le 26