#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 2e9;
const double PI = acos(-1);
int main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  for (int u = 0; u < t; u++) {
    int n;
    cin >> n;
    vector<pair<long long, pair<long long, long long>>> p, q;
    for (int i = 0; i < n; i++) {
      long long k, l, r;
      cin >> k >> l >> r;
      if (l > r)
        p.push_back({k, {l - r, r}});
      else
        q.push_back({k, {r - l, r}});
    }
    sort(p.begin(), p.end());
    sort(q.begin(), q.end());
    long long ans = 0;
    int ps = p.size(), qs = q.size();
    for (int i = 0; i < ps; i++) {
      if (i + 1 <= p[i].first)
        ans += p[i].second.first + p[i].second.second;
      else
        ans += p[i].second.second;
    }
    for (int i = 0; i < qs; i++) {
      if (ps + i + 1 <= q[i].first)
        ans += -q[i].second.first + q[i].second.second;
      else
        ans += q[i].second.second;
    }
    cout << ans << endl;
  }
  return 0;
}
