#P1935E. Distance Learning Courses in MAC

    ID: 9147 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 9 Uploaded By: Tags>bitmasksbrute forcedata structuresgreedymath*2400

Distance Learning Courses in MAC

No submission language available for this problem.

Description

The New Year has arrived in the Master's Assistance Center, which means it's time to introduce a new feature!

Now students are given distance learning courses, with a total of nn courses available. For the ii-th distance learning course, a student can receive a grade ranging from xix_i to yiy_i.

However, not all courses may be available to each student. Specifically, the jj-th student is only given courses with numbers from ljl_j to rjr_j, meaning the distance learning courses with numbers lj,lj+1,,rjl_j, l_j + 1, \ldots, r_j.

The creators of the distance learning courses have decided to determine the final grade in a special way. Let the jj-th student receive grades clj,clj+1,,crjc_{l_j}, c_{l_j + 1}, \ldots, c_{r_j} for their distance learning courses. Then their final grade will be equal to cljc_{l_j} | clj+1c_{l_j + 1} | \ldots | crjc_{r_j}, where | denotes the bitwise OR operation.

Since the chatbot for solving distance learning courses is broken, the students have asked for your help. For each of the qq students, tell them the maximum final grade they can achieve.

Each test consists of multiple test cases. The first line contains a single integer tt (1t21041 \leq t \leq 2 \cdot 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1n21051 \le n \le 2 \cdot 10^5) — the number of distance learning courses.

Each of the following nn lines contains two integers xix_i and yiy_i (0xiyi<2300 \le x_i \le y_i < 2^{30}) — the minimum and maximum grade that can be received for the ii-th course.

The next line contains a single integer qq (1q21051 \le q \le 2\cdot10^5) — the number of students.

Each of the following qq lines contains two integers ljl_j and rjr_j (1ljrjn1 \le l_j \le r_j \le n) — the minimum and maximum course numbers accessible to the jj-th student.

It is guaranteed that the sum of nn over all test cases and the sum of qq over all test cases do not exceed 21052\cdot10^5.

For each test case, output qq integers, where the jj-th integer is the maximum final grade that the jj-th student can achieve.

Input

Each test consists of multiple test cases. The first line contains a single integer tt (1t21041 \leq t \leq 2 \cdot 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1n21051 \le n \le 2 \cdot 10^5) — the number of distance learning courses.

Each of the following nn lines contains two integers xix_i and yiy_i (0xiyi<2300 \le x_i \le y_i < 2^{30}) — the minimum and maximum grade that can be received for the ii-th course.

The next line contains a single integer qq (1q21051 \le q \le 2\cdot10^5) — the number of students.

Each of the following qq lines contains two integers ljl_j and rjr_j (1ljrjn1 \le l_j \le r_j \le n) — the minimum and maximum course numbers accessible to the jj-th student.

It is guaranteed that the sum of nn over all test cases and the sum of qq over all test cases do not exceed 21052\cdot10^5.

Output

For each test case, output qq integers, where the jj-th integer is the maximum final grade that the jj-th student can achieve.

Sample Input 1

3
2
0 1
3 4
3
1 1
1 2
2 2
4
1 7
1 7
3 10
2 2
5
1 3
3 4
2 3
1 4
1 2
6
1 2
2 2
0 1
1 1
3 3
0 0
4
3 4
5 5
2 5
1 2

Sample Output 1

1 5 4 
15 11 15 15 7 
1 3 3 3

Note

In the first test case:

  1. The maximum grade for the first student is 11:
    • On the first distance learning course, he will receive a grade of 11.
    Therefore, the final grade is 11.

  2. The maximum grade for the second student is 55:
    • On the first distance learning course, he will receive a grade of 11.
    • On the second distance learning course, he will receive a grade of 44.
    Therefore, the final grade is 11 | 44 == 55.
  3. The maximum grade for the third student is 44:
    • On the second distance learning course, he will receive a grade of 44.
    Therefore, the final grade is 44.

In the second test case:

  1. The maximum grade for the first student is 1515:
    • On the first distance learning course, he will receive a grade of 77.
    • On the second distance learning course, he will receive a grade of 44.
    • On the third distance learning course, he will receive a grade of 88.
    Therefore, the final grade is 77 | 44 | 88 == 1515.

  2. The maximum grade for the second student is 1111:
    • On the third distance learning course, he will receive a grade of 99.
    • On the fourth distance learning course, he will receive a grade of 22.
    Therefore, the final grade is 99 | 22 == 1111.