Under construction

Still being built. Things may change

Chochlik

Interview

Time limit1sMemory limit512 MB

Summary
Decide for each department whether wheels linked by same-direction and opposite-direction belts can all spin without contradiction.
Level

Medium4 of 10

Topics
Union-find, Graph, BFS
Solved
No attempts yet

Problem

In a nineteenth-century factory, power was transmitted through drive belts. The factory ran smoothly until one day a mischievous imp slipped inside and swapped the arrangement of some of the belts. The owner now wants to know whether, with the belts in their current arrangement, the factory can still work correctly.

There are two ways to connect two distinct wheels. If a wheel spins in some direction, a wheel joined to it by a type-AA connection spins in the same direction, while a wheel joined by a type-BB connection spins in the opposite direction.

The two ways of connecting wheels with drive belts.

A department of the factory can work correctly if setting any single wheel spinning never forces some other wheel to turn in two opposite directions at the same time.

Your task is to write a program that, for each department, decides whether it can work correctly with the belts arranged as they are now.

Input

The first line of standard input contains one integer dd (1≤d≤101 \le d \le 10), the number of departments in the factory.

The descriptions of the departments follow. The first line of each description contains two integers kk and pp (1≤k≤100 0001 \le k \le 100\,000, 0≤p≤100 0000 \le p \le 100\,000): the number of wheels in the department and the number of connections between them. Each of the next pp lines contains two integers aia_i and bib_i and one letter cic_i, separated by single spaces (1≤ai,bi≤k1 \le a_i, b_i \le k, ci∈{A,B}c_i \in \{A, B\}, ai≠bia_i \ne b_i), giving the numbers of the two connected wheels and the type of connection between them.

Two wheels may be joined by more than one belt.

Output

Print exactly dd lines to standard output. The ii-th line should contain one word:

  • TAK if the ii-th department can work correctly
  • NIE otherwise

Note

Explanation of the sample. The first department cannot work correctly: spinning wheel 11 clockwise would force wheel 22 to turn clockwise and counterclockwise at the same time, which is not allowed. The second department has no belts at all, so it is a valid configuration.

Examples1

  1. Example 1

    Input
    2
    4 4
    1 2 B
    2 3 A
    3 4 A
    4 1 A
    2 0
    
    Expected output
    NIE
    TAK