#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<vector<int>> rightPlace(n + 1);
    long long ans = 0;
    multiset<int> mset;
    for (int i = 0; i < n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      ans += l;
      rightPlace[k].push_back(r - l);
      mset.insert(r - l);
    }
    for (int i = n; i >= 1; i--) {
      for (int j = 0; j < rightPlace[i].size(); j++) {
        if (mset.find(rightPlace[i][j]) != mset.end()) {
          mset.erase(mset.find(rightPlace[i][j]));
          mset.insert(0);
        }
      }
      ans += *(--mset.end());
      mset.erase(--mset.end());
    }
    cout << ans << endl;
  }
  return 0;
}
