#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int inf = 1e9;
int mod = 1e9 + 7;
signed int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int _;
  cin >> _;
  while (_--) {
    int n;
    cin >> n;
    vector<pair<int, int>> M[n + 1];
    long long int ans = 0;
    multiset<pair<int, int>> trash;
    bool ban[n + 1];
    memset(ban, false, sizeof(ban));
    for (int i = 0; i < n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      M[k].push_back({l - r, i});
      ans += 1LL * r;
      trash.insert({l - r, -i});
    }
    multiset<pair<int, int>> s;
    for (int i = n; i > 0; i--) {
      for (auto it : M[i]) {
        int u = it.first;
        int x = it.second;
        if (ban[x]) continue;
        s.insert(it);
        trash.erase(trash.find({u, -x}));
      }
      if (s.empty()) {
        auto it = *(trash.begin());
        trash.erase(trash.find(it));
        ban[-it.second] = true;
        continue;
      }
      auto it1 = *(s.rbegin());
      if (!trash.empty()) {
        auto it2 = *(trash.begin());
        if (it2.first < 0) {
          trash.erase(trash.find(it2));
          ban[-it2.second] = true;
          continue;
        }
      }
      ans += 1LL * it1.first;
      s.erase(s.find(it1));
    }
    cout << ans << "\n";
  }
  return 0;
}
