#include <bits/stdc++.h>
using namespace std;
int k[200001], l[200001], r[200001];
long long sum(multiset<int> S) {
  long long s = 0;
  for (int i = 0; i < S.size(); i++) s += i;
  return s;
}
int solve(int v[], int n) {
  multiset<int> s;
  for (int i = 1; i <= n; i++) {
    int j = 0;
    while (j < s.size() && k[j + 1] == i) s.insert(v[j]);
    while (s.size() > i) s.erase(s.begin());
  }
  return sum(s);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    long long sum = 0;
    int n, red = 0, blue = 0;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      int li, ri;
      cin >> k[i] >> li >> ri;
      sum += min(li, ri);
      int y = k[i];
      if (l < r) {
        r[n - y] = ri - li;
        blue++;
      } else {
        l[y] = li - ri;
        red++;
      }
    }
    sum += solve(l, n);
    sum += solve(r, n);
    cout << sum << "\n";
  }
  return 0;
}
