Train of Threes

No attempts yetTime limit5sMemory limit256 MB

Problem

Threes! is a puzzle game available on iOS, Android, and other platforms. The rules are simple, but the structure behind them is deep: you repeatedly match adjacent numbers to build larger numbers. Here you play a simplified version of the game.

The game starts with an array of NN numbers. Each number is 11, or 22, or of the form 3×2i3 \times 2^i for some integer i0i \ge 0.

You can combine two matching adjacent numbers into a larger number. A 11 next to a 22 combines into a single 33. Two adjacent 33s combine into a single 66, two adjacent 66s combine into a single 1212, and in general two identical adjacent numbers that are at least 33 combine into a single number equal to their sum. The numbers 11 and 22 are special: a 11 matches only a 22, and a 22 matches only a 11.

The goal of the game is to form the largest number possible. The game ends when no two adjacent numbers can be combined. For example, starting from the array {12,24,6,1,2,3,12,3,3,6,1,2,2,1,24}\{12, 24, 6, 1, 2, 3, 12, 3, 3, 6, 1, 2, 2, 1, 24\}, the largest number you can form is 4848.

Input

The first line contains an integer TT, the number of test cases (1T10001 \le T \le 1000). Then follow 2T2T lines. The first line of each test case contains an integer NN, the size of the array (1N100001 \le N \le 10000). The next line contains NN integers, where each integer is 11, 22, or of the form 3×2i3 \times 2^i for some integer 0i110 \le i \le 11.

Output

For each of the TT test cases, print the largest number that can be formed on its own line.