P22415 [CodeChef] DEQUEUE
提高+
题目描述

Read problem statements in [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.

You are given a double-ended queue $Q$. Initially, it contains elements $Q_{1}, Q_{2}, \ldots, Q_{M}$ in this order. Each of these elements is an integer between $1$ and $N$ (inclusive) and each integer between $1$ and $N$ (inclusive) occurs in the queue at least once.

We want to pop some (possibly zero) elements from the front of the queue and some (not necessarily the same number, possibly zero) elements from the back. Among all the popped elements, each integer between $1$ and $N$ (inclusive) should appear at least once. Find the smallest possible total number of elements we need to pop.

------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $M$ space-separated integers $Q_{1}, Q_{2}, \ldots, Q_{M}$.

------ Output ------
For each test case, print a single line containing one integer — the minimum number of elements we need to pop.

------ Constraints ------
$1 ≤ T ≤ 10$
$1 ≤ N ≤ 10^{5}$
$N ≤ M ≤ 2 \cdot 10^{5}$
for each $i$ ($1 ≤ i ≤ N$), there is at least one element equal to $i$ in the queue
the sum of $N$ over all test cases does not exceed $10^{5}$
the sum of $M$ over all test cases does not exceed $2 \cdot 10^{5}$

----- Sample Input 1 ------
3

2 2

1 2

2 3

1 2 1

3 6

1 1 1 3 2 2
----- Sample Output 1 ------
2

2

4
----- explanation 1 ------
Example case 1: Since $N = M$ and all elements are pairwise distinct, we have to pop everything to get each value at least once.

Example case 2: We can pop the first $2$ or the last $2$ elements.

Example case 3: We can pop the first element and the last $3$ elements, so the answer is $1 + 3 = 4$.

输入格式

见题面

输出格式

见题面

样例输入
3 2 2 1 2 2 3 1 2 1 3 6 1 1 1 3 2 2
样例输出
2 2 4