#include <bits/stdc++.h>
using namespace std;
long long n;
long long ft[200001];
void upd(long long p, long long v) {
  while (p <= n) {
    ft[p] += v;
    p += p & -p;
  }
}
long long qry(long long p) {
  long long res = 0;
  while (p > 0) {
    res += ft[p];
    p -= p & -p;
  }
  return res;
}
long long solve(vector<pair<long long, long long> > arr) {
  sort(arr.begin(), arr.end());
  reverse(arr.begin(), arr.end());
  n = arr.size();
  for (int i = 0; i <= n; i++) {
    ft[i] = 0;
  }
  long long res = 0;
  for (int i = 0; i < n; i++) {
    arr[i].second = min(n, arr[i].second);
    if (arr[i].second == 0 || qry(arr[i].second) >= arr[i].second) {
      upd(n, 1);
    } else {
      upd(arr[i].second, 1);
      res += arr[i].first;
    }
  }
  return res;
}
long long runtc() {
  long long res = 0;
  cin >> n;
  vector<pair<long long, long long> > one, two;
  for (int i = 1; i <= n; i++) {
    long long k, l, r;
    cin >> k >> l >> r;
    if (l == r) {
      res += l;
    } else if (l > r) {
      res += r;
      one.push_back(make_pair(l - r, k));
    } else {
      res += l;
      if (k < n) two.push_back(make_pair(r - l, n - k));
    }
  }
  res += solve(one);
  res += solve(two);
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tcs;
  cin >> tcs;
  while (tcs--) {
    cout << runtc() << '\n';
  }
}
