Under construction

Still being built. Things may change

Gahee and Process 2

Time limit3.5sMemory limit512 MB

Summary
Given n processes with id, remaining time, and priority, find which process id the scheduler runs at second T for each query.
Level

Medium7 of 10

Topics
Binary search, Sorting, Math
Solved
No attempts yet

Problem

Gahee was assigned to implement a scheduler. The criteria for choosing the process to run are as follows.

  • The process with the largest priority value.
  • If several processes share the largest priority value, the process with the smallest idid.

The scheduler Gahee wrote runs with the following algorithm.

  1. Choose the process to run according to the criteria. Let idsid_s be the idid of the chosen process. Run the process with idsid_s.
  2. After 1 second, the priority of every process except the one with idid equal to idsid_s increases by 1. The time needed to finish the process with idid equal to idsid_s decreases by 1.
  3. If any process still has remaining execution time, return to step 1. Otherwise, stop.

Only one process runs at a time. At second 1, the scheduler runs the process it chose first. Gahee wants to know which process the scheduler selects at second tt. Please help Gahee.

Input

The first line contains QQ and nn.

The next nn lines, from line 2 to line n+1n+1, each contain AiA_i, BiB_i, and CiC_i separated by spaces. This means the ii-th process has idid equal to AiA_i, needs BiB_i seconds to finish, and has initial priority CiC_i.

From line n+2n+2 to line n+1+Qn+1+Q, each query is given as follows.

TcT_c

TcT_c is a query asking which process idid the scheduler selects at second TcT_c.

Output

Print the answer to each of the QQ queries on its own line.

Constraints

  • 1≤n≤1051 \le n \le 10^5
  • 1≤Q≤501 \le Q \le 50
  • 1≤Ai,Bi,Ci≤10121 \le A_i, B_i, C_i \le 10^{12}
  • Each TcT_c is at least 1 and at most the smaller of 101710^{17} and the total time needed to finish all processes.

Examples2

  1. Example 1

    Input
    5 2
    1 5 1
    2 5 1
    1
    3
    5
    7
    10
    
    Expected output
    1
    1
    1
    1
    2
    
  2. Example 2

    Input
    5 2
    1 5 1000000
    2 10000000000 1
    1
    2
    3
    1000
    10000000000
    
    Expected output
    1
    1
    1
    2
    2