#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
i64 solve() {
  i64 N;
  cin >> N;
  vector<tuple<i64, i64, i64, i64>> lefters;
  vector<tuple<i64, i64, i64, i64>> righters;
  vector<tuple<i64, i64, i64>> samers;
  for (int i = 0; (i64)(i) < (i64)(N); ++i) {
    i64 k, l, r;
    cin >> k >> l >> r;
    k--;
    if (l > r) {
      lefters.emplace_back(l - r, k, l, r);
    } else if (r > l) {
      righters.emplace_back(r - r, k, l, r);
    } else {
      samers.emplace_back(k, l, r);
    }
  }
  sort(lefters.rbegin(), lefters.rend());
  sort(righters.rbegin(), righters.rend());
  set<int> available;
  for (int i = 0; (i64)(i) < (i64)(N); ++i) available.insert(i);
  available.insert(-1);
  available.insert(N);
  vector<i64> delight(N, -1);
  vector<i64> rest;
  for (int i = 0; (i64)(i) < (i64)(lefters.size()); ++i) {
    auto [gain, k, l, r] = lefters[i];
    i64 j = min<i64>(k, lefters.size() + samers.size() - 1);
    auto it = available.lower_bound(j);
    assert(it != available.end());
    if (*it > j) {
      --it;
      if (*it == -1) {
        ;
        rest.push_back(r);
        continue;
      }
    }
    delight[*it] = l;
    available.erase(it);
  }
  for (int i = 0; (i64)(i) < (i64)(righters.size()); ++i) {
    auto [gain, k, l, r] = righters[i];
    i64 j = max<i64>(k + 1, lefters.size());
    auto it = available.lower_bound(j);
    assert(it != available.end());
    if (*it == N) {
      ;
      rest.push_back(l);
      continue;
    }
    delight[*it] = r;
    available.erase(it);
  }
  i64 ans = 0;
  int vacant = 0;
  for (auto x : delight) {
    if (x >= 0)
      ans += x;
    else
      vacant++;
  };
  for (auto x : rest) {
    ans += x;
    --vacant;
  }
  for (auto [gain, l, r] : samers) {
    ans += l;
    --vacant;
  };
  assert(vacant == 0);
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T--) {
    cout << solve() << '\n';
  }
}
