#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long mod1 = 998244353;
const long long inf = 2e18 + 5;
const long long nax = 200005;
long long n;
vector<pair<long long, pair<long long, long long>>> a, b;
bool mywish(const pair<long long, pair<long long, long long>> &a,
            const pair<long long, pair<long long, long long>> &b) {
  return a.first < b.first;
}
bool mywish2(const pair<long long, pair<long long, long long>> &a,
             const pair<long long, pair<long long, long long>> &b) {
  if (a.first == b.first)
    return a.second.first > b.second.first;
  else
    return a.first > b.first;
}
void solve() {
  cin >> n;
  a.clear();
  b.clear();
  long long ans = 0;
  for (long long i = (1); i < (n + 1); i++) {
    long long k, l, r;
    cin >> k >> l >> r;
    if (r >= l)
      b.push_back({k, {l, r}});
    else
      a.push_back({k, {l, r}});
  }
  sort(a.begin(), a.end(), mywish);
  sort(b.begin(), b.end(), mywish2);
  long long pos = 1;
  long long p = 0;
  while (p < a.size()) {
    if (pos <= a[p].first)
      ans += a[p].second.first;
    else
      ans += a[p].second.second;
    p++;
    pos++;
  }
  p = 0;
  while (p < b.size()) {
    if (pos <= b[p].first)
      ans += b[p].second.first;
    else
      ans += b[p].second.second;
    p++;
    pos++;
  }
  for (auto x : a)
    cerr << x.first << " " << x.second.first << " " << x.second.second << '\n';
  cerr << '\n';
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) solve();
  return 0;
}
