## Magical Candy Store november long challenge problem solution code with explaination-

Chef and Chefu are at a magical candy store playing a game with the following rules:

- There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed.
- Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter.
- There is a sequence of distinct integers . The game consists of turns; in the -th turn, the open counter offers only candies to the player present at this counter. This player should choose a positive number of candies to accept, where .
- If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter).
- After each turns, the counter which was currently open is closed and the counter which was currently closed is opened.
- The primary goal of each player is to maximise his own number of candies after turns. As a second priority, each player wants to minimise the number of candies his opponent has after turns.

You should process queries. In each query, you are given and you should find the number of candies Chef has after turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo .

### Input

- The first line of the input contains a single integer denoting the number of test cases. The description of test cases follows.
- The first line of each test case contains a single integer .
- The second line contains space-separated integers .
- The third line contains a single integer .
- Each of the next lines contains a single integer describing a query.

### Output

For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo .

### Constraints

- for each valid
- are pairwise distinct
- the sum of over all test cases does not exceed

### Subtasks

Subtask #1 (15 points):

Subtask #2 (85 points): original constraints

### Example Input

```
1
4
4 10 2 1
2
4
5
```

### Example Output

```
17
21
```

### Explanation

Example case 1: In the -st, -nd and -rd turn, Chef takes , and candies ( in total) respectively. In the -th turn, Chef takes candy ( in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the -th round, Chef can take candies, so he has candies.

### Solution-

### Solution -

We can make 4 cases here and ans can differ according to the poison of 1.

## Comments

## Post a Comment