Sonam Bewafa asks questions
##### Submissions: 2103   Accuracy: 13.04%   Difficulty: Medium   Marks: 4

They declared Sonam as bewafa. Although she is not, believe me! She asked a number of queries to people regrading their position in a test. Now its your duty to remove her bewafa tag by answering simple queries. All the students who give test can score from 1 to 10^18. Lower the marks, better the rank. Now instead of directly telling the marks of student they have been assigned groups where marks are distributed in continuous intervals, you have been given l(i) lowest mark of interval i and r(i) highest marks in interval i. So marks distribution in that interval is given as l(i), l(i)+1, l(i)+2 . . . r(i).

Now Sonam ask queries in which she gives rank of the student (x) and you have to tell marks obtained by that student.

Note: rank1 is better than rank2 and rank2 is better than rank3 and so on and the first interval starts from 1.

Input:
The first line of input contains an integer T, denoting the no of test cases. Then T test cases follow. Each test case contains two space separated values N and Q denoting the no of groups and number of queries asked respectively. The next line contains N group of two integers separated by space which shows lowest marks in group i ie l(i) and highest marks in group i ie r(i) such that if i < j then r(i) < l(j). The next lines contain Q space separated integers x, denoting rank of student.

Output:
For each query output marks obtain by student whose rank is x(1<=x<=10^18).

Constraints:
1<=T<=50
1<=N<=10^5
1<=Q<=10^5
1<= l(i) < r(i) <=10^18
1<=x<=10^18

Example:
Input:
1
3 3
1 10 12 20 22 30
5 15 25

Output:
5 16 27

Explanation:
Intervals are from 1 to 10, second interval from 12 to 20 and third 22 to 30.
In this test case, from 1 to 10 , they are given the ranks from 1 to 10 but in the second interval, it is starting from 12 , so we will have to give its rank 11 and so on like this.

Rank:    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15......
Marks: 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16.....

So 5th rank will score 5 marks,15th rank will score 16 marks and 25th rank will score 27 marks.

#### ** For More Input/Output Examples Use 'Expected Output' option **

Contributor: vaibhav garg
Author: justrelax

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.