#P16762. [GKS 2020 #D] Locked Doors

[GKS 2020 #D] Locked Doors

Problem Description

Bangles is preparing to go on a tour of her local museum. The museum is made up of N rooms in a row, numbered from 1 to N from left to right. The rooms are connected by N-1 locked doors, each connecting a pair of adjacent rooms. Each door has a difficulty level indicating how difficult it is for Bangles to open the door. No two doors will have the same difficulty level. The door between the ii-th room and (i+1)(i+1)-th room has difficulty level Di{D_i}.

Bangles will pick one of the rooms to start in, and visit each of the rooms in the museum one at a time, taking pictures as she goes. She takes a picture in her starting room, then she repeats the following procedure until she has taken a picture in all the rooms: Of the two locked doors available to her, she will open the door with the lower difficulty level and take a picture in the newly unlocked room. If there is only one locked door available to her, then she will unlock that door. Once a door is unlocked, it remains unlocked.

Bangles is not yet sure which room she would like to start in, so she needs you to answer QQ queries. For the ii-th query, she would like to know: What is the Ki{K_i}-th room that she will take a picture in if she starts in the Si{S_i}-th room?

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow. The first line of each test case contains the two integers NN and QQ. The second line contains N1N-1 integers, describing the locked doors. The ii-th integer (starting from 1) is Di{D_i}. Then, QQ lines follow, describing the queries. The ii-th of these lines contains the two integers Si{S_i} and Ki{K_i}.

Output Format

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is a list of the answers for the QQ queries in order, separated by spaces.

2
5 4
90 30 40 60
3 4
3 1
1 5
4 3
10 2
6 2 4 5 9 30 7 1 8
6 8
6 8
Case #1: 5 3 5 2
Case #2: 8 8

Hint

In sample case #1, there are four queries:

  • In the first query, Bangle takes pictures in the rooms in the order 33, 22, 44, 55 and 11, so the answer is 55.
  • In the second query, Bangle takes pictures in the rooms in the order 33, 22, 44, 55 and 11, so the answer is 33.
  • In the third query, Bangle takes pictures in the rooms in the order 11, 22, 33, 44 and 55, so the answer is 55.
  • In the fourth query, Bangle takes pictures in the rooms in the order 44, 33, 22, 55, and 11, so the answer is 22.

In sample case #2, there are two queries:

  • In the first query, Bangle takes pictures in the rooms in the order 66, 55, 44, 33, 22, 11, 77, 88, 99 and 1010, so the answer is 88.
  • The second query is the same as the first, so the answer is also 88.

Limits

1T1001 \le T \le 100.

1Di1051 \le D_i \le 10^5, for all ii.

All DiD_i are distinct.

1SiN1 \le S_i \le N, for all ii.

1KiN1 \le K_i \le N, for all ii.

Test Set 1

2N10002 \le N \le 1000.

1Q10001 \le Q \le 1000.

Test Set 2

2N1052 \le N \le 10^5 and 1Q1051 \le Q \le 10^5 for at most 20 test cases.

For the remaining cases, 2N10002 \le N \le 1000 and 1Q10001 \le Q \le 1000.