#P16851. [GKS 2021 #D] Final Exam

[GKS 2021 #D] Final Exam

Problem Description

It's time for the final exam in algorithms and data structures!

Edsger prepared NN sets of problems. Each set consists of problems in an increasing difficulty sequence; the ii-th set can be described by 22 integers AiA_i and BiB_i (AiBiA_i \le B_i), which denotes that this set contains problems with difficulties Ai,Ai+1,,BiA_i, A_i + 1, \ldots, B_i. Among all problems from all sets, it is guaranteed that no 22 problems have the same difficulty.

This semester Edsger has to test MM students. He wants to test each student with exactly 11 problem from 11 of his sets. No 22 students can get the exact same problem, so when Edsger tests a student with some problem, he cannot use this problem anymore. Through countless lectures, exercises, and projects, Edsger has gauged student number jj to have skill level SjS_j, and wants to give that student a problem with difficulty SjS_j. Unfortunately, this is not always possible, as Edsger may have not prepared a problem of this difficulty, or he may have already asked this problem to some other student earlier. Therefore, Edsger will choose for the jj-th student a problem of difficulty PjP_j, in a way that PjSj|P_j - S_j| is minimal and a question of difficulty PjP_j was not already given to any of the students before the jj-th student. In case of ties, Edsger will always choose the easier problem. Note that the problem chosen for the jj-th student may affect problems chosen for all the students tested later, so you have to process students in the same order as they appear in the input.

As keeping track of all the problems can be fairly complicated, can you help Edsger and determine which problems he should give to all of his students?

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow.

Each test case begins with a line which contains 22 integers NN and MM: the number of problem sets, and the number of students, respectively. NN lines follow, describing the problem sets.

Each of these NN lines consists of 22 integers AiA_i and BiB_i denoting the easiest and the hardest problem in the ii-th problem set. Finally, the test case ends with a single line with MM integers S1,S2,,SMS_1, S_2, \ldots, S_M denoting students' skill levels in the order they will be tested.

Output Format

For each test case, output one line containing Case #xx: P1P_1 P2P_2 \ldots PMP_M, where xx is the test case number (starting from 11) and PjP_j is a difficulty of a problem that will be given to the jj-th student.

2
5 4
1 2
6 7
9 12
24 24
41 50
14 24 24 4
1 1
42 42
24
Case #1: 12 24 11 2
Case #2: 42

Hint

In Sample Case #11, we have N=5N = 5 problem sets and M=4M = 4 students.

  • For the first student, we are looking for a problem with the difficulty closest to their skill level S1=14S_1 = 14. The problem with the minimum difference is problem with difficulty 1212, which we can find in the third problem set, so P1=12P_1 = 12.
  • For the second student, we are looking for a problem with the difficulty closest to their skill level S2=24S_2 = 24. Fortunately, we can find a problem of this exact difficulty in the fourth problem set, so P2=24P_2 = 24.
  • For the third student, we are once again looking for a problem with the difficulty closest to the skill level S3=24S_3 = 24. As we already used the problem with difficulty 2424, we cannot use this problem. The problem closest in difficulty is 1111, as 1212 was already used as well. Therefore P3=11P_3 = 11.
  • Finally, for the fourth student, we are looking for the problem closest to his skill level S4=4S_4 = 4. We have 22 problems with the same difference: 22 and 66. We choose the easier problem, so P4=2P_4 = 2.

In Sample Case #22, we have N=1N = 1 problem set and M=1M = 1 student. In the only problem set, there is only 11 problem, so we have to use this problem to examine the first and only student, so P1=42P_1 = 42.

Limits

1T1001 \le T \le 100.

Among all problem sets, no 22 problems have the same difficulty.

The number of problems in total is greater than or equal to the number of students.

Test Set 11

1N10001 \le N \le 1000.

1M10001 \le M \le 1000.

1AiBi10001 \le A_i \le B_i \le 1000 for all ii.

1Sj10001 \le S_j \le 1000 for all jj.

Test Set 22

1N1051 \le N \le 10^5.

1M1051 \le M \le 10^5.

1AiBi10181 \le A_i \le B_i \le 10^{18} for all ii.

1Sj10181 \le S_j \le 10^{18} for all jj.