#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
struct A {
  int l, k, r;
} a[N];
int cmp(A a, A b) { return a.k < b.k; }
int main() {
  int t, n;
  cin >> t;
  while (t--) {
    priority_queue<int, vector<int>, greater<int> > que;
    long long sum = 0;
    long long now = 0;
    int cnt = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d%d%d", &a[i].k, &a[i].l, &a[i].r);
    }
    sort(a + 1, a + n + 1, cmp);
    int j = 1;
    for (int i = 1; i <= n; i++) {
      while (j <= n && a[j].k <= i) {
        if (a[i].l >= a[i].r) {
          sum += a[i].r;
          que.push(a[i].l - a[i].r);
        }
        j++;
      }
      while (que.size() > i) {
        que.pop();
      }
    }
    while (que.size()) {
      sum += que.top();
      que.pop();
    }
    j = n;
    for (int i = n; i > 0; i--) {
      while (a[j].k >= i && j) {
        if (a[j].r > a[j].l) {
          sum += a[j].l;
          que.push(a[j].r - a[j].l);
        }
        j--;
      }
      while (que.size() > n - i) que.pop();
    }
    while (que.size()) {
      sum += que.top();
      que.pop();
    }
    cout << sum << endl;
  }
  return 0;
}
