#include <bits/stdc++.h>
using namespace std;
long long t, n, k, l, r;
vector<pair<long long, pair<long long, long long> > > v, v1;
bool trick(const pair<long long, pair<long long, long long> > &a,
           const pair<long long, pair<long long, long long> > &b) {
  if (a.first < b.first)
    return true;
  else if (a.first > b.first)
    return false;
  else
    return (a.second.first - a.second.second >
            b.second.first - b.second.second);
}
bool trick2(const pair<long long, pair<long long, long long> > &a,
            const pair<long long, pair<long long, long long> > &b) {
  if (a.first < b.first)
    return true;
  else if (a.first > b.first)
    return false;
  else
    return (a.second.first - a.second.second <
            b.second.first - b.second.second);
}
void solve() {
  cin >> t;
  while (t--) {
    v.clear();
    v1.clear();
    cin >> n;
    for (long long i = 0; i < n; i++) {
      cin >> k >> l >> r;
      if (l > r)
        v.push_back(make_pair(k, make_pair(l, r)));
      else
        v1.push_back(make_pair(k, make_pair(l, r)));
    }
    sort(v.begin(), v.end(), trick);
    sort(v1.begin(), v1.end(), trick2);
    long long cnt = 0;
    for (long long i = 0; i < v.size(); i++) {
      if (v[i].first >= i + 1)
        cnt += v[i].second.first;
      else
        cnt += v[i].second.second;
    }
    for (long long i = 0; i < v1.size(); i++) {
      if (v1[i].first >= (v.size() + i + 1))
        cnt += v1[i].second.first;
      else
        cnt += v1[i].second.second;
    }
    cout << cnt << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}
