#P5980. [PA 2019] Herbata

[PA 2019] Herbata

Description

You have infinitely many cups with unlimited capacity and nn cups of water. The ii-th cup has volume lil_i and temperature aia_i.

You can perform infinitely many operations. Each operation is one of the following:

  1. Choose one cup of water. Suppose its volume is VV and its temperature is TT. You may pour it into several empty cups so that the temperature of water in each cup is still TT, and the sum of their volumes equals VV. Note that the volumes can be any non-negative real numbers.

  2. Choose two cups of water. Suppose one has volume VaV_a and temperature TaT_a, and the other has volume VbV_b and temperature TbT_b. You may mix them into one cup of water with volume Va+VbV_a+V_b and temperature Va×Ta+Vb×TbVa+Vb\dfrac{V_a\times T_a+V_b\times T_b}{V_a+V_b}.

Your goal is to perform some operations so that after all operations, for every i(1in)i(1\le i\le n), the ii-th cup has volume lil_i and temperature bib_i. Write a program to determine whether a solution exists.

Input Format

The first line contains a positive integer TT, denoting the number of testdata.

For each testdata, the first line contains a positive integer nn.

The next nn lines each contain three positive integers li,ai,bil_i,a_i,b_i.

Output Format

For each testdata, output one line. If a solution exists, output TAK; otherwise, output NIE.

5
2
2 1 4
2 5 2
2
1 4 3
1 5 4
2
1 5 7
1 7 5
2
1 4 1
1 2 5
3
2 6 4
1 2 3
3 4 5
TAK
NIE
TAK
NIE
TAK

Hint

For 100%100\% of the testdata, 1T1051\le T\le 10^5, 1n1051\le n\le 10^5, 1li,ai,bi1061\le l_i,a_i,b_i\le 10^6. The input guarantees that the sum of all nn does not exceed 10610^6 across all testdata.

Translated by ChatGPT 5