Square Cutting

Given only the counts of squares cut per turn, recover the smallest possible long side L of the original rectangle.

Medium7MathNumber theoryGreedyNo attempts yetTime limit2sMemory limit512 MB

Problem

Two players take turns cutting one rectangular sheet of paper. The long side of the sheet has length LL and the short side has length WW.

On a turn, a player cuts the largest square that fits in the rectangle that is left. In a rectangle whose short side is WW, that square has side WW. The player cuts one or more squares of that size, every square cut in the turn has the same size, and what remains after the cut must be a single rectangle. The player who cuts the paper with nothing left over wins.

A game is recorded as L W r a1 a2  arL\ W\ r\ a_1\ a_2\ \dots\ a_r, where rr is the number of turns and aia_i is the number of squares cut on turn ii. For example, on a sheet with long side 5 and short side 2, the first player cuts two squares of side 2 and leaves a 2×12 \times 1 rectangle, then the second player cuts two squares of side 1 and the game ends. The record of that game is 5 2 2 2 2.

A storage fault erased the first two numbers, LL and WW, from every record. Recover LL from the surviving rr and a1a_1 through ara_r. When several values of LL are possible, answer with the smallest one.

Input

The first line contains the number of records MM.

Each of the next MM lines holds one record. The line starts with the number of turns rr, followed by rr integers a1a_1 through ara_r separated by spaces.

  • 1M201 \le M \le 20
  • 1r501 \le r \le 50
  • 1ai1001 \le a_i \le 100
  • Every record comes from a game that can actually be played, so ar2a_r \ge 2 whenever r2r \ge 2.
  • The original sheet satisfies 1WL101001 \le W \le L \le 10^{100}.

Output

Print MM lines. On line ii, print the smallest long side LL among the rectangles that produce record ii.