#include <bits/stdc++.h>
using namespace std;
template <typename T>
void prarr(T a) {
  cerr << "[ ";
  for (auto i : a) {
    cerr << i << " ";
  }
  cerr << "]"
       << "\n";
}
template <typename Arg1>
void _er(const char* name, Arg1&& arg1) {
  cerr << "[" << name << " : " << arg1 << "]"
       << "\n";
}
template <typename Arg1, typename... Args>
void _er(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr << "[";
  cerr.write(names, comma - names) << " : " << arg1 << "] ";
  _er(comma + 2, args...);
}
tuple<long long int, long long int, long long int> A[200005];
vector<tuple<long long int, long long int, long long int> > C;
vector<pair<long long int, long long int> > B;
set<long long int> g;
set<pair<long long int, long long int> > curr;
long long int ans[200005];
void solve() {
  g.clear();
  curr.clear();
  B.clear();
  C.clear();
  long long int N;
  cin >> N;
  for (long long int i = 1; i <= N; i++) {
    ans[i] = 0;
  }
  long long int ma = 0;
  for (long long int i = 1; i <= N; i++) {
    long long int k, l, r;
    cin >> k >> l >> r;
    A[i] = {k, l, r};
    ma = max(ma, k);
    g.insert(i);
    if ((l - r) > 0) {
      C.push_back({(l - r), k, i});
    } else {
      B.push_back({(r - l), i});
    }
  }
  sort(C.begin(), C.end());
  reverse(C.begin(), C.end());
  for (auto it : C) {
    long long int diff = get<0>(it);
    long long int k = get<1>(it);
    long long int ind = get<2>(it);
    auto jt = g.upper_bound(k);
    jt--;
    if (*jt > k) {
      B.push_back({-1 * diff, ind});
      continue;
    }
    curr.insert({*jt, ind});
    g.erase(jt);
  }
  g.clear();
  for (long long int i = 1; i <= N; i++) {
    g.insert(i);
  }
  long long int ans_ind = 1;
  for (auto it : curr) {
    ans[ans_ind] = it.second;
    g.erase(ans_ind);
    ans_ind++;
  }
  sort(B.begin(), B.end());
  reverse(B.begin(), B.end());
  for (auto it : B) {
    long long int ind = it.second;
    long long int k = get<0>(A[ind]);
    auto jt = g.upper_bound(k);
    if (jt != g.end()) {
      ans[*jt] = ind;
      g.erase(jt);
    } else {
      jt = g.begin();
      ans[*jt] = ind;
      g.erase(jt);
    }
  }
  assert(g.size() == 0);
  long long int sum = 0;
  set<long long int> ge;
  for (long long int i = 1; i <= N; i++) {
    long long int ind = ans[i];
    assert(ind >= 1 && ind <= N);
    ge.insert(ind);
    long long int k = get<0>(A[ind]);
    long long int l = get<1>(A[ind]);
    long long int r = get<2>(A[ind]);
    if (i <= k) {
      sum += l;
    } else {
      sum += r;
    }
  }
  assert(g.size() == 0 && ge.size() == N);
  cout << sum << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
