#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200000 + 5;
struct BTree {
  int lt, rt, v;
} bt[MAXN * 3];
struct Camel {
  int K, L, R;
};
struct Camel a[MAXN], b[MAXN];
void build(int i, int l, int r) {
  bt[i].v = 0;
  bt[i].lt = l;
  bt[i].rt = r;
  if (l == r) {
    return;
  }
  int mid = (l + r) / 2;
  build(i << 1, l, mid);
  build((i << 1) + 1, mid + 1, r);
}
bool insert(int i, int K) {
  bool res = false;
  if (bt[i].v == bt[i].rt - bt[i].lt + 1) {
    return false;
  }
  if (bt[i].lt == bt[i].rt) {
    if (bt[i].v == 0) {
      bt[i].v++;
      return true;
    }
    return false;
  }
  int mid = (bt[i].rt + bt[i].lt) >> 1;
  if (K <= mid) {
    res = insert(i << 1, K);
  } else {
    res = insert((i << 1) + 1, K);
    if (res == false) {
      res = insert((i << 1), mid);
    }
  }
  if (res) {
    bt[i].v++;
  }
  return res;
}
bool cmp(Camel x, Camel y) { return (x.L - x.R) > (y.L - y.R); }
int solve(Camel* a, int n) {
  int ret = 0;
  build(1, 1, n);
  for (int i = 0; i < n; i++) {
    int K = a[i].K;
    if (K > 0 && insert(1, K)) {
      ret += a[i].L;
    } else {
      ret += a[i].R;
    }
  }
  return ret;
}
int main() {
  int T, N, na, nb;
  cin >> T;
  while (T--) {
    cin >> N;
    na = nb = 0;
    int K, L, R;
    for (int i = 0; i < N; i++) {
      cin >> K >> L >> R;
      if (L >= R) {
        a[na].K = K;
        a[na].L = L;
        a[na].R = R;
        na++;
      } else {
        b[nb].K = N - K;
        b[nb].L = R;
        b[nb].R = L;
        nb++;
      }
    }
    int ans = 0;
    if (na > 0) {
      sort(a, a + na, cmp);
      ans += solve(a, na);
    }
    if (nb > 0) {
      sort(b, b + nb, cmp);
      ans += solve(b, nb);
    }
    cout << ans << endl;
  }
  system("pause");
  return 0;
}
