#include <bits/stdc++.h>
using namespace std;
long long hap(vector<pair<long long, long long> > left) {
  int n = left.size();
  sort(left.begin(), left.end());
  multiset<pair<long long, long long> > candi;
  for (pair<long long, long long> p : left) candi.insert(p);
  int id = 0;
  multiset<pair<long long, long long> > need;
  vector<pair<long long, long long> > dis;
  for (int l = 1; l <= n; ++l) {
    while (id < n && left[id].first == l) {
      need.insert(left[id]);
      id++;
    }
    while (need.size() > l) {
      dis.push_back(*need.begin());
      candi.erase(candi.find(*need.begin()));
      need.erase(need.begin());
    }
  }
  long long hap = 0;
  for (auto p : candi) {
    hap += p.second;
  }
  return hap;
}
void solve() {
  int n;
  cin >> n;
  vector<pair<long long, long long> > left, right;
  long long base = 0;
  for (int i = 0; i < n; ++i) {
    long long k, l, r;
    cin >> k >> l >> r;
    if (l > r && k < n)
      left.push_back({k, l - r});
    else if (l < r && k < n)
      right.push_back({n - k, r - l});
    if (k < n)
      base += min(l, r);
    else
      base += l;
  }
  cout << base + hap(left) + hap(right) << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
