#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, T;
int score(vector<pair<int, int>> &x) {
  long long res = 0;
  sort(x.begin(), x.end());
  priority_queue<int> q;
  int idx = x.size() - 1;
  for (int i = max(x[idx].first, 0); i; --i) {
    while (idx >= 0 && x[idx].first == i) q.push(x[idx--].second);
    if (!q.empty()) {
      res += q.top();
      q.pop();
    }
  }
  return res;
}
int main() {
  cin >> T;
  while (T--) {
    cin >> n;
    long long ans = 0;
    vector<pair<int, int>> L, R;
    for (int i = 0, first, l, r; i < n; ++i) {
      cin >> first >> l >> r;
      if (l <= r) {
        ans += l;
        R.push_back({n - first, r - l});
      } else {
        ans += r;
        L.push_back({first, l - r});
      }
    }
    ans += score(L);
    ans += score(R);
    printf("%lld\n", ans);
  }
  return 0;
}
