#include <bits/stdc++.h>
using namespace std;
void Fast_IO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
}
int main() {
  Fast_IO();
  int n_testcases;
  cin >> n_testcases;
  for (int n_testcase = 1; n_testcase <= n_testcases; n_testcase++) {
    long long n;
    cin >> n;
    vector<vector<pair<long long, pair<long long, long long> > > > k(n);
    for (long long _ = (long long)1; _ <= (long long)n; _++) {
      long long ki, li, ri;
      cin >> ki >> li >> ri;
      ki--;
      k[ki].push_back(make_pair(li - ri, make_pair(li, ri)));
    }
    for (long long i = (long long)0; i <= (long long)n - 1; i++)
      if ((long long)int(k[i].size()) > 0)
        sort(k[i].begin(), k[i].end(),
             greater<pair<long long, pair<long long, long long> > >());
    vector<pair<long long, pair<long long, long long> > > neg0;
    long long ans = 0;
    long long taken = 0, tot = 0;
    for (long long i = (long long)0; i <= (long long)n - 1; i++)
      if ((long long)int(k[i].size()) > 0) {
        long long j = 0;
        while (j <= (long long)int(k[i].size()) - 1) {
          if (k[i][j].first <= 0) {
            neg0.push_back(make_pair(
                i + 1, make_pair(k[i][j].second.first, k[i][j].second.second)));
          } else {
            if (taken < i + 1)
              ans += k[i][j].second.first, taken++;
            else
              ans += k[i][j].second.second;
            tot++;
          }
          j++;
        }
      }
    for (auto x : neg0) {
      if (x.first <= tot)
        ans += x.second.second;
      else
        ans += x.second.first;
      tot++;
    }
    cout << ans << '\n';
  }
  return 0;
}
