#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long int sm = 0;
    priority_queue<int> pq[n];
    priority_queue<int, vector<int>, greater<int> > rs;
    for (int i = 0; i < n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      sm += r;
      pq[k - 1].push(l - r);
    }
    int comp = 0, prv = 0;
    for (int i = 0; i < n; i++) {
      int c1 = pq[i].size(), cr = 0;
      if (pq[i].empty()) continue;
      while (!pq[i].empty()) {
        int x = pq[i].top();
        if (rs.size() < i + 1) {
          if (rs.size() and rs.top() < 0 and rs.size() >= prv)
            rs.pop(), rs.push(0);
          rs.push(x);
        } else if (x > (int)rs.top())
          rs.pop(), rs.push(x);
        else
          rs.push(0);
        pq[i].pop();
        cr++;
      }
      comp += c1;
      prv = i + 1;
    }
    while (!rs.empty()) {
      sm += (int)rs.top();
      rs.pop();
    }
    cout << sm << "\n";
  }
  return 0;
}
