#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long mod1 = 998244353;
const long long inf = 92233720000000000ll;
long long n;
bool mywish(const pair<long long, long long> &a,
            const pair<long long, long long> &b) {
  if (a.second == b.second)
    return a.first > b.first;
  else
    return a.second < b.second;
}
long long optimum(vector<pair<long long, long long> > v, long long add) {
  sort(v.begin(), v.end(), mywish);
  long long sz = v.size();
  long long ans = 0;
  if (add == 0) {
    for (long long i = (0); i < (sz); i++)
      if (v[i].second >= i + 1) ans += v[i].first;
  } else {
    for (long long i = (0); i < (sz); i++)
      if (v[i].second < i + add + 1) ans += v[i].first;
  }
  return ans;
}
void solve() {
  vector<pair<long long, long long> > v1, v2;
  cin >> n;
  long long ans = 0;
  for (long long i = (0); i < (n); i++) {
    long long l, r, k;
    cin >> k >> l >> r;
    if (l > r)
      v1.push_back({l - r, k});
    else
      v2.push_back({r - l, k});
    ans += min(l, r);
  }
  ans += optimum(v1, 0);
  ans += optimum(v2, (long long)v1.size());
  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;
}
