#include <bits/stdc++.h>
template <class T>
void rd(T &x) {
  x = 0;
  T f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
using namespace std;
const int N = 2e5 + 10;
int T, n;
struct ghb {
  int k, l, r;
  void read() {
    rd(k);
    rd(l);
    rd(r);
  }
  bool operator<(const ghb &o) const { return k > o.k; }
} a[N], a1[N], a2[N];
int cnt1, cnt2;
bool cmp(ghb x, ghb y) { return x.k < y.k; }
int main() {
  rd(T);
  while (T--) {
    rd(n);
    cnt1 = cnt2 = 0;
    for (int i = 1; i <= n; i++) a[i].read();
    for (int i = 1; i <= n; i++)
      if (a[i].l >= a[i].r)
        a1[++cnt1] = a[i];
      else
        a2[++cnt2] = a[i];
    sort(a1 + 1, a1 + cnt1 + 1, cmp);
    sort(a2 + 1, a2 + cnt2 + 1);
    long long ans = 0;
    for (int i = 1; i <= cnt1; i++) ans += a1[i].l;
    for (int i = 1; i <= cnt2; i++) ans += a2[i].r;
    if (cnt2 && (cnt1 == 0 || a1[cnt1].k <= a2[1].k)) {
      for (int i = 1; i <= cnt2; i++)
        if (a2[i].k == a2[1].k)
          ans += a2[i].l - a2[i].r;
        else
          break;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
