#include <bits/stdc++.h>
using namespace std;
const int INF = 1145141919, MOD = 1e9 + 7;
const int64_t LINF = 8931145141919364364, LMOD = 998244353;
int k[200010], l[200010], r[200010];
void solve() {
  int n;
  cin >> n;
  int64_t ans = 0;
  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>> pqL, pqR;
  for (int i = 0; i < n; ++i) {
    cin >> k[i] >> l[i] >> r[i];
    if (k[i] == n) {
      ans += l[i];
    } else if (l[i] == r[i]) {
      ans += l[i];
    } else if (l[i] < r[i]) {
      pqR.emplace(n - k[i], r[i], i);
    } else {
      pqL.emplace(k[i], l[i], i);
    }
  }
  priority_queue<pair<int, int>> pq;
  for (int i = n; i >= 1; --i) {
    while (not pqL.empty()) {
      auto [a, c, idx] = pqL.top();
      if (a < i) break;
      pqL.pop();
      pq.emplace(c, idx);
    }
    if (not pq.empty()) {
      auto [c, idx] = pq.top();
      pq.pop();
      ans += c;
    }
  }
  while (not pq.empty()) {
    auto [c, idx] = pq.top();
    pq.pop();
    ans += r[idx];
  }
  for (int i = n - 1; i >= 0; --i) {
    while (not pqR.empty()) {
      auto [a, c, idx] = pqR.top();
      if (a < i) break;
      pqR.pop();
      pq.emplace(c, idx);
    }
    if (not pq.empty()) {
      auto [c, idx] = pq.top();
      pq.pop();
      ans += c;
    }
  }
  while (not pq.empty()) {
    auto [c, idx] = pq.top();
    pq.pop();
    ans += l[idx];
  }
  cout << ans << '\n';
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
