Ace in the Hole (Small)

Reconstruct the lexicographically greatest 321-avoiding permutation consistent with the given optimal worst-case search order for value 1.

Hard8Game theoryBrute forceCombinatoricsNo attempts yetTime limit30sMemory limit512 MB

Problem

Amy builds a deck of NN cards, each carrying one value from 11 to NN. She arranges the deck so that the values contain no decreasing subsequence of length 33. For example, 1,5,4,6,3,21, 5, 4, 6, 3, 2 is not allowed because 5,3,25, 3, 2 decreases.

Amy hands the deck to Ben. Ben knows the deck has no decreasing subsequence of length 33, but he does not know the arrangement. Ben wants to find the card with value 11. He picks a card, turns it over to read its value, and repeats until he finds value 11. At every step Ben picks a card that minimizes the worst-case number of cards he still has to examine.

Ben later says he was unlucky and examined all NN cards before he found value 11. Given the order in which Ben examined the cards, find the value of each card. If several decks are possible, choose the lexicographically greatest one.

Deck AA is lexicographically greater than deck BB when, at the first position where the two differ, the card in AA has the greater value.

Take N=3N = 3 with Ben examining positions in the order 2,1,32, 1, 3, where positions are counted from 11. The values must have been 2,3,12, 3, 1. If card 22 held value 11, Ben would have stopped at once. If card 22 held value 22, Ben would have known that card 11 holds value 11, because the arrangement (3,2,1)(3, 2, 1) contains a decreasing subsequence of length 33 and is impossible. Neither case needs a third examination. Card 22 therefore held value 33. For the same reason card 11 did not hold value 11. The values are 2,3,12, 3, 1.

Input

The first line contains the number of test cases TT. Each test case begins with a line containing one integer NN, the number of cards in the deck. The next line contains NN integers separated by single spaces describing the order in which Ben examined the deck. The ii-th integer is the position of the card Ben examined ii-th, with positions counted from 11.

Output

For each test case print one line in the form Case #x: y, where xx is the test case number starting from 11 and yy is the values of the cards in position order, separated by single spaces.

Constraints

  • 1T1001 \le T \le 100
  • 1N81 \le N \le 8
  • For the given examination order, at least one deck satisfies every condition in the problem, including the condition that Ben had to examine all NN cards.