#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
struct Node {
  int val, k;
  bool operator<(const Node& rhs) const { return k < rhs.k; }
} a[maxn], b[maxn];
int n, cnta, cntb;
long long ans;
std::priority_queue<int, std::vector<int>, std::greater<int> > q;
void solve() {
  scanf("%d", &n);
  int k, l, r;
  for (int i = 1; i <= n; ++i) {
    cin >> k >> l >> r;
    if (l >= r) {
      ans += r;
      a[++cnta] = Node{l - r, k};
    } else {
      ans += l;
      b[++cntb] = Node{l - r, n - k};
    }
  }
  sort(a + 1, a + 1 + cnta);
  sort(b + 1, b + 1 + cntb);
  for (int i = 1; i <= cnta; ++i) {
    if (a[i].k == 0) continue;
    if (q.size() < a[i].k) {
      q.push(a[i].val);
    } else if (q.size() == a[i].k) {
      if (a[i].val > q.top()) {
        q.pop();
        q.push(a[i].val);
      }
    }
  }
  while (!q.empty()) {
    ans += q.top();
    q.pop();
  }
  for (int i = 1; i <= cntb; ++i) {
    if (b[i].k == 0) continue;
    if (q.size() < b[i].k) {
      q.push(b[i].val);
    } else if (q.size() == b[i].k) {
      if (b[i].val > q.top()) {
        q.pop();
        q.push(b[i].val);
      }
    }
  }
  while (!q.empty()) {
    ans += q.top();
    q.pop();
  }
  cout << ans << endl;
  return;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
