#include <bits/stdc++.h>
using namespace std;
const int nm = 200002;
struct rec {
  int k;
  long long l, r;
};
int n;
rec a[nm];
int nb, nc;
rec b[nm], c[nm];
bool cmp(rec &a, rec &b) {
  if (a.k != b.k) {
    return a.k < b.k;
  }
  return a.l - a.r > b.l - b.r;
}
bool cmp2(rec &a, rec &b) {
  if (a.k != b.k) {
    return a.k > b.k;
  }
  return a.r - a.l > b.r - b.l;
}
void solve() {
  scanf("%d", &n);
  long long res = 0;
  nb = nc = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d%lld%lld", &a[i].k, &a[i].l, &a[i].r);
    if (a[i].l > a[i].r) {
      b[++nb] = a[i];
    } else if (a[i].l < a[i].r) {
      c[++nc] = a[i];
    } else {
      res += a[i].l;
    }
  }
  sort(b + 1, b + nb + 1, cmp);
  int cur = 0;
  for (int i = 1; i <= nb; ++i) {
    if (cur + 1 <= b[i].k) {
      res += b[i].l;
      cur++;
    } else {
      res += b[i].r;
    }
  }
  sort(c + 1, c + nc + 1, cmp2);
  cur = n + 1;
  for (int i = 1; i <= nc; ++i) {
    if (cur - 1 > c[i].k) {
      res += c[i].r;
      cur--;
    } else {
      res += c[i].l;
    }
  }
  printf("%lld\n", res);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    solve();
  }
}
