This page is still under construction.

Parts of this page are still being built. What you see may change.

Why Do They Sing?

Time limit1sMemory limit128 MB

Summary
Decide whether a path from the bottom edge to the top edge of a rectangle avoids every given singing circle.
Level

Medium6 of 10

Topics
Union-find, Geometry
Solved
No attempts yet

Problem

Did you know that among Hektor's many strengths is a perfect musical ear? Sometimes, though, it becomes a real curse, because it makes listening to people who sing out of tune an especially unpleasant experience.

Today Hektor's school is holding auditions for the shows Why Do They Sing? and Stars Sing Under the Ice. The yard in front of the school has filled with contestants waiting their turn, each warming up by singing mercilessly off-key.

Is it possible to cross the yard without hearing a single one of the off-key contestants?

The yard is a rectangle of height N and width M. On it we mark (N+1) × (M+1) points with integer coordinates, with point (0, 0) in the top-left corner and point (N, M) in the bottom-right corner. So the first coordinate runs vertically from the top (0) down to the bottom (N), and the second runs horizontally from the left (0) to the right (M).

K off-key people stand in the yard. Each is described by the coordinates of the point where they stand and by the range of their singing, that is, the radius of a circle centered at that point inside which the person can be heard.

The off-key people stand at points with integer coordinates, but Hektor (it is his school and his yard, after all!) may move along any path with real coordinates that does not leave the yard.

Determine whether there exists a real-coordinate path connecting some point on the bottom edge of the yard (that is, a point (N, a) for any real a between 0 and M) with some point on the top edge (that is, a point (0, b) for any real b between 0 and M), such that the path stays inside the yard and does not pass through the singing range (circle) of any of the singers.

Input

The first line contains a natural number Z (1 ≤ Z ≤ 10), the number of test sets. The test sets follow, one after another.

The first line of each test set contains three space-separated integers N, M, K (1 ≤ N, M ≤ 1000, 1 ≤ K ≤ 1000). The next K lines each describe one off-key person: three space-separated integers w, c, r (0 ≤ w ≤ N, 0 ≤ c ≤ M, 1 ≤ r ≤ 1000), where (w, c) are the person's coordinates and r is their singing range (radius).

Output

For each test set, print TAK on its own line if such a path exists, and NIE otherwise. (Here TAK means "yes" and NIE means "no".)

Hint

Examples1

  1. Example 1

    Input
    2
    3 4 2
    2 1 1
    2 3 1
    3 4 2
    2 1 1
    1 3 1
    
    Expected output
    NIE
    TAK