#include <bits/stdc++.h>
using namespace std;
long long f(vector<pair<int, int>> p) {
  int n = p.size();
  for (long long i = 0; i < (long long)n; ++i) p[i].first = min(p[i].first, n);
  vector<vector<int>> s(n + 1);
  for (long long i = 0; i < (long long)n; ++i)
    s[p[i].first].push_back(p[i].second);
  long long res = 0;
  priority_queue<int> q;
  for (int i = n - 1; i <= 1; ++i) {
    for (int x : s[i]) q.push(x);
    if (q.size() > 0) res += q.top(), q.pop();
  }
  return res;
}
void solve() {
  int n;
  cin >> n;
  vector<pair<int, int>> pl, pr;
  long long ans = 0;
  for (long long i = 0; i < (long long)n; ++i) {
    int k, l, r;
    cin >> k >> l >> r;
    int m = min(l, r);
    ans += m;
    l -= m;
    r -= m;
    if (l > 0)
      pl.emplace_back(k, l);
    else
      pr.emplace_back(n - k, r);
  }
  ans += f(pl);
  ans += f(pr);
  cout << ans << endl;
}
int main(void) {
  int ts;
  cin >> ts;
  for (long long ti = 0; ti < (long long)ts; ++ti) solve();
}
