This page is still under construction.

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

Tea

Time limit2sMemory limit256 MB

Summary
Given n cups with amounts and initial temperatures, decide whether repeated splitting and mixing can produce the required amounts and target temperatures.
Level

Medium6 of 10

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

Problem

Bytemommy whole-heartedly loves her Bytekids. However she is kinda forgetful, so instead of giving them proper names, she numbered them with consecutive integers from 11 to nn. Every day she prepares a tea for each of her Bytekids in their favourite cups. One peculiar property of all tea cups in their home is that they have infinite capacity, even though they take finite space only. However, this is for our simplicity only. Bytekid number ii prefers to drink exactly l_il\_i bitres of tea every day. However, the amount of tea is not their only requirement. Its temperature has to be properly adjusted as well. Bytekid number ii would like its tea to have exactly b_ib\_i Bytesius degrees.

Unfortunately, one day scatterbrained Bytemommy messed up the tea temperatures and the temperature of the tea in the ii-th cup was exactly a_ia\_i Bytesius degrees, instead of b_ib\_i (however the ii-th kid still got l_il\_i bitres in its cup). Nothing is lost yet. Bytekids are very clever and, using some auxiliary cups, started to mix up their teas trying to get cups with appropriate amounts and temperatures of teas. You need to determine whether it is possible for Bytekids to reach their goal, that is to get nn teas so that the ii-th tea has exactly l_il\_i bitres and b_ib\_i Bytesius degrees.

Formally, Bytekids are allowed to perform the following steps arbitrarily many times:

  • Partitioning the tea. Given a cup with aa bitres of tea with temperature tt, create two cups of tea with xx and a−xa-x bitres of tea with temperature tt for some arbitrary real value of xx such that 0<x<a0<x<a (the initial cup of tea will no longer exist, obviously).
  • Mixing the tea. Given two cups of tea with aa and bb bitres of tea with temperatures t_at\_a and t_bt\_b, respectively, create one cup of tea with a+ba+b bitres of tea with temperature a⋅t_a+b⋅t_ba+b,\frac{a \cdot t\_a + b \cdot t\_b}{a + b}, that is, the weighted mean of the initial temperatures (again, the initial two cups of tea will no longer exist).

Input

The first line of input contains one integer tt (1≤t≤100 0001 \le t \le 100\,000) denoting the number of testcases.

The description of each testcase starts with a line containing one integer nn (1≤n≤100 0001 \le n \le 100\,000) denoting the number of Bytekids. The following nn lines describe the Bytekids: the ii-th of them contains three integers l_il\_i, a_ia\_i and b_ib\_i (1≤l_i,a_i,b_i≤1 000 0001 \le l\_i, a\_i, b\_i \le 1\,000\,000) denoting the amount of tea in the ii-th cup in bitres (both the initial and the required final one) and the initial and required temperature of that tea, respectively.

The sum of the values of nn over all testcases will not exceed 1 000 0001\,000\,000.

Output

You need to print tt lines. The ii-th of them should contain the word TAK if it is possible for Bytekids to reach their goal in the ii-th testcase, or NIE otherwise.

Hint

Denote cups of tea as a pair of numbers. The pair (l,t)(l, t) denotes a cup with ll bitres of tea with temperature tt Bytesius degrees.

In the first testcase Bytekids have cups (2,1)(2, 1) and (2,5)(2, 5). Using the operation of partitioning the tea they can get cups (12,1)(\frac12, 1), (32,1)(\frac32, 1), (12,5)(\frac12, 5) and (32,5)(\frac32, 5).

Then, by mixing cups (12,1)(\frac12, 1) and (32,5)(\frac32, 5), they get 12+32=2\tfrac12 + \tfrac32 = 2 bitres with temperature

12⋅1+32⋅512+32=4,\frac{\frac12 \cdot 1 + \frac32 \cdot 5}{\frac12 + \frac32} = 4,

that is, the cup (2,4)(2,4). Similarly, by mixing the cup (32,1)(\frac32, 1) with (12,5)(\frac12, 5), they get (2,2)(2, 2). In the end, Bytekids will have two cups with appropriate amounts and temperatures of tea.

In the second testcase both teas are too hot. We can't do much here.

However, in the third testcase it is sufficient for Bytekids to swap their cups.

Examples1

  1. Example 1

    Input
    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
    
    Expected output
    TAK
    NIE
    TAK
    NIE
    TAK