#include <bits/stdc++.h>
using namespace std;
template <class T = long long>
using V = vector<T>;
template <class T = long long>
using VV = V<V<T>>;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
  if (b) {
    cout << -1 << '\n';
    exit(0);
  }
}
template <class T>
inline void fin(bool b = true, T e = 0) {
  if (b) {
    cout << e << '\n';
    exit(0);
  }
}
template <class T>
T Roundup_div(T x, T y) {
  return (x + (y - 1)) / y;
}
const long long INF = 1e18;
template <typename T>
T pow(T a, long long n, T e = 1) {
  T ret = e;
  while (n) {
    if (n & 1) ret *= a;
    a *= a;
    n >>= 1;
  }
  return ret;
}
long long n;
void solve() {
  cin >> n;
  long long res = 0;
  VV<> lef(n), rig(n);
  for (long long i = (0); i < (n); ++i) {
    long long k, l, r;
    cin >> k >> l >> r;
    k--;
    res += min(l, r);
    if (l > r) lef[k].push_back(l - r);
    if (l < r) rig[k].push_back(r - l);
  }
  priority_queue<long long, vector<long long>, greater<long long>> lq, rq;
  for (long long i = (0); i < (n); ++i) {
    for (auto v : lef[i]) lq.push(v);
    while (((long long)(lq).size()) > i + 1) lq.pop();
  }
  for (long long i = (n - 1 - 1); i >= (0); --i) {
    for (auto v : rig[i]) rq.push(v);
    while (((long long)(rq).size()) > n - i) rq.pop();
  }
  while (((long long)(lq).size())) {
    res += lq.top();
    lq.pop();
  }
  while (((long long)(rq).size())) {
    res += rq.top();
    rq.pop();
  }
  cout << res << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) solve();
}
