Chochlik
InterviewTime limit1sMemory limit512 MB
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- connection spins in the same direction, while a wheel joined by a type- 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 (), the number of departments in the factory.
The descriptions of the departments follow. The first line of each description contains two integers and (, ): the number of wheels in the department and the number of connections between them. Each of the next lines contains two integers and and one letter , separated by single spaces (, , ), 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 lines to standard output. The -th line should contain one word:
TAKif the -th department can work correctlyNIEotherwise
Note

Explanation of the sample. The first department cannot work correctly: spinning wheel clockwise would force wheel 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.