#include <bits/stdc++.h>
using namespace std;
vector<int> k, l, r;
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    set<int> pos;
    priority_queue<pair<int, int> > fro, bac;
    for (int i = 0; i < (n); ++i) pos.insert(i + 1);
    k.resize(n);
    l.resize(n);
    r.resize(n);
    for (int i = 0; i < (n); ++i) cin >> k[i] >> l[i] >> r[i];
    for (int i = 0; i < (n); ++i) {
      if (l[i] > r[i]) fro.push(make_pair(l[i] - r[i], k[i]));
      if (r[i] > l[i]) bac.push(make_pair(r[i] - l[i], k[i]));
    }
    long long ans = 0;
    for (int i = 0; i < (n); ++i) ans += r[i];
    while (!fro.empty()) {
      pair<int, int> tt = fro.top();
      fro.pop();
      auto it = pos.upper_bound(tt.second);
      if (it != pos.begin()) {
        it--;
        pos.erase(*it);
        ans += tt.first;
      }
    }
    int ss = (int)(pos).size();
    pos.clear();
    for (int i = 0; i < (n - ss); ++i) pos.insert(ss + i + 1);
    while (!bac.empty()) {
      pair<int, int> tt = bac.top();
      bac.pop();
      auto it = pos.upper_bound(tt.second);
      if (it == pos.end()) {
        ans -= tt.first;
      } else {
        pos.erase(*it);
      }
    }
    cout << ans << '\n';
    k.clear();
    l.clear();
    r.clear();
  }
  return 0;
}
