CF-GYM-101498-F

While cooking your dinner, you will need n ingredients. Initially, all ingredients are in the refrigerator.

You are not allowed to keep more than k ingredients outside the refrigerator at the same time. If there are k ingredients outside the refrigerator, and you need to use another ingredient from it, then you must do the following:

1 Open the refrigerator.
2 Return an ingredient that is currently outside.
3 Take the required ingredient.

Whenever you need an ingredient, you will open the refrigerator only if it’s not outside. Each time you open the refrigerator you can take only one item. Your task is to minimize the number of times you will need to open the refrigerator.

Input

The first line contains an integer T (1  ≤  T  ≤  100), where T is the number of test cases.

Each case contains two lines. The first line contains two integers n and k (1  ≤  n, k  ≤  105), the number of ingredients needed, and the maximum allowed number of ingredients that can be kept outside the refrigerator at the same time.

The second line contains n integers a1, a2, …, an (1  ≤  ai  ≤  109), where ai is the ID of the ith ingredient you will need. Ingredients must be used in the order given in the input.

Output

For each test case, print a single integer that represents the minimum number of times you will open the refrigerator.

Example

Input

2
5 3
2 4 5 2 1
7 3
1 2 3 4 3 2 1

Output

4
5

Note

In the first test case, you can keep up to 3 items outside the refrigerator. You must open the refrigerator 3 times to use the first three ingredients (2, 4, and 5), and then keep them outside the refrigerator. The fourth ingredient (2) is already outside the refrigerator, so you can use it directly. You cannot use the fifth item because there are 3 items outside the refrigerator, so you must open the refrigerator, return an item, and take the fifth ingredient from the refrigerator and use it.

影响次数的关键在于每次放回去的时候放哪个。每次需要把在外面的元素,下一个第一个出现的位置最远的那个元素,放回去。没有下一次出现的,就更优先放回去。

模拟即可,过程实现比较复杂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<set>
#include<map>
#include<algorithm>
#include<queue>
#include<vector>
#include<functional>
using namespace std;
typedef long long ll;
int a[100000 + 10];
int n, k;
map<int, int, greater<int> >s;
map<int, queue<int> >index;
map<int, int>outside;
int ans;
void take_out(int i, int x)
{
outside[x] = 1;
index[x].pop();
if (index[x].size())
{
s[index[x].front()] = x;
}
else
{
s[n + x] = x;
}
ans++;
}
void push_in(int i, int x)
{
outside[x] = 0;
s.erase(i);
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
index.clear();
s.clear();
outside.clear();
ans = 0;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
index[a[i]].push(i);
}
for (int i = 0; i < n; i++)
{
if (outside[a[i]])
{
if (index[a[i]].size())
index[a[i]].pop();
s.erase(i);
if (index[a[i]].size())
{
s[index[a[i]].front()] = a[i];
}
else
{
s[a[i] + n] = a[i];
}
}
else
{
if (s.size() < k)
{
take_out(i, a[i]);
}
else
{
push_in(s.begin()->first, s.begin()->second);
take_out(i, a[i]);
}
}
}
cout << ans << endl;
}
}