#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
struct node {
  int l, r, k;
} p[200005];
bool cmp(int x, int y) { return p[x].k < p[y].k; }
bool cmq(int x, int y) { return p[x].k > p[y].k; }
void solve() {
  int n;
  cin >> n;
  int ans = 0;
  vector<int> v1, v2;
  for (int i = 1; i <= n; i++) {
    cin >> p[i].k >> p[i].l >> p[i].r;
    if (p[i].r > p[i].l) {
      v2.emplace_back(i);
      ans += p[i].l;
    } else {
      ans += p[i].r;
      v1.emplace_back(i);
    }
  }
  sort(v1.begin(), v1.end(), cmp);
  priority_queue<int, vector<int>, greater<int> > q;
  int cnt = 0;
  for (auto u : v1) {
    int t = p[u].k;
    if (t <= cnt) {
      int s = q.top();
      q.pop();
      if (s < p[u].l - p[u].r) {
        q.emplace(p[u].l - p[u].r);
      } else
        q.emplace(s);
    } else {
      q.emplace(p[u].l - p[u].r);
      cnt++;
    }
  }
  while (!q.empty()) {
    int cur = q.top();
    ans += cur;
    q.pop();
  }
  sort(v2.begin(), v2.end(), cmq);
  cnt = 0;
  for (auto u : v2) {
    int t = p[u].k + 1;
    if (t > n) continue;
    if (t >= n - cnt + 1) {
      int s = q.top();
      q.pop();
      if (s < p[u].r - p[u].l) {
        q.emplace(p[u].r - p[u].l);
      } else
        q.emplace(s);
    } else {
      q.emplace(p[u].r - p[u].l);
      cnt++;
    }
  }
  while (!q.empty()) {
    int cur = q.top();
    ans += cur;
    q.pop();
  }
  cout << ans << '\n';
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
