#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mxn = 2e5 + 5;
int n;
array<int, 3> a[mxn];
void solve() {
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> a[i][0] >> a[i][1] >> a[i][2];
  sort(a, a + n);
  set<int> ff, fb;
  for (int i = 0; i < n; ++i) {
    ff.insert(i);
    fb.insert(i);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int k = a[i][0], l = a[i][1], r = a[i][2];
    if (l > r) {
      auto lb = ff.upper_bound(k - 1);
      if (lb == ff.begin()) {
        ff.erase(--ff.end());
        ans += r;
      } else {
        --lb;
        ff.erase(lb);
        ans += l;
      }
    } else {
      auto lb = fb.lower_bound(k);
      if (lb == fb.end()) {
        fb.erase(fb.begin());
        ans += l;
      } else {
        fb.erase(lb);
        ans += r;
      }
    }
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T;
  cin >> T;
  for (int i = 0; i < T; ++i) solve();
  return 0;
}
