#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double EPS = 1e-9;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
signed main(void) {
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long sum = 0;
    vector<pair<long long, long long> > vpo;
    vector<pair<long long, long long> > vne;
    for (long long i = 0; i < (n); i++) {
      long long a, b, c;
      cin >> a >> b >> c;
      if (c - b >= 0)
        vpo.push_back(pair<long long, long long>(a, c - b));
      else
        vne.push_back(pair<long long, long long>(a, c - b));
      sum += b;
    }
    sort(vpo.begin(), vpo.end());
    reverse(vpo.begin(), vpo.end());
    priority_queue<long long> q;
    for (long long i = 0; i < (vpo.size()); i++) {
      long long num = n - q.size();
      if (vpo[i].first < num) {
        q.push(-vpo[i].second);
      } else if (q.size() && -q.top() < vpo[i].second) {
        q.pop();
        q.push(-vpo[i].second);
      } else {
        q.push(0);
      }
    }
    sort(vne.begin(), vne.end());
    reverse(vne.begin(), vne.end());
    for (long long i = 0; i < (vne.size()); i++) {
      long long num = n - q.size();
      if (vne[i].first < num) {
        q.push(-vne[i].second);
      } else if (q.size() && -q.top() < vne[i].second) {
        q.pop();
        q.push(-vne[i].second);
      } else {
        q.push(0);
      }
    }
    long long ans = 0;
    while (q.size()) ans -= q.top(), q.pop();
    cout << sum + ans << endl;
  }
}
