#P1943E2. MEX Game 2 (Hard Version)

    ID: 10468 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 10 Uploaded By: Tags>binary searchgreedytwo pointers*3300

MEX Game 2 (Hard Version)

Description

This is the hard version of the problem. The only difference between the two versions is the constraint on tt, mm and the sum of mm. You can make hacks only if both versions of the problem are solved.

Alice and Bob play yet another game on an array aa of size nn. Alice starts with an empty array cc. Both players take turns playing, with Alice starting first.

On Alice's turn, she picks one element from aa, appends that element to cc, and then deletes it from aa.

On Bob's turn, he picks at most kk elements from aa, and then deletes it from aa.

The game ends when the array aa is empty. Alice's score is defined to be the MEX^\dagger of cc. Alice wants to maximize her score while Bob wants to minimize it. Find Alice's final score if both players play optimally.

The array will be given in compressed format. Instead of giving the elements present in the array, we will be giving their frequencies. Formally, you will be given mm, the maximum element in the array, and then m+1m + 1 integers f0,f1,,fmf_0, f_1, \ldots, f_{m}, where fif_i represents the number of times ii occurs in the array aa.

^\dagger The MEX\operatorname{MEX} (minimum excludant) of an array of integers is defined as the smallest non-negative integer which does not occur in the array. For example:

  • The MEX of [2,2,1][2,2,1] is 00, because 00 does not belong to the array.
  • The MEX of [3,1,0,1][3,1,0,1] is 22, because 00 and 11 belong to the array, but 22 does not.
  • The MEX of [0,3,1,2][0,3,1,2] is 44, because 00, 11, 22 and 33 belong to the array, but 44 does not.

Each test contains multiple test cases. The first line contains a single integer tt (1t1051 \leq t \leq 10^5) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers mm and kk (1m2105,1k1091 \le m \le 2 \cdot 10^5, 1 \le k \le 10^9).

The second line contains m+1m + 1 integers f0,f1,,fmf_0, f_1, \ldots, f_m (1fi1091 \le f_i \le 10^9).

It is guaranteed the sum of mm over all test cases does not exceed 21052 \cdot 10^5.

For each test case, find Alice's score if both players play optimally.

Input

Each test contains multiple test cases. The first line contains a single integer tt (1t1051 \leq t \leq 10^5) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers mm and kk (1m2105,1k1091 \le m \le 2 \cdot 10^5, 1 \le k \le 10^9).

The second line contains m+1m + 1 integers f0,f1,,fmf_0, f_1, \ldots, f_m (1fi1091 \le f_i \le 10^9).

It is guaranteed the sum of mm over all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, find Alice's score if both players play optimally.

Sample Input 1

5
1 4
4 5
2 1000000000
1000000000 1000000000 1000000000
3 2
2 3 100 1
1 1
2 2
3 1
1 1 1 1

Sample Output 1

2
1
3
2
1

Note

In the first test case, the array aa is [0,0,0,0,1,1,1,1,1][0, 0, 0, 0, 1, 1, 1, 1, 1]. A possible game with a score of 22 is as follows:

  1. Alice chooses the element 00. After this move, a=[0,0,0,1,1,1,1,1]a = [0, 0, 0, 1, 1, 1, 1, 1] and c=[0]c=[0].
  2. Bob chooses to remove the 33 elements 00, 00 and 11. After this move, a=[0,1,1,1,1]a = [0, 1, 1, 1, 1] and c=[0]c=[0].
  3. Alice chooses the element 11. After this move, a=[0,1,1,1]a = [0,1,1,1] and c=[0,1]c=[0,1].
  4. Bob removes the 44 remaining elements 00, 11, 11 and 11. After this move, a=[]a=[\,] and c=[0,1]c=[0,1].

At the end, c=[0,1]c=[0,1] which has a MEX of 22. Note that this is an example game and does not necessarily represent the optimal strategy for both players.

In the second test case, Alice can choose a 00 in her first turn, guaranteeing that her score is at least 11. While Bob can remove all copies element 11 in his first turn, thus guaranteeing that Alice's score cannot exceed 11. So Alice's score is 11 if both players play optimally.