Draining the Job Queue

easy games dp 2000 ms · 256 MB

Draining the Job Queue

Two worker schedulers, A and B, take turns draining a shared job queue. A always moves first. Each scheduler is configured with the same fixed set of batch sizes S — on its turn, a scheduler must remove exactly s jobs from the front of the queue for some s in S, provided at least s jobs remain. A scheduler that cannot make a legal move (every batch size in S is larger than the number of jobs left) stalls and loses the round. The scheduler that removes the very last job earns the on-call bonus and wins.

You are given S and q independent starting queue sizes. For each starting size, assuming both schedulers play optimally, determine who wins.

Input

The first line contains two integers k and q (1 ≤ k ≤ 10, 1 ≤ q ≤ 10^6) — the number of allowed batch sizes and the number of queries.

The second line contains k distinct integers s_1, ..., s_k (1 ≤ s_i ≤ 100) — the allowed batch sizes, in no particular order.

The third line contains q integers n_1, ..., n_q (0 ≤ n_i ≤ 10^6), the starting number of jobs in the queue for each query.

Output

Print q lines. For the i-th query, print A if scheduler A wins with optimal play starting from a queue of size n_i, or B if scheduler B wins.

Notes

In the first example S = {1, 4}. Starting from n = 2, A must remove 1 job (4 is too many), leaving 1 job for B, who removes it and wins. Starting from n = 4, A removes all 4 jobs at once and wins immediately.

Starting from n = 0 the queue is already empty, so A has no legal move and B wins by default.

Example 1

Input

2 3
1 4
2 4 0

Output

B
A
B

Example 2

Input

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

Output

B
B
A
A
B
A
A
B
B
A

Hints · reveal one at a time if you're stuck

Hint 1

This is a subtraction game. Since the allowed batch sizes are arbitrary, there is no simple formula, so think about deciding each queue size from smaller ones: a size is winning for the player to move exactly when some legal move leaves the opponent in a losing size.

Hint 2

Fill a table of win or lose for every size up to the largest query, starting from 0 where the mover is stuck and therefore loses. Each entry needs only up to 10 earlier entries, so the table is cheap, and each query is then a lookup.

report an issue Prefer your own editor? sooks prep 98 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.