#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<long long int> k(n + 5), l(n + 5), r(n + 5), f1(n + 5, 0);
  vector<vector<int> > v(n + 5, vector<int>());
  for (int i = 1; i <= n; i++)
    cin >> k[i] >> l[i] >> r[i], v[k[i]].push_back(i);
  multiset<pair<long long int, long long int> > s;
  for (int i = 1; i <= n; i++) {
    if (v[i].size() == 0) continue;
    vector<int> v1 = v[i];
    vector<pair<long long int, long long int> > v2;
    for (__typeof(v1.begin()) it = v1.begin(); it != v1.end(); it++) {
      int id = *it;
      v2.push_back(make_pair(l[id] - r[id], id));
    }
    sort(v2.begin(), v2.end());
    reverse(v2.begin(), v2.end());
    int sz = v2.size();
    for (int j = 0; j <= sz - 1; j++) {
      if (s.size() < i) {
        s.insert(v2[j]);
        f1[v2[j].second] = 1;
        continue;
      }
      multiset<pair<long long int, long long int> >::iterator it = s.begin();
      if ((it->first) < v2[j].first) {
        f1[it->second] = 0;
        s.erase(it);
        s.insert(v2[j]);
      }
    }
  }
  long long int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (f1[i])
      ans += l[i];
    else
      ans += r[i];
  }
  vector<pair<long long int, long long int> > v3;
  for (int i = 1; i <= n; i++) {
    if (f1[i]) v3.push_back(make_pair(l[i] - r[i], i));
  }
  sort(v3.begin(), v3.end());
  reverse(v3.begin(), v3.end());
  int sz = v3.size();
  for (int i = 1; i <= sz; i++) {
    int id = v3[i - 1].second;
    if (k[id] < i && r[id] > l[id]) {
      ans += r[id] - l[id];
    }
  }
  cout << ans;
  cout << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
