#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 233;
int L[maxn], R[maxn], n;
void add(int *ch, int x) {
  for (int i = x; i <= n; i += i & (-i)) ch[i]++;
}
int query(int *ch, int x) {
  int sum = 0;
  for (int i = x; i; i -= i & (-i)) sum += ch[i];
  return sum;
}
struct camel {
  int l, r, k, v;
} a[maxn];
bool cmp(const camel &a, const camel &b) { return a.v > b.v; }
int main() {
  int T;
  cin >> T;
  while (T--) {
    long long ans = 0;
    cin >> n;
    for (int i = 1; i <= n; i++) L[i] = R[i] = 0;
    for (int i = 1; i <= n; i++)
      scanf("%d%d%d", &a[i].k, &a[i].l, &a[i].r),
          a[i].v = max(a[i].l, a[i].r) - min(a[i].l, a[i].r);
    sort(a + 1, a + 1 + n, cmp);
    for (int i = 1; i <= n; i++) {
      if (a[i].l >= a[i].r) {
        int k = a[i].k;
        if (query(L, k) < k) {
          add(L, k);
          ans += a[i].l;
        } else
          ans += a[i].r;
      } else {
        int k = n - a[i].k;
        if (query(R, k) < k) {
          add(R, k);
          ans += a[i].r;
        } else
          ans += a[i].l;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
