#include <bits/stdc++.h>
using namespace std;
long long int k[200005], l[200005], r[200005];
bool c1(long long int i, long long int j) { return k[i] < k[j]; }
void solve() {
  long long int n, res = 0;
  cin >> n;
  vector<long long int> g1, g2;
  for (long long int i = 1; i <= n; i++) {
    cin >> k[i] >> l[i] >> r[i];
    if (l[i] > r[i])
      g1.push_back(i);
    else
      g2.push_back(i);
  }
  sort(g1.begin(), g1.end(), c1);
  long long int cur = 1;
  multiset<long long int> m;
  for (long long int i = 0; i < long long int(g1.size()); i++) {
    long long int j = i;
    while (j < long long int(g1.size()) && k[g1[i]] == k[g1[j]]) j++;
    while (i < j) {
      if (cur > k[g1[i]]) {
        if ((*m.begin()) < l[g1[i]] - r[g1[i]]) {
          res += l[g1[i]] - (*m.begin());
          m.erase(m.begin());
          m.insert(l[g1[i]] - r[g1[i]]);
        }
      } else {
        m.insert(l[g1[i]] - r[g1[i]]);
        res += l[g1[i]];
        cur++;
      }
      i++;
    }
    i--;
  }
  sort(g2.begin(), g2.end(), c1);
  long long int temp = cur;
  cur = n;
  m.clear();
  for (long long int i = long long int(g2.size()) - 1; i >= 0; i--) {
    long long int j = i;
    while (j >= 0 && k[g2[i]] == k[g2[j]]) {
      k[g2[j]]++;
      j--;
    }
    if (k[g2[i]] == n + 1) {
      while (i > j) {
        res += l[g2[i]];
        i--;
      }
      i++;
      continue;
    }
    while (i > j) {
      if (cur < k[g2[i]] || cur < temp) {
        if ((*m.begin()) < r[g2[i]] - l[g2[i]]) {
          res += r[g2[i]] - (*m.begin());
          m.erase(m.begin());
          m.insert(r[g2[i]] - l[g2[i]]);
        }
      } else {
        m.insert(r[g2[i]] - l[g2[i]]);
        res += r[g2[i]];
        cur--;
      }
      i--;
    }
    i++;
  }
  cout << res << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int qc = 1;
  cin >> qc;
  for (long long int i = 1; i <= qc; i++) solve();
}
