#include <bits/stdc++.h>
using namespace std;
struct fast_ios {
  fast_ios() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &x : v) is >> x;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &v) {
  for (long long i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i != v.size() - 1) cout << endl;
  };
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
  cout << '(' << p.first << ',' << p.second << ')';
  return os;
}
template <typename T>
void Out(T x) {
  cout << x << endl;
}
template <typename T1, typename T2>
void chOut(bool f, T1 y, T2 n) {
  if (f)
    Out(y);
  else
    Out(n);
}
using vec = vector<long long>;
using mat = vector<vec>;
using Pii = pair<long long, long long>;
using v_bool = vector<bool>;
using v_Pii = vector<Pii>;
const long long mod = 1000000007;
long long calc(v_Pii v) {
  long long n = ((long long)v.size());
  long long ans = 0;
  set<long long> s;
  for (long long i = (0); i < (n); i++) s.insert(i);
  s.insert(-1);
  for (long long i = (0); i < (n); i++) {
    long long d = v[i].first, k = v[i].second;
    auto it = s.upper_bound(k);
    it--;
    if (*it != -1) {
      s.erase(it);
      ans += d;
    }
  }
  return ans;
}
signed main() {
  long long T;
  cin >> T;
  for (long long _ = (0); _ < (T); _++) {
    long long N;
    cin >> N;
    vec K(N), L(N), R(N);
    long long ans = 0;
    v_Pii vp, vm;
    for (long long i = (0); i < (N); i++) {
      cin >> K[i] >> L[i] >> R[i];
      K[i]--;
      if (K[i] == N - 1) {
        ans += L[i];
      } else if (L[i] >= R[i]) {
        ans += R[i];
        vp.push_back(Pii(L[i] - R[i], K[i]));
      } else {
        ans += L[i];
        vm.push_back(Pii(R[i] - L[i], N - 2 - K[i]));
      }
    }
    sort(vp.begin(), vp.end());
    sort(vm.begin(), vm.end());
    ans += calc(vp) + calc(vm);
    Out(ans);
  }
  return 0;
}
