#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
struct T {
  int k, v;
};
T a[N], b[N];
int x[N << 2];
void pu(int rt) { x[rt] = min(x[rt << 1 | 1], x[rt << 1]); }
void modify(int p, int v, int st, int en, int rt) {
  if (st == en) {
    x[rt] = 1;
    return;
  }
  if (p <= ((st + en) >> 1))
    modify(p, v, st, (st + en) >> 1, rt << 1);
  else
    modify(p, v, ((st + en) >> 1) + 1, en, rt << 1 | 1);
  pu(rt);
}
int query(int l, int r, int st, int en, int rt) {
  if (l > en || r < st) return 1;
  if (l <= st && en <= r) return x[rt];
  return min(query(l, r, st, (st + en) >> 1, rt << 1),
             query(l, r, ((st + en) >> 1) + 1, en, rt << 1 | 1));
}
void build(int st, int en, int rt) {
  x[rt] = 0;
  if (st == en) return;
  build(st, (st + en) >> 1, rt << 1);
  build(((st + en) >> 1) + 1, en, rt << 1 | 1);
  pu(rt);
}
long long solve(T* a, int n) {
  sort(a + 1, a + n + 1, [&](T x, T y) {
    if (x.v != y.v) return x.v > y.v;
    return x.k > y.k;
  });
  long long ret = 0;
  build(1, n, 1);
  for (int i = 1; i <= n; i++) {
    int st = 1, en = a[i].k;
    if (a[i].k > n) {
      ret += a[i].v;
      continue;
    }
    if (a[i].k == 0) continue;
    if (query(st, en, 1, n, 1) != 0) {
      continue;
    }
    while (st != en) {
      int md = (st + en) >> 1;
      if (query(md + 1, en, 1, n, 1) == 0)
        st = md + 1;
      else
        en = md;
    }
    ret += a[i].v;
    modify(st, 1, 1, n, 1);
  }
  return ret;
}
int main(void) {
  int kase;
  scanf("%d", &kase);
  while (kase--) {
    int n;
    scanf("%d", &n);
    long long sum = 0;
    int na = 0, nb = 0;
    for (int i = 1, k, l, r; i <= n; i++) {
      scanf("%d%d%d", &k, &l, &r);
      sum += min(l, r);
      if (l > r)
        a[++na] = {k, l - r};
      else if (r > l)
        b[++nb] = {n - k, r - l};
    }
    sum += solve(a, na);
    sum += solve(b, nb);
    printf("%lld\n", sum);
  }
}
