#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(random_device{}());
const int mod = 998244353;
const int N = 2e5 + 5;
const int M = 1e6 + 5;
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
inline long long ksm(long long x, long long y) {
  long long res = 1;
  while (y) {
    if (y & 1) res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int n, m;
struct node {
  int k, l, r;
  int flag;
  int id;
} a[N];
bool cmp(node p, node q) {
  int x = abs(p.l - p.r), y = abs(q.l - q.r);
  if (x != y) return x > y;
  int k1 = p.k, k2 = q.k;
  if (p.l - p.r < 0) k1 = n - k1 + 1;
  if (q.l - q.r < 0) k2 = n - k2 + 1;
  return k1 < k2;
}
int pre[N], pre2[N];
int Find(int x) { return pre[x] == x ? x : pre[x] = Find(pre[x]); }
void join(int x, int y) {
  int fx = Find(x), fy = Find(y);
  if (fx != fy) {
    pre[fx] = fy;
  }
}
int Find2(int x) { return pre2[x] == x ? x : pre2[x] = Find2(pre2[x]); }
void join2(int x, int y) {
  int fx = Find2(x), fy = Find2(y);
  if (fx != fy) {
    pre2[fx] = fy;
  }
}
int main() {
  ;
  ios::sync_with_stdio(false);
  cin.tie(0);
  ;
  int _;
  cin >> _;
  while (_--) {
    cin >> n;
    for (int i = 0; i <= n + 1; i++) pre[i] = pre2[i] = i;
    for (int i = 1; i <= n; i++) {
      cin >> a[i].k >> a[i].l >> a[i].r;
      a[i].id = i;
      if (a[i].l - a[i].r > 0)
        a[i].flag = 1;
      else
        a[i].flag = 0;
    }
    sort(a + 1, a + 1 + n, cmp);
    int last = n;
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
      if (a[i].flag) {
        int now = Find(a[i].k);
        if (now == 0) {
          ans += a[i].r;
        } else {
          ans += a[i].l;
          join(now, now - 1);
          join2(now, now + 1);
        }
      } else {
        int now = Find2(a[i].k + 1);
        if (now == n + 1) {
          ans += a[i].l;
        } else {
          ans += a[i].r;
          join(now, now - 1);
          join2(now, now + 1);
        }
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
