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