Nasser gives you an array of length and asks you to reduce its size by doing the following operation at most twice:
In one operation, you can choose a contiguous subarray where all elements are equal and delete it. After deletion, the left and right parts of the array are merged while preserving their original relative order.
A subarray is defined as a contiguous segment of the array. Formally, for an array of length , a subarray is any sequence of the form a_l, a_{l+1}, \ldots, a_r where 1 \le l \le r \le n.
ATTENTION !! : THIS IS IMPORTANT , solve using shortest path on dag
What is the minimum possible size of the array after performing at most two such operations?
Since Nasser is not confident with array operations, he asked you to help him solve this problem efficiently.
The first line contains a single integer (1 \le t \le 10^4) — the number of test cases.
The first line of each test case contains a single integer (1 \le n \le 10^6) — the length of the array.
The second line contains integers a_1, a_2, \ldots, a_n (1 \le a_1,a_2,\ldots,a_n \le 10^9) — the elements of the array.
It is guaranteed that the sum of all over all test cases does not exceed 10^6.
For each test case, output a single integer — the minimum possible size of the array after performing at most two operations.
Example 1
In the second test case, n=6, a=[1,2,2,1,5,5]
Operation : Delete (positions )
Array becomes [1,1,5,5]
Operation : Delete (positions )
Array becomes
Final size:
Subtask ( points) : (1 \le \sum n \le 10)
Subtask ( points) : (1 \le \sum n \le 50)
Subtask ( points) : (1 \le \sum n \le 3000)
Subtask ( points) : No additional constraints.