Decommissioning Duel

easy games bit-manipulation math 1000 ms · 256 MB

Decommissioning Duel

The old data center is being shut down, and two on-call engineers, Ada and Ben, are left to power off whatever is still running. The hall has n racks; rack i still holds a_i live servers.

They settle it with a duel. Ada goes first, then they alternate. On each turn the engineer picks one rack that still has live servers and powers off any positive number of servers in that rack (possibly all of them). Servers in different racks can never be powered off in the same turn. Whoever powers off the very last live server in the hall is spared from writing the postmortem.

Both engineers play optimally. Determine who is spared. If Ada can force a win, also count her winning first moves: a first move is determined by the rack she picks and how many servers she powers off there, and it is winning if, after it, Ada can still force a win however Ben plays.

Each input describes several independent halls.

Input

The first line contains t (1 ≤ t ≤ 10^4), the number of halls.

Each hall is described by two lines. The first contains n (1 ≤ n ≤ 2·10^5), the number of racks. The second contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9), the number of live servers in each rack.

The sum of n over all halls does not exceed 2·10^5.

Output

For each hall print one line. If Ada can force a win, print Ada, a space, and the number of her winning first moves. Otherwise print Ben.

Notes

In the first example, the first hall has a single rack: Ada powers off all 5 servers at once, and that is her only winning move (leaving any server lets Ben finish the rack). In the second hall, whatever Ada does, Ben powers off the last server. In the third hall (racks of 3, 4 and 5 servers) Ada's only winning first move is to power off 2 servers in the first rack, leaving 1, 4 and 5.

Example 1

Input

3
1
5
2
1 1
3
3 4 5

Output

Ada 1
Ben
Ada 1

Example 2

Input

2
4
6 7 12 9
3
2 7 5

Output

Ada 3
Ben

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

Hint 1

This is a take-away game where each rack is a pile and any positive number can be removed from one pile per turn. Try small cases, like one rack or two racks of equal and unequal sizes, to see what decides who takes the last server.

Hint 2

Combine all the rack sizes with XOR. Ben wins exactly when that value is zero. When it is not zero, a winning first move must leave a total of zero, and a rack can be reduced to that value only if it has one particular bit set, the highest bit of the total.

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

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