#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void readin(T &x) {
  x = 0;
  T fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') fh = -1;
  for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
  x *= fh;
}
inline void d_read(double &x) {
  x = 0.0;
  int fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') fh = -1;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48);
  if (ch == '.') {
    double num = 1.0;
    ch = getchar();
    for (; isdigit(ch); ch = getchar()) x = x + (num /= 10) * (ch ^ 48);
  }
  x *= fh;
}
template <typename T>
inline void wt(T x) {
  if (x > 9) wt(x / 10);
  putchar(x % 10 + 48);
}
template <typename T>
inline void writeln(T x, char c) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  wt(x);
  putchar(c);
}
struct info {
  int pos;
  long long del;
} a[200005], b[200005];
inline bool operator<(const info &x, const info &y) { return x.del > y.del; }
priority_queue<info> q;
int n, al, bl;
inline bool comp1(info x, info y) {
  if (x.pos ^ y.pos) return x.pos > y.pos;
  return x.del < y.del;
}
inline bool comp2(info x, info y) {
  if (x.pos ^ y.pos) return x.pos < y.pos;
  return x.del < y.del;
}
int main() {
  int T;
  readin(T);
  while (T--) {
    readin(n);
    int k;
    long long l, r;
    al = 0;
    bl = 0;
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
      readin(k);
      readin(l);
      readin(r);
      if (l > r)
        a[++al] = (info){k, l - r};
      else
        b[++bl] = (info){k, r - l};
      ans += max(l, r);
    }
    sort(a + 1, a + al + 1, comp1);
    a[al + 1].pos = 0;
    for (int p = al, i = 1; p >= 1; p--) {
      if (q.empty()) {
        q.push(a[i]);
        i++;
      }
      while (a[i].pos >= p) {
        q.push(a[i]);
        i++;
      }
      if (q.top().pos < p) ans -= q.top().del;
      q.pop();
    }
    sort(b + 1, b + bl + 1, comp2);
    b[bl + 1].pos = INT_MAX;
    for (int p = al + 1, i = 1; p <= n; p++) {
      if (q.empty()) {
        q.push(b[i]);
        i++;
      }
      while (b[i].pos < p) {
        q.push(b[i]);
        i++;
      }
      if (q.top().pos >= p) ans -= q.top().del;
      q.pop();
    }
    writeln(ans, '\n');
  }
  return 0;
}
