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
#include <iostream>
#include <vector>
#include <algorithm>

constexpr double eps = 1e-6;

bool eps_eq(double lhs, double rhs) {
  return lhs + eps > rhs && rhs + eps > lhs;
}

bool eps_gt(double lhs, double rhs) {
  return lhs > rhs + eps;
}

bool eps_lt(double lhs, double rhs) {
  return lhs + eps < rhs;
}

bool eps_zero(double num) {
  return num > -eps && num < eps;
}

struct Tea {
  Tea(int vol, int temp) : vol_(vol), temp_(temp) {}

  double vol_; // volume
  double temp_; // temperature

  bool operator<(const Tea& rhs) {
    return eps_lt(this->temp_, rhs.temp_);
  }
};

bool can_distribute_tea() { // TODO: Solve for n == 1
  int n;
  std::cin >> n;

  std::vector<Tea> current, target;

  for (int i = 0; i < n; i++) {
    int vol, current_temp, target_temp;
    std::cin >> vol >> current_temp >> target_temp;
    current.emplace_back(vol, current_temp);
    target.emplace_back(vol, target_temp);
  }

  std::sort(current.begin(), current.end());
  std::sort(target.begin(), target.end());

  auto coldest = current.begin();
  auto second_coldest = coldest + 1;

  for (const Tea& tea : target) {
    double vol = tea.vol_;
    double temp = tea.temp_;

    if (eps_gt(coldest->temp_, temp)) {
      return false; // Coldest tea is too hot
    }

    while (true) {
      while (second_coldest != current.end()
          && eps_eq(coldest->temp_, second_coldest->temp_)) {
        second_coldest->vol_ += coldest->vol_;
        coldest++;
        second_coldest++;
      }

      if (coldest + 1 == current.end()) {
        if (!eps_eq(temp, coldest->temp_)) {
          return false;
        }
        if (eps_gt(vol, coldest->vol_)) exit(1); // Shouldn't happen
      }

      if (eps_eq(coldest->temp_, temp)) {
        if (eps_lt(coldest->vol_, vol)) {
          return false; // We run out of cold tea
        }

        coldest->vol_ -= vol;

        if (eps_zero(coldest->vol_)) {
          coldest++;
          second_coldest++;
        }

        break;
      }

      // Tea mixing
      if (eps_lt(second_coldest->temp_, temp)
          || eps_eq(second_coldest->temp_, temp)) { // Hotter still too cold
        second_coldest->temp_ = (coldest->vol_ * coldest->temp_
            + second_coldest->vol_ * second_coldest->temp_)
            / (coldest->vol_ + second_coldest->vol_);
        second_coldest->vol_ += coldest->vol_;
        coldest++;
        second_coldest++;
      } else {
        double hotter_vol = coldest->vol_ * (temp - coldest->temp_)
            / (second_coldest->temp_ - temp);
        hotter_vol = std::min(second_coldest->vol_, hotter_vol);

        coldest->temp_ = (coldest->vol_ * coldest->temp_
            + hotter_vol * second_coldest->temp_)
            / (coldest->vol_ + hotter_vol);
        coldest->vol_ += hotter_vol;
        second_coldest->vol_ -= hotter_vol;

        if (eps_zero(second_coldest->vol_)) {
          second_coldest->vol_ = coldest->vol_;
          second_coldest->temp_ = coldest->temp_;
          coldest++;
          second_coldest++;
        }
      }
    }
  }

  return true;
}

int main() {
  std::ios_base::sync_with_stdio(false);

  int cases;
  std::cin >> cases;

  while (cases--) {
    std::cout << (can_distribute_tea() ? "TAK" : "NIE") << std::endl;
  }

  return 0;
}