Exotic Mechanical System

No attempts yetTime limit1sMemory limit128 MB

Problem

An underappreciated inventor has built an exotic mechanical system made of gears of assorted diameters. Every gear has a white side and a black side. The gears in the system can be joined in four ways:

  • Joined by a shaft: gears xx and yy mounted on the same shaft turn in the same direction with the same angular velocity (angular velocity is the angle swept divided by the elapsed time). This can be done in two ways:

    • (Way A) The sides that face each other have the same colour. It makes no difference whether both facing sides are white or both are black.
    • (Way a) The sides that face each other have different colours. It makes no difference whether the black side of xx meets the white side of yy or the other way round.
  • Joined by teeth: gears xx and yy meshed by their teeth turn in opposite directions, and the edge (tooth) speed of the two gears is equal in magnitude. This can also be done in two ways:

    • (Way B) Crossing from gear xx to gear yy through the contact point, the colour stays the same.
    • (Way b) Crossing from gear xx to gear yy through the contact point, the colour changes. It makes no difference whether it changes from white to black or from black to white.

Examples of the different kinds of joints are shown in the figure.

Assume the given system can always be built. Inventors have their tricks — for instance, meshing one gear of diameter 11 with hundreds of other gears of diameter 11. When it comes to building such systems, nothing is impossible for them.

The inventor has not yet started his system. He is afraid that when he tries to set it in motion it will not work, and he is unsure whether the gears will spin at suitable speeds. So he hands you the schematic and asks for help. Initially every gear is at rest. Compute the angular velocities of the gears once a drive is attached to gear number 11, or determine that gear number 11 cannot rotate. The drive makes gear 11 spin at an angular velocity of 11 rotation per second, provided gear 11 is not blocked.

Oh — and I almost forgot! Thanks to Dobrosia for the figures.

Input

The first line of input contains the number of test cases dd (1d1001 \le d \le 100). The test cases follow.

Each test case describes one mechanical system. The first line of a test case contains the number of gears nn and the number of connections between them mm (1n10001 \le n \le 1000).

The next nn lines give the diameters of gears i=1ni = 1 \ldots n in order. The diameter sis_i of a gear is an integer between 11 and 100100.

The next mm lines describe the connections between gears. Each connection consists of two distinct gear numbers xx and yy (1x,yn1 \le x, y \le n) and a letter denoting the type of connection: a, b, A, or B. Any given pair of gears appears in the input at most once.

Output

For each test case, print on a separate line the angular velocities of gears 11 through nn in rotations per second; if gear number 11 cannot rotate, print the word NIE on that line instead. Round each speed to exactly two digits after the decimal point (rounding halves up), and separate the values on a line with single spaces.