#include <bits/stdc++.h>
using namespace std;
long long t, n, apos = 0, bpos = 0, k, l, r, ans = 0;
priority_queue<long long, vector<long long>, greater<long long> > q1, q2;
struct node {
  long long L, R, k;
} a[200005], b[200005];
bool cmp(node x, node y) { return x.k < y.k; }
inline void clear() {
  apos = bpos = 0;
  ans = 0;
}
signed main() {
  cin >> t;
  while (t--) {
    cin >> n;
    for (long long i = 1; i <= n; i++) {
      cin >> k >> l >> r;
      if (l > r)
        a[++apos].L = l, a[apos].R = r, a[apos].k = k;
      else
        b[++bpos].L = l, b[bpos].R = r, b[bpos].k = n - k;
      ans += min(l, r);
    }
    sort(a + 1, a + 1 + apos, cmp);
    sort(b + 1, b + 1 + bpos, cmp);
    for (long long i = 1; i <= apos; i++) {
      if (a[i].k == 0) continue;
      if (q1.size() < a[i].k)
        q1.push(a[i].L - a[i].R);
      else if (q1.size() == a[i].k && q1.top() < a[i].L - a[i].R) {
        q1.pop();
        q1.push(a[i].L - a[i].R);
      }
    }
    for (long long i = 1; i <= bpos; i++) {
      if (b[i].k == 0) continue;
      if (q2.size() < b[i].k)
        q2.push(b[i].R - b[i].L);
      else if (q2.size() == b[i].k && q2.top() < b[i].R - b[i].L) {
        q2.pop();
        q2.push(b[i].R - b[i].L);
      }
    }
    while (!q1.empty()) {
      ans += q1.top();
      q1.pop();
    }
    while (!q2.empty()) {
      ans += q2.top();
      q2.pop();
    }
    cout << ans << endl;
  }
  return 0;
}
