#include <bits/stdc++.h>
using namespace std;
using vi = vector<long long>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using vp = vector<pair<long long, long long> >;
using vvp = vector<vp>;
using vb = vector<bool>;
using vvb = vector<vb>;
const long long inf = 1001001001001001001;
const long long INF = 1001001001;
const long long mod = 1000000007;
const double eps = 1e-10;
template <class T>
bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
void out(T a) {
  cout << a << '\n';
}
template <class T>
void outp(T a) {
  cout << '(' << a.first << ',' << a.second << ')' << '\n';
}
template <class T>
void outvp(T v) {
  for (long long i = 0; i < (long long)(v.size()); i++)
    cout << '(' << v[i].first << ',' << v[i].second << ')';
  cout << '\n';
}
template <class T>
void outvvp(T v) {
  for (long long i = 0; i < (long long)(v.size()); i++) outvp(v[i]);
}
template <class T>
void outv(T v) {
  for (long long i = 0; i < (long long)(v.size()); i++) {
    if (i) cout << ' ';
    cout << v[i];
  }
  cout << '\n';
}
template <class T>
void outvv(T v) {
  for (long long i = 0; i < (long long)(v.size()); i++) outv(v[i]);
}
template <class T>
bool isin(T x, T l, T r) {
  return (l) <= (x) && (x) <= (r);
}
template <class T>
void yesno(T b) {
  if (b)
    out("yes");
  else
    out("no");
}
template <class T>
void YesNo(T b) {
  if (b)
    out("Yes");
  else
    out("No");
}
template <class T>
void YESNO(T b) {
  if (b)
    out("YES");
  else
    out("NO");
}
template <class T>
void noyes(T b) {
  if (b)
    out("no");
  else
    out("yes");
}
template <class T>
void NoYes(T b) {
  if (b)
    out("No");
  else
    out("Yes");
}
template <class T>
void NOYES(T b) {
  if (b)
    out("NO");
  else
    out("YES");
}
void outs(long long a, long long b) {
  if (a >= inf - 100)
    out(b);
  else
    out(a);
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long modpow(long long a, long long b) {
  long long res = 1;
  a %= mod;
  while (b) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
int main() {
  long long t;
  cin >> t;
  for (long long tt = 0; tt < (long long)(t); tt++) {
    long long n;
    cin >> n;
    long long sum = 0;
    vp a, b;
    for (long long i = 0; i < (long long)(n); i++) {
      long long k, l, r;
      cin >> k >> l >> r;
      k--;
      if (l > r) a.emplace_back(l - r, k);
      if (r > l) b.emplace_back(r - l, n - k - 2);
      sum += min(l, r);
    }
    sort(a.begin(), a.end());
    reverse(b.begin(), b.end());
    sort(b.begin(), b.end());
    reverse(b.begin(), b.end());
    set<long long> s;
    for (long long i = 0; i < (long long)(a.size()); i++) s.insert(i);
    for (long long i = 0; i < (long long)(a.size()); i++) {
      long long x = a[i].first, k = a[i].second;
      auto itr = s.lower_bound(k + 1);
      if (itr == s.begin()) continue;
      itr--;
      s.erase(itr);
      sum += x;
    }
    set<long long> multiset<long long>;
    for (long long i = 0; i < (long long)(b.size()); i++)
      multiset<long long>.insert(i);
    for (long long i = 0; i < (long long)(b.size()); i++) {
      long long x = b[i].first, k = b[i].second;
      auto itr = multiset<long long>.lower_bound(k + 1);
      if (itr == multiset<long long>.begin()) continue;
      itr--;
      multiset<long long>.erase(itr);
      sum += x;
    }
    out(sum);
  }
}
