This page is still under construction.

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

Byteland Worldbeat Publishers

Time limit5sMemory limit128 MB

Summary
Given a sparse matrix of pair efficiencies (some described by row-wise column ranges), decide whether all maximum-size matchings have the same total weight.
Level

Hard8 of 10

Topics
Graph, Greedy, Sorting, Math
Solved
No attempts yet

Problem

Byteasar is a manager at Byteland Worldbeat Publishers (BWP), which employs nn composers and mm lyricists. The artists work in pairs, each pair made of exactly one composer and one lyricist.

Byteasar knows every employee's skills, so he can estimate how productive any composer-lyricist pair would be. The efficiency of a pair is the number of songs it writes per week.

Byteasar wants to form min⁡(n,m)\min(n, m) disjoint pairs (every artist belongs to at most one pair) so that the total number of songs per week is as large as possible. Artists left without a partner do not work.

After studying the data, Byteasar suspects that the weekly total does not depend on how the pairs are chosen. Help him check this suspicion: decide whether every way of forming min⁡(n,m)\min(n, m) disjoint pairs yields the same total efficiency.

Input

The first line contains one integer tt (1≤t≤101 \le t \le 10), the number of test cases. The test cases follow.

Each test case begins with a line of three integers nn, mm, and kk (1≤n,m≤1000001 \le n, m \le 100000, 1≤k≤3000001 \le k \le 300000): the number of composers, the number of lyricists, and the number of description lines. Composers are numbered 11 to nn and lyricists 11 to mm.

Each of the next kk lines contains four integers aia_i, bib_i, cic_i, and pip_i (1≤ai≤n1 \le a_i \le n, 1≤bi≤ci≤m1 \le b_i \le c_i \le m, 1≤pi≤1091 \le p_i \le 10^9): composer aia_i together with any lyricist from bib_i to cic_i (inclusive) produces pip_i songs per week.

Each composer-lyricist pair is described at most once. A pair that is not listed has efficiency 00 songs per week, yet it may still be formed.

Output

For each test case print one line: TAK (Polish for "yes") if every arrangement of min⁡(n,m)\min(n, m) disjoint pairs gives the same total efficiency, or NIE (Polish for "no") otherwise.

Examples4

  1. Example 1

    Input
    2
    2 3 3
    1 1 3 3
    2 1 1 3
    2 2 3 3
    3 3 7
    1 1 1 5
    1 2 2 6
    2 1 1 5
    2 2 2 6
    3 1 1 8
    3 2 2 9
    3 3 3 10
    
    Expected output
    TAK
    NIE
    
  2. Example 2

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

    Input
    3
    3 2 6
    1 1 1 4
    1 2 2 7
    2 1 1 4
    2 2 2 7
    3 1 1 4
    3 2 2 7
    3 2 6
    1 1 1 4
    1 2 2 7
    2 1 1 4
    2 2 2 7
    3 1 1 9
    3 2 2 7
    2 1 2
    1 1 1 5
    2 1 1 5
    
    Expected output
    TAK
    NIE
    TAK
    
  4. Example 4

    Input
    3
    2 2 4
    1 1 1 4
    1 2 2 5
    2 1 1 5
    2 2 2 6
    2 2 1
    2 1 1 7
    3 3 9
    1 1 1 10
    1 2 2 20
    1 3 3 30
    2 1 1 11
    2 2 2 21
    2 3 3 31
    3 1 1 12
    3 2 2 22
    3 3 3 32
    
    Expected output
    TAK
    NIE
    TAK