#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
struct T {
  int k, v;
};
T a[N], b[N];
long long solve(T* a, int n) {
  sort(a + 1, a + n + 1, [&](T x, T y) {
    if (x.k == y.k)
      return x.v > y.v;
    else
      return x.k < y.k;
  });
  long long ret = 0;
  for (int i = 1, j = 1; i <= n; i++) {
    if (j <= a[i].k) {
      ret += a[i].v;
      j++;
    }
  }
  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 (k == n)
        sum += l - min(l, r);
      else if (l > r)
        a[++na] = {k, l - r};
      else
        b[++nb] = {n - k, r - l};
    }
    sum += solve(a, na);
    sum += solve(b, nb);
    printf("%lld\n", sum);
  }
}
