#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
long long n, T, b[maxn], t = 0, m = 0, k, a[maxn], ans, vit[maxn], f[maxn];
struct dang {
  long long x, y, dd;
};
dang c[maxn];
bool check, ck;
string s;
vector<long long> adj[maxn];
inline void sol() {
  cin >> n;
  ans = 0;
  priority_queue<long long, vector<long long>, greater<long long> > pr;
  for (int i = 1; i <= n; i++) {
    long long k, l, r;
    cin >> k >> l >> r;
    if (l >= r) {
      adj[k].push_back(l - r);
    } else {
      adj[n - k].push_back({r - l});
    }
    ans += min(l, r);
  }
  for (int i = 1; i <= n; i++) {
    for (long long u : adj[i]) {
      pr.push(u);
      ans += u;
    }
    while (pr.size() > i) {
      ans -= pr.top();
      pr.pop();
    }
  }
  cout << ans << '\n';
  for (int i = 1; i <= n; i++) adj[i].clear();
}
int main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  cin >> T;
  while (T-- > 0) sol();
}
