#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long num = 0, f = 1;
  char x = getchar();
  while (x < '0' || x > '9') {
    if (x == '-') f = -1;
    x = getchar();
  }
  while (x >= '0' && x <= '9') {
    num = num * 10 + x - '0';
    x = getchar();
  }
  return num * f;
}
const long long maxn = 200005;
struct point {
  long long k, d;
} p[maxn], a[maxn], b[maxn];
priority_queue<long long> pq;
long long n, cnt1, cnt2;
bool cmp1(point a, point b) { return a.k < b.k; }
bool cmp2(point a, point b) { return a.k > b.k; }
long long main() {
  long long T = read();
  while (T--) {
    n = read();
    long long ans = 0;
    cnt1 = cnt2 = 0;
    for (long long i = 1, l, r; i <= n; ++i) {
      p[i].k = read();
      l = read();
      r = read();
      p[i].d = l - r;
      if (l - r > 0) {
        ans += r;
        a[++cnt1] = p[i];
      } else {
        ans += l;
        b[++cnt2] = p[i];
        b[cnt2].d = -b[cnt2].d;
      }
    }
    sort(a + 1, a + cnt1 + 1, cmp1);
    sort(b + 1, b + cnt2 + 1, cmp2);
    for (long long i = 1; i <= cnt1; ++i) {
      pq.push(a[i].d);
      if (pq.size() > a[i].k) pq.pop();
    }
    while (pq.size()) {
      ans += pq.top();
      pq.pop();
    }
    for (long long i = 1; i <= cnt2; ++i) {
      pq.push(b[i].d);
      if (pq.size() > n - b[i].k) pq.pop();
    }
    while (pq.size()) {
      ans += pq.top();
      pq.pop();
    }
    printf("%lld\n", ans);
  }
  return 0;
}
