#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
pair<int, int> a[N], b[N];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, cnta = 0, cntb = 0;
    cin >> n;
    int res = 0;
    for (int i = 0; i < n; i++) {
      int first, l, r;
      cin >> first >> l >> r;
      if (first == 0 && l >= r || first == n && r > l) {
        res += min(l, r);
        continue;
      }
      if (l >= r)
        res += r, a[cnta++] = {first, l - r};
      else
        res += l, b[cntb++] = {n - first, r - l};
    }
    sort(a, a + cnta), sort(b, b + cntb);
    priority_queue<int, vector<int>, greater<int> > heap;
    for (int i = 0; i < cnta; i++) {
      heap.push(a[i].second);
      while (heap.size() > a[i].first) heap.pop();
    }
    while (heap.size()) {
      res += heap.top();
      heap.pop();
    }
    for (int i = 0; i < cntb; i++) {
      heap.push(b[i].second);
      while (heap.size() > b[i].first) heap.pop();
    }
    while (heap.size()) {
      res += heap.top();
      heap.pop();
    }
    cout << res << endl;
  }
  return 0;
}
