This page is still under construction.

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

Wi-Fi Network

Time limit1sMemory limit128 MB

Summary
Decide whether a point in an open square sees all two or three computers through straight segments that cross none of up to 100 walls.
Level

Hard8 of 10

Topics
Geometry, Brute force
Solved
No attempts yet

Problem

Hektor came up with an idea to make himself rich. He decided to write a program that solves the well-known problem of placing a Wi-Fi router so that every computer in an apartment can reach it, meaning the shortest route connecting the router to a computer does not pass through any wall.

The first version of the program is heavily simplified: it handles only apartments with at most three computers, and the router has unlimited range. Because the range is unlimited, the shortest route from the router to a computer is simply the straight segment between them, so a computer is reachable exactly when that straight segment does not cross any wall.

Can you write such a program? For each test set decide whether at least one spot for the router exists from which every computer is reachable.

Input

The first line contains the number of test sets ZZ (1≤Z≤101 \le Z \le 10). The test sets follow.

The first line of each set contains three integers RR, NN, MM (2≤R≤100002 \le R \le 10000, 2≤N≤32 \le N \le 3, 1≤M≤1001 \le M \le 100). The area searched for the router is limited to points (X,Y)(X, Y) with −R<X<R-R < X < R and −R<Y<R-R < Y < R. Here NN is the number of computers and MM is the number of walls in the area.

The next NN lines each contain two integers XX, YY (−R<X<R-R < X < R, −R<Y<R-R < Y < R), the position of a computer.

The next MM lines each contain four integers x0x_0, y0y_0, x1x_1, y1y_1 (each strictly between −R-R and RR), describing the segment from (x0,y0)(x_0, y_0) to (x1,y1)(x_1, y_1) that represents one wall.

You may assume that no two walls cross, although they may share endpoints. No computer lies on a wall.

Output

For each test set print TAK if the router can be placed so that its signal reaches every computer without passing through a wall, and NIE otherwise. Print each answer on its own line.

Examples5

  1. Example 1

    Input
    2
    5 2 3
    1 0
    -1 0
    0 1 0 -1
    -1 1 1 1
    -1 -1 1 -1
    5 2 1
    1 0
    -1 0
    0 -4 0 4
    
    Expected output
    NIE
    TAK
    
  2. Example 2

    Input
    1
    10 2 1
    2 2
    4 4
    -9 -9 -9 9
    
    Expected output
    TAK
    
  3. Example 3

    Input
    1
    10 2 1
    3 0
    -3 0
    0 -2 0 2
    
    Expected output
    TAK
    
  4. Example 4

    Input
    1
    10 3 1
    0 0
    4 0
    0 4
    -9 -9 -8 -9
    
    Expected output
    TAK
    
  5. Example 5

    Input
    1
    10 3 1
    3 0
    -3 0
    0 3
    0 -2 0 2
    
    Expected output
    TAK