1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fi first
#define se second
#define pii pair <int, int>
#define pli pair <ll, int>
#define pll pair <ll, ll>
#define ppii pair <pii, pii>
#define ppli pair <pli, pli>
#define ppll pair <pli, pll>
using namespace std;

const ll MIN_INF = -(1ll<<61);

template <typename T>
ostream & operator << (ostream &out, const vector <T> &V) {
	if (!V.empty()) {
		out << "{";
		for (auto v : V)
			out << v << ", ";
		out << "\b\b}";			// \b is backspace
	}
	else {
		out << "{}";
	}
	return out;
}

template <class T1, class T2>
ostream & operator << (ostream &out, const pair <T1, T2> &P) {
	out << "{" << P.first << ", " << P.second << "}";
	return out;
}

struct cup {
    ld l, a, b;
};

struct cup1 {
    ld a, b, l;
    cup1(ld a, ld b, ld l) : a(a), b(b), l(l) { }
    cup1() { }
    friend bool operator < (const cup1 &c1, const cup1 &c2) {
        return c1.a > c2.a || (c1.a == c2.a && c1.b > c2.b);
    }
};

struct cup2 {
    ld a, b, l;
    cup2(ld a, ld b, ld l) : a(a), b(b), l(l) { }
    cup2() { }
    friend bool operator < (const cup2 &c1, const cup2 &c2) {
        return c1.b > c2.b;
    }
};

void testcase_her() {
    int n;
    cin >> n;
    vector <cup> Cup(n); multiset <cup1> S1; multiset <cup2> S2;
    for (int i = 0; i < n; i++) {
        ld l, a, b;
        cin >> l >> a >> b;
        Cup[i] = {l, a, b};
        if (a > b) {
            S1.insert(cup1(a, b, l));
        }
        else if (a < b) {
            S2.insert(cup2(a, b, l));
        }
    }
    ld eps = 1e-12;
    cout.precision(15);
    while (!S1.empty() && !S2.empty()) {
        auto c1 = *S1.begin();
        auto c2 = *S2.begin();
        S1.erase(S1.begin());
        S2.erase(S2.begin());
        ld a1 = c1.a, b1 = c1.b, l1 = c1.l, a2 = c2.a, b2 = c2.b, l2 = c2.l;
        ld tmp = (a1 * l1 + a2 * l2) / (l1 + l2);
        //cout << a1 << " " << b1 << " " << l1 << " | " << a2 << " " << b2 << " " << l2 << " | " << tmp <<endl;
        if ((b1 >= b2 && b2 <= tmp) || (b1 < b2 && b2 <= tmp && a1 - b2 > 0)) {
            ld k1 = l2 * (b2 - a2) / (a1 - b2);
            if (abs(k1) > eps) {
                if (b2 < b1 - eps) {
                    S2.insert(cup2(b2, b1, k1));
                }
                else if (b2 > b1 + eps) {
                    S1.insert(cup1(b2, b1, k1));
                }
                //cout << "PUSJ " << b2 << " " << b1 << " " << k1 <<endl;
            }
            if (abs(l1 - k1) > eps) {
                if (a1 > b1 + eps) {
                    S1.insert(cup1(a1, b1, l1 - k1));
                }
                else if (a1 < b1 - eps) {
                    S2.insert(cup2(a1, b1, l1 - k1));
                }
            }
            //cout << " Case 2:  " << k1 <<endl;
        }
        else if ((b1 >= b2 && b1 >= tmp) || (b1 < b2 && b1 >= tmp && b1 - a2 > 0)) {
            ld k2 = l1 * (a1 - b1) / (b1 - a2);
            if (abs(k2) > eps) {
                if (b1 > b2 + eps) {
                    S1.insert(cup1(b1, b2, k2));
                }
                else if (b1 < b2 - eps) {
                    S2.insert(cup2(b1, b2, k2));
                }
            }
            if (abs(l2 - k2) > eps) {
                if (a2 < b2 - eps) {
                    S2.insert(cup2(a2, b2, l2 - k2));
                }
                else if (a2 > b2 + eps) {
                    S1.insert(cup1(a2, b2, l2 - k2));
                }
            }
            //cout << " Case 1:  " << k2 <<endl;
        }
        else {
            if (a1 < b2) {
                cout << "NIE" <<endl;
                return;
            }
            if (l1 >= l2) {
                ld k2 = l1 * (a1 - b2) / (b2 - a2);
                if (abs(l1 + k2 - l2) > eps) {
                    S1.insert(cup1(b2, b1, l1 + k2 - l2));
                }
                if (abs(l2 - k2) > eps) {
                    if (a2 > b1 + eps) {
                        S1.insert(cup1(a2, b1, l2 - k2));
                    }
                    else if (a2 < b1 - eps) {
                        S2.insert(cup2(a2, b1, l2 - k2));
                    }
                }
            }
            else {
                if (b1 > a2) {
                    ld k1 = l2 * (b1 - a2) / (a1 - b1);
                    if (abs(l2 + k1 - l1) > eps) {
                        S1.insert(cup1(b1, b2, l2 + k1 - l1));
                    }
                    if (abs(l1 - k1) > eps) {
                        if (a1 > b2 + eps) {
                            S1.insert(cup1(a1, b2, l1 - k1));
                        }
                        else if (a1 < b2 - eps) {
                            S2.insert(cup2(a1, b2, l1 - k1));
                        }
                    }
                }
                else {
                    ld k2 = l1 * (a1 - b2) / (b2 - a2);
                    if (l2 >= l1 + k2) {        // jedynka znika
                        l2 -= (l1 + k2);
                        if (abs(l2) > eps) {
                            if (a2 > b2 + eps) {
                                S1.insert(cup1(a2, b2, l2));
                            }
                            else if (a2 < b2 - eps) {
                                S2.insert(cup2(a2, b2, l2));
                            }
                        }
                        if (abs(l1) > eps) {
                            if (a2 > b1 + eps) {
                                S1.insert(cup1(a2, b1, l1));
                            }
                            else if (a2 < b1 - eps) {
                                S2.insert(cup2(a2, b1, l1));
                            }
                        }
                    }
                    else {                  // dwójka znika
                        if (abs(l1 + k2 - l2) > eps) {
                            if (b2 > b1 + eps) {
                                S1.insert(cup1(b2, b1, l1 + k2 - l2));
                            }
                            else if (b2 < b1 - eps) {
                                S2.insert(cup2(b2, b1, l1 + k2 - l2));
                            }
                        }
                        if (abs(l2 - k2) > eps) {
                            if (a2 > b1 + eps) {
                                S1.insert(cup1(a2, b1, l2 - k2));
                            }
                            else if (a2 < b1 - eps) {
                                S2.insert(cup2(a2, b1, l2 - k2));
                            }
                        }
                    }
                }
            }
        }
    }
    cout << ((S1.empty() && S2.empty()) ? "TAK" : "NIE") <<endl;
}

int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int T;
    cin >> T;
    for (int t = 0; t < T; t++) {
        testcase_her();
    }
    return 0;
}