#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f, N = 5e5 + 5;
int n, n1;
struct node {
  long long k, l, r;
} a[N];
bool cmp1(node x, node y) { return x.l - x.r > y.l - y.r; }
bool cmp(node x, node y) { return x.k > y.k; }
bool cmp2(node x, node y) { return x.k < y.k; }
void slove2() {
  priority_queue<long long> q;
  long long ans = 0;
  int j = 1, i;
  for (i = 1; i <= n1; i++) {
    while (a[j].k == i && j <= n1) {
      ans += a[j].l;
      q.push(-a[j].l + a[j].r);
      j++;
    }
    while (q.size() > i) {
      int x = q.top();
      q.pop();
      ans += x;
    }
  }
  while (j <= n1) ans += a[j].l, j++;
  while (!q.empty()) q.pop();
  j = n;
  while (a[j].k > n) {
    ans += a[j].l;
    j--;
  }
  for (int i = n; i > n1; i--) {
    while (a[j].k == i && j > n1) {
      q.push(a[j].l - a[j].r);
      ans += a[j].r;
      j--;
    }
    while (q.size() > n - i + 1) {
      long long x = q.top();
      q.pop();
      ans += x;
    }
  }
  while (j > n1) {
    ans += a[j].r;
    j--;
  }
  cout << ans << endl;
}
int main() {
  freopen("b.out", "w", stdout);
  int i, T;
  cin >> T;
  while (T--) {
    cin >> n;
    for (i = 1; i <= n; i++) scanf("%d%d%d", &a[i].k, &a[i].l, &a[i].r);
    sort(a + 1, a + n + 1, cmp1);
    for (i = 1; i <= n; i++) {
      if (a[i].l < a[i].r) break;
    }
    n1 = i;
    n1--;
    sort(a + 1, a + n1 + 1, cmp2);
    sort(a + n1 + 1, a + n + 1, cmp2);
    for (i = n1 + 1; i <= n; i++) a[i].k++;
    slove2();
  }
}
