#include <bits/stdc++.h>
using namespace std;
const long long maxn = 10010;
long long n;
void solve() {
  cin >> n;
  vector<long long> infrontbetter[n + 2];
  vector<long long> behindbetter[n + 2];
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    long long k, l, r;
    cin >> k >> l >> r;
    if (l >= r)
      infrontbetter[k].push_back(l - r);
    else
      behindbetter[k].push_back(r - l);
    ans += min(l, r);
  }
  set<long long> S;
  for (long long i = 1; i <= n; i++) {
    for (auto j : infrontbetter[i]) {
      S.insert(j);
    }
    while ((long long)S.size() > i) S.erase(S.begin());
  }
  for (auto i : S) ans += i;
  S.clear();
  for (long long i = n; i >= 2; i--) {
    for (auto j : behindbetter[i - 1]) {
      S.insert(j);
    }
    while ((long long)S.size() > (n - i + 1)) S.erase(S.begin());
  }
  for (auto i : S) ans += i;
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long tc;
  cin >> tc;
  while (tc--) solve();
}
