Little N and Little O will participate in a grand programming competition in November 2022, the NOIP!
Little P will preside over the competition as a referee.
Little N and little O each lead a team of people, with players numbered from to in each team. Each player has a corresponding programming level. Specifically, in the team led by little N, the programming level of the player numbered is ; in the team led by little O, the programming level of the player numbered () is . Furthermore, each of and forms a permutation of to .
Before each game, Little P will choose two parameters , , which means that the members of this game will be chosen from all players in the range on both teams. Little N and Little O will then select parameters , by rolling dice, and only players whose numbers belong to can participate. In order to provide the audience with the most exciting game, both teams will send their player whose number is in that has the highest programming level to participate in the game. Assuming that the level of the player sent by little N is , and the level of the player sent by little O is , then the excitement level of the game is .
There are a total of games in NOIP, and the parameters and of each game have been determined, but and have not been extracted yet. Little P wants to know, for each game, the sum of the excitement values of the game under all possible parameters . Since the answer may be very large, you only need to output the result modulo for each game.
Input Specification
The first line contains two positive integers and , which respectively represent the test case number and the number of participants. Samples will have .
The second line contains positive integers, the -th integer being , which represents the programming level of player number in Little N's team.
The third line contains positive integers, the -th integer being , which represents the programming level of player number in Little O's team.
The fourth line contains a positive integer , indicating the number of games played.
In the next line, line contains two positive integers , , representing the parameters , of the -th match.
Output Specification
Output Q lines, the -th line should contain a non-negative integer, which represents the result, modulo , of the sum of excitement values of all possible games in the -th round.
Sample Input 1
0 2
2 1
1 2
1
1 2
Sample Output 1
8
Explanation of Sample 1
- When , , Little N will send player No. 1, and Little O will send player No. 2. The excitement is .
- When , , Little N will send player No. 1, and Little O will send player No. 1. The excitement is .
- When , , Little N will send player No. 2, and Little O will send player No. 2, and excitement is .
Additional Samples
Additional sample cases can be found here.
- The second case satisfies the constraints of cases 1-2.
- The third case satisfies the constraints of cases 3-5.
Constraints
For all data, it is guaranteed that , , , and , are permutations of the integers from to .
Case | random | random | ||
---|---|---|---|---|
yes | yes | |||
no | no | |||
yes | yes | |||
no | ||||
no | ||||
Comments