#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f, N = 5e5 + 5;
int n;
struct node {
  long long k, l, r;
} a[N];
long long b[N];
void slove1() {
  long long ans = 0, i;
  do {
    long long tmp = 0;
    for (i = 1; i <= n; i++) tmp += (b[i] <= a[i].k ? a[i].l : a[i].r);
    ans = max(ans, tmp);
  } while (next_permutation(b + 1, b + n + 1));
  cout << ans << endl;
}
bool cmp(node x, node y) { return x.k > y.k; }
void slove2() {
  sort(a + 1, a + n + 1, cmp);
  priority_queue<long long> q;
  long long ans = 0;
  int j = 1;
  for (int i = n - 1; i >= 0; i--) {
    while (a[j].k == i + 1 && j <= n) {
      q.push(a[j].l - a[j].r);
      ans += a[j].r;
      j++;
    }
    while ((q.size() > n - i || (q.top() > 0)) && !q.empty()) {
      long long x = q.top();
      q.pop();
      ans += x;
    }
  }
  cout << ans << endl;
}
int main() {
  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);
      b[i] = i;
    }
    if (n <= 10) {
      slove1();
    } else
      slove2();
  }
}
