#include <bits/stdc++.h>
using namespace std;
struct camel {
  int i, l, r;
  bool assigned;
  camel() { this->assigned = false; }
};
bool operator<(const camel& c1, const camel& c2) {
  int d1 = c1.l - c1.r;
  int d2 = c2.l - c2.r;
  if (d1 == d2) return c1.i < c2.i;
  return d1 > d2;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    vector<camel> v(n);
    for (int i = 0; i < n; i++) {
      cin >> v[i].i >> v[i].l >> v[i].r;
    }
    vector<camel> v1, v2;
    for (auto c : v) {
      if ((c.l - c.r) > 0)
        v1.push_back(c);
      else
        v2.push_back(c);
    }
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    v = v1;
    bool taken[n + 1];
    memset(taken, 0, sizeof taken);
    long long res = 0;
    for (int i = 0; i < v.size(); i++) {
      if (taken[v[i].i])
        res += v[i].r;
      else {
        taken[v[i].i] = true;
        res += v[i].l;
        v[i].assigned = true;
      }
    }
    int indx = 1;
    for (int i = 0; i < v.size(); i++) {
      while (indx <= n && taken[indx]) indx++;
      if (indx > n) break;
      if (v[i].assigned) continue;
      if (v[i].i >= indx) {
        res -= v[i].r;
        res += v[i].l;
        v[i].assigned = true;
        indx++;
      }
    }
    int cnt = v.size();
    for (int i = 0; i < v2.size(); i++) {
      if (v2[i].i > cnt)
        res += v2[i].l;
      else {
        res += v2[i].r;
        v2[i].assigned = true;
        cnt++;
      }
    }
    for (int i = 0; i < v.size(); i++) {
    }
    for (int i = 0; i < v2.size(); i++) {
    }
    cout << res << endl;
  }
  return 0;
}
