#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vp = vector<pair<int, int>>;
ll calc(vp p) {
  int n = p.size();
  for (int i = 0; i < n; i++) p[i].first = min(p[i].first, n);
  vector<vector<int>> s(n + 1);
  for (int i = 0; i < n; i++) s[p[i].first].push_back(p[i].second);
  ll res = 0;
  priority_queue<int> q;
  for (int i = n; i >= 1; --i) {
    for (int x : s[i]) q.push(x);
    if (q.size() > 0) {
      res += q.top();
      q.pop();
    }
  }
  return res;
}
void solve() {
  int n;
  cin >> n;
  vp pl, pr;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    int k, l, r;
    cin >> k >> l >> r;
    if (l > r) {
      ans += r;
      pl.emplace_back(k, l - r);
    } else {
      ans += l;
      pr.emplace_back(n + 1 - k, r - l);
    }
  }
  ans += calc(pl);
  ans += calc(pr);
  cout << ans << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
