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
//---===---
#include <stdio.h>
#include <unistd.h>

typedef unsigned int uint;

char buf_in[4096000];
int buf_in_head = 0;
int buf_in_tail = 0;

static int getChar(void) {
  while (buf_in_head == buf_in_tail) {
    int rv = read(0, buf_in, sizeof(buf_in));
    if (rv > 0) {
      buf_in_head = 0;
      buf_in_tail = rv;
      break;
    };
    if (rv == 0) return EOF;
  }
  return buf_in[buf_in_head++];
}

// only 1 call is safe, and only if previously getChar() had been called.
static void ungetChar(void) {
  --buf_in_head;
}

static uint getUInt() {
  uint v;
  int c;
  for (;;) {
    c = getChar();
    if (c < '0') continue;
    if (c > '9') continue;
    v = c - '0';
    break;
  };
  for (;;) {
    c = getChar();
    if (c < '0') break;
    if (c > '9') break;
    v *= 10;
    v += c - '0';
  };
  ungetChar();
  return v;
}
//---===---

void solve (void) {
  // 2 <= n <= 100,000
  uint n = getUInt();
  // 1 <= w1 <= w2 <= 1,000,000,000
  // 1 <= h1 <= h2 <= 1,000,000,000
  uint w1 = getUInt();
  uint w2 = getUInt();
  uint h1 = getUInt();
  uint h2 = getUInt();
  uint ok = 1;

  while (--n) {
    // 1 <= w3 <= w4 <= 1,000,000,000
    // 1 <= h3 <= h4 <= 1,000,000,000
    uint w3 = getUInt();
    uint w4 = getUInt();
    uint h3 = getUInt();
    uint h4 = getUInt();
    if (w3 < w1) { w1 = w3; ok = 0; };
    if (w4 > w2) { w2 = w4; ok = 0; };
    if (h3 < h1) { h1 = h3; ok = 0; };
    if (h4 > h2) { h2 = h4; ok = 0; };
    if (w3 == w1) if (w4 == w2) if (h3 == h1) if (h4 == h2) ok = 1;
  };

  printf(ok ? "TAK\n" : "NIE\n");
}

int main (void) {
  // 1 <= t <= 10
  uint t = getUInt();

  while (t--) solve();

  return 0;
}