#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n;
    cin >> n;
    int k, l, r;
    vector<tuple<int, int, int, int> > vl;
    vector<tuple<int, int, int, int> > vr;
    int nvl = 0;
    int nvr = 0;
    for (int j = 1; j <= n; j++) {
      cin >> k >> l >> r;
      if (l >= r) {
        vl.push_back(make_tuple(k, r - l, l, r));
        nvl++;
      } else {
        vr.push_back(make_tuple(k, r - l, l, r));
        nvr++;
      }
    }
    sort(vl.begin(), vl.end());
    sort(vr.begin(), vr.end());
    long long ans = 0L;
    int undone = 0;
    for (int j = 1; j <= nvl; j++) {
      if (get<0>(vl[j - 1]) >= j - undone) {
        ans += get<2>(vl[j - 1]);
      } else {
        ans += get<3>(vl[j - 1]);
        undone++;
      }
    }
    undone = 0;
    for (int j = nvr; j > 0; j--) {
      if (get<0>(vr[j - 1]) < j + undone + nvl) {
        ans += get<3>(vr[j - 1]);
      } else {
        ans += get<2>(vr[j - 1]);
        undone++;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
