#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 10;
long long t, n, k[maxn], l[maxn], r[maxn], cnt[maxn];
vector<long long> v;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    v.clear();
    memset(cnt, 0, sizeof cnt);
    cin >> n;
    long long ans = 0;
    for (long long i = 0; i < n; i++) {
      cin >> k[i] >> l[i] >> r[i];
      ans += max(l[i], r[i]);
      v.push_back(l[i] - r[i]);
      cnt[k[i]]++;
    }
    sort(v.rbegin(), v.rend());
    long long pos = 1e9;
    for (long long i = 0; i < n; i++)
      if (v[i] < 0) {
        pos = i;
        break;
      }
    long long sum = 0;
    long long xx;
    for (long long i = 1; i <= n; i++) {
      sum += cnt[i];
      if (sum > i) {
        for (long long j = sum - 1; j > i + 1; j--) ans -= abs(v[j]);
        sum = i;
      }
      if (sum >= pos || i >= pos) {
        xx = i;
        break;
      }
    }
    for (long long i = xx; i <= n; i++) ans += v[i];
    cout << ans << endl;
  }
  return 0;
}
