#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
A cvt(B x) {
  stringstream ss;
  ss << x;
  A y;
  ss >> y;
  return y;
}
int main() {
  int tests;
  scanf("%d", &tests);
  for (int test = 0; test < (int)(tests); test++) {
    int n;
    scanf("%d", &n);
    vector<int> k(n), l(n), r(n);
    vector<vector<int> > f, b;
    long long sc = 0;
    for (int i = 0; i < (int)(n); i++) {
      scanf("%d %d %d", &k[i], &l[i], &r[i]);
      sc += min(l[i], r[i]);
      if (l[i] > r[i]) f.push_back({k[i], l[i] - r[i]});
      if (l[i] < r[i]) b.push_back({k[i], r[i] - l[i]});
    }
    int j;
    sort((f).begin(), (f).end());
    reverse((f).begin(), (f).end());
    priority_queue<int> pqf;
    j = 0;
    for (int i = (int)f.size() - 1; i >= 0; i--) {
      while (j < f.size() && f[j][0] >= i) {
        pqf.push(f[j][1]);
        j++;
      }
      if (!pqf.empty()) {
        sc += pqf.top();
        pqf.pop();
      }
    }
    sort((b).begin(), (b).end());
    priority_queue<int> pqb;
    j = 0;
    for (int i = f.size(); i < n; i++) {
      while (j < b.size() && b[j][0] <= i) {
        pqb.push(b[j][1]);
        j++;
      }
      if (!pqb.empty()) {
        sc += pqb.top();
        pqb.pop();
      }
    }
    cout << sc << '\n';
  }
  return 0;
}
