#include <bits/stdc++.h>
using namespace std;
int t;
int n;
int l[200086], r[200086], k[200086];
int a[200086];
set<int> st[2];
long long ans;
inline bool cmp(int i, int j) {
  int x, y;
  if (l[i] > r[i])
    x = k[i];
  else
    x = n - k[i];
  if (l[j] > r[j])
    y = k[j];
  else
    y = n - k[j];
  if (x ^ y) return x < y;
  return abs(l[i] - r[i]) < abs(l[j] - r[j]);
}
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    st[0].clear(), st[1].clear();
    for (int i = 1; i <= n; i++) st[0].insert(-i), st[1].insert(i), a[i] = i;
    ans = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d%d", &k[i], &l[i], &r[i]);
    }
    sort(a + 1, a + 1 + n, cmp);
    for (int i = 1; i <= n; i++) {
      int x = a[i];
      if (l[x] > r[x]) {
        set<int>::iterator it = st[0].lower_bound(-k[x]);
        if (it == st[0].end()) {
          ans += r[x];
        } else {
          ans += l[x];
          st[0].erase(it);
          st[1].erase(-(*it));
        }
      } else {
        set<int>::iterator it = st[1].upper_bound(k[x]);
        if (it == st[1].end()) {
          ans += l[x];
        } else {
          ans += r[x];
          st[1].erase(it);
          st[0].erase(-(*it));
        }
      }
    }
    printf("%lld\n", ans);
  }
}
