#include <bits/stdc++.h>
using namespace std;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline long long msbp(long long x) { return 31 - __builtin_clz(x); }
inline long long msb(long long x) { return 1 << msbp(x); }
const long long INF = 0x3f3f3f3f;
const long long N = 2e5 + 10;
long long t, n;
long long solve(vector<pair<long long, long long> >& v) {
  sort(v.begin(), v.end());
  long long res = 0;
  set<long long> cur;
  for (auto [k, add] : v) {
    if (cur.size() < k) {
      cur.insert(add);
    } else if (add > *cur.begin()) {
      cur.erase(cur.begin());
      cur.insert(add);
    }
  }
  for (long long x : cur) res += x;
  return res;
}
signed main() {
  ios::sync_with_stdio(false);
  cin >> t;
  while (t--) {
    cin >> n;
    long long ans = 0;
    vector<pair<long long, long long> > v1, v2;
    for (long long i = 0; i < (n); ++i) {
      long long k, l, r;
      cin >> k >> l >> r;
      if (k == n) {
        ans += l;
        continue;
      }
      ans += min(l, r);
      if (l > r)
        v1.push_back({k, l - r});
      else
        v2.push_back({n - k, r - l});
    }
    cout << ans + solve(v1) + solve(v2) << endl;
  }
  return 0;
}
