#include <bits/stdc++.h>
const long long INF = 1000000001;
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long t1;
  cin >> t1;
  for (long long q11 = 0; q11 < t1; q11++) {
    long long n;
    cin >> n;
    map<pair<long long, pair<long long, long long>>, pair<long long, long long>>
        a;
    for (long long q = 0; q < n; q++) {
      pair<long long, pair<long long, long long>> w;
      cin >> w.first >> w.second.first >> w.second.second;
      a[w] = {-1, q};
    }
    set<pair<pair<long long, long long>, pair<long long, long long>>> s;
    for (auto q : a) {
      s.insert({{q.first.second.first - q.first.second.second, q.first.first},
                q.first.second});
    }
    long long q1 = 0;
    for (auto q : s) {
      a[{q.first.second, q.second}].first = q1++;
    }
    set<pair<pair<long long, long long>, pair<long long, long long>>> d;
    for (auto q : a) {
      if (q.second.first < q.second.second) {
        d.insert({{q.second.second + n - q.first.first, q.first.first},
                  q.first.second});
      } else {
        d.insert(
            {{q.second.second + q.first.first, q.first.first}, q.first.second});
      }
    }
    long long answer = 0;
    q1 = 0;
    for (auto q : d) {
      answer += (q.first.second > q1 ? q.second.first : q.second.second);
      q1++;
    }
    cout << answer << '\n';
  }
  return 0;
}
