#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
using vs = vector<string>;
using tll = tuple<ll, ll, ll>;
using vtll = vector<tll>;
const ld PI = 3.1415926535897932;
void in() {}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
  cerr << " " << h;
  if (sizeof...(t) > 0) cerr << " :";
  debug_out(t...);
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> vec) {
  for (size_t i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}
ll ctoi(char c) {
  switch (c) {
    case '0':
      return 0;
    case '1':
      return 1;
    case '2':
      return 2;
    case '3':
      return 3;
    case '4':
      return 4;
    case '5':
      return 5;
    case '6':
      return 6;
    case '7':
      return 7;
    case '8':
      return 8;
    case '9':
      return 9;
    default:
      return 0;
  }
}
bool pairCompare(const pll firstElof, pll secondElof) {
  return firstElof.first > secondElof.first;
}
ll i, j, k, l;
ll N, M, K, H, W, L, X, Y, Z, R, Q;
ll MOD = 1000000007, INF = 1LL << 60, ans = 0, z = 0, o = 1;
vl flag, D;
vll path;
template <typename T>
struct BIT {
  ll n;
  vector<T> bit[2];
  BIT(ll n_) { init(n_); }
  void init(ll n_) {
    n = n_ + 1;
    for (ll p = 0; p < 2; p++) bit[p].assign(n, 0);
  }
  void add_sub(ll p, ll i, T x) {
    for (ll idx = i; idx < n; idx += (idx & -idx)) {
      bit[p][idx] += x;
    }
  }
  void add(ll l, ll r, T x) {
    add_sub(0, l, -x * (l - 1));
    add_sub(0, r, x * (r - 1));
    add_sub(1, l, x);
    add_sub(1, r, -x);
  }
  T sum_sub(ll p, ll i) {
    T s(0);
    for (ll idx = i; idx > 0; idx -= (idx & -idx)) s += bit[p][idx];
    return s;
  }
  T sum(ll i) { return sum_sub(0, i) + sum_sub(1, i) * i; }
  T query(ll l, ll r) { return sum(r - 1) - sum(l - 1); }
  T value(ll i) { return sum(i) - sum(i - 1); }
  ll binarySearch(ll key) {
    ll left = 0;
    ll right = n + 1;
    while (right - left > 1) {
      ll mid = left + (right - left) / 2;
      if (value(mid) >= key)
        right = mid;
      else
        left = mid;
    }
    return right;
  }
};
int main() {
  ll T;
  cin >> T;
  for (k = 0; k < T; k++) {
    ll ans = 0;
    ll N;
    cin >> N;
    BIT<ll> B(N);
    BIT<ll> B2(N);
    vpll L, R;
    for (i = 0; i < N; i++) {
      ll k, l, r;
      cin >> k >> l >> r;
      ans += min(l, r);
      if (l >= r) {
        L.push_back(make_pair(l - r, k));
      } else {
        R.push_back(make_pair(r - l, k));
      }
    }
    sort(L.begin(), L.end());
    reverse(L.begin(), L.end());
    sort(R.begin(), R.end());
    reverse(R.begin(), R.end());
    for (i = 0; i < L.size(); i++) {
      if (B.sum(L[i].second) < L[i].second) {
        ans += L[i].first;
        ll ind = L[i].second;
        while (B.value(ind) != 0) ind--;
        B.add(ind, ind + 1, 1);
      }
    }
    for (i = 0; i < R.size(); i++) {
      if (B2.sum(N) - B2.sum(R[i].second) < N - R[i].second) {
        ans += R[i].first;
        ll ind = R[i].second;
        while (B2.value(ind) != 0) ind++;
        B2.add(ind, ind + 1, 1);
      }
    }
    cout << ans << endl;
    ;
  }
}
