Given each peak's apparent-highest peak ahead, assign integer heights matching all sightings and print the lexicographically smallest heights or Impossible.
Hard8GeometryBacktrackingMathNo attempts yetTime limit5sMemory limit512 MBYou are hiking along a mountain range. There is exactly one peak every kilometer and nothing in between, so the peaks are numbered 1 through N in the order you walk them. On every peak you lie down to rest, look ahead, and one of the peaks in front of you looks like the highest one.
The peak that looks highest is not always the highest one. A taller peak can hide behind a nearer and lower peak, and when you look downhill a faraway peak can look higher than a close one.
Peak B looks highest from peak A when all three of these hold: B is further down the road than A, every peak between A and B is strictly below the line through the tops of A and B, and every peak beyond B is on or below that line.

In the picture, peak 4 looks highest from peak 1 and from peak 3. From peak 2 you cannot see peak 4 at all, because peak 3 hides it, so peak 3 looks highest.
You do not know how high the peaks are, but you remember which peak looked highest from each of them. Assign heights that agree with that memory. You were lying down, so treat every sighting as taken from the ground level of the peak you were on.
The first line has the number of test cases T. Each test case takes two lines. The first line has N, the number of peaks. Your walk starts on peak 1 and ends on peak N. The second line has N−1 numbers x1,x2,…,xN−1, where xi is the index of the peak that looked highest from peak i. Peak N is the last one, so it records nothing.
For each test case print one line in the form Case #n: y1 y2 ... yN, where n is the test case number starting from 1 and yi is the height of peak i. Every height must be an integer between 0 and 109, inclusive.
Several assignments of heights can agree with the same memory, so print the lexicographically smallest one. A sequence y is lexicographically smaller than a sequence z when y holds the smaller value at the first position where the two differ.
If no assignment of heights agrees with the memory, print Case #n: Impossible instead.