Broken Gearbox

Assign each of n gear radii to a vertex of a connected graph so that every edge's distance equals the sum of its assigned radii, returning the lexicographically smallest assignment or impossible.

Hard8GraphMathDFSGreedyNo attempts yetTime limit2sMemory limit512 MB

Problem

The Mechanical Turk was an 18th-century fake robot that created the illusion of artificial intelligence by playing chess. It inspired us to build our own fake robot, one that creates the illusion of real intelligence by solving programming contest problems.

To make the machine look more convincing, we mounted gears on axles inside an uncovered panel. The gears are purely decorative, so we placed them to form an impressive meshing pattern without any regard for gear ratios or turning direction. It is entirely possible that none of the gears can actually turn.

It is guaranteed that every axle was connected to every other axle by meshing, either directly or indirectly. Two axles aa and bb have directly meshing gears when their distance equals the sum of the radii of their gears, that is, dab=rga+rgbd_{ab} = r_{g_a} + r_{g_b}.

Sadly, the gears fell off the machine. We believe we collected all of them, but we no longer know which gear belongs on which axle. Find a way to put the gears back on the axles so that they mesh the way they did originally.

Input

The input consists of:

  • One line with an integer nn (2n1052 \le n \le 10^5), the number of gears and axles.
  • One line with nn integers r1,,rnr_1, \ldots, r_n (1ri1081 \le r_i \le 10^8), the radius of each gear.
  • One line with an integer mm (n1m105n - 1 \le m \le 10^5), the number of pairs of axles to mesh.
  • mm lines, each with three integers aia_i, bib_i, did_i (1ai<bin1 \le a_i < b_i \le n, 1di1081 \le d_i \le 10^8): the indices of two axles that were directly meshed, and the distance between them.

Output

If the machine can be fixed with the given gears, output nn integers g1,,gng_1, \ldots, g_n on one line, separated by spaces, where gig_i is the 1-based index of the gear to put on the ii-th axle. The sequence g1,,gng_1, \ldots, g_n must be a permutation of 11 to nn. If several valid placements exist, output the lexicographically smallest sequence g1,,gng_1, \ldots, g_n: at the first position where two sequences differ, the one with the smaller gear index comes first. Otherwise, output impossible.