#include <bits/stdc++.h>
constexpr long long MAXCOMB = 202020;
constexpr long long MOD = 1000000007;
constexpr long double EPS = 1e-9;
using namespace std;
using ll = long long;
using ld = long double;
constexpr ll INF2 = 1000000000000000037;
constexpr ll INF = 1000000007;
constexpr ld PI = 3.1415926535897932384626433832795028L;
using P = pair<ll, ll>;
using TP = tuple<ll, ll, ll>;
ll intpow(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1) ans *= a;
    a *= a;
    b /= 2;
  }
  return ans;
}
ll overmul(ll a, ll b) { return (b ? (a > INF2 / b ? INF2 : a * b) : 0LL); }
template <class T = ll>
using v = vector<T>;
template <class T = ll>
using vv = vector<vector<T>>;
template <class T = ll>
using vvv = vector<vector<vector<T>>>;
template <class T>
bool maxi(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool mini(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
T sumer(const vector<T> &a) {
  return accumulate((a).begin(), (a).end(), (T)0);
}
template <class T>
T miner(const vector<T> &a) {
  return *min_element((a).begin(), (a).end());
}
template <class T>
T maxer(const vector<T> &a) {
  return *max_element((a).begin(), (a).end());
}
template <class T>
T ceil(T a, T b) {
  return a / b + !!(a % b);
}
template <class T>
ll bs(ll ok, ll ng, T checker) {
  while (abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    (checker(mid) ? ok : ng) = mid;
  }
  return ok;
}
template <class T>
ld bs2(ld ok, ld ng, T checker) {
  for (ll i = 0; i < 180; i++) {
    ld mid = (ok + ng) / 2;
    (checker(mid) ? ok : ng) = mid;
  }
  return ok;
}
template <class T>
ll UNIQ(vector<T> &a) {
  sort((a).begin(), (a).end());
  a.erase(unique((a).begin(), (a).end()), a.end());
  return a.size();
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &pe) {
  os << pe.first << " " << pe.second;
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const v<T> &ve) {
  for (ll i = 0; i < ve.size(); i++) os << (i ? " " : "") << ve[i];
  return os;
}
ll topbit(ll a) { return a == 0 ? -1 : 63 - __builtin_clzll(a); }
ll botbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); }
ll popcount(ll a) { return __builtin_popcountll(a); }
template <int mod>
struct ModInt {
  ll x;
  ModInt() : x(0) {}
  ModInt(ll y)
      : x(y >= 0 ? (y < mod ? y : y % mod) : (mod - (-y) % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inv();
    return *this;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  bool operator<(const ModInt &p) const { return x < p.x; }
  ModInt operator+(const ll &q) const { return ModInt(*this) += (ModInt)q; }
  ModInt operator-(const ll &q) const { return ModInt(*this) -= (ModInt)q; }
  ModInt operator*(const ll &q) const { return ModInt(*this) *= (ModInt)q; }
  ModInt operator/(const ll &q) const { return ModInt(*this) /= (ModInt)q; }
  bool operator==(const ll &q) const { return x == q; }
  bool operator!=(const ll &q) const { return x != q; }
  ModInt operator++() {
    if (++x == mod) x = 0;
    return *this;
  }
  ModInt operator--() {
    x = (x == 0 ? mod - 1 : x - 1);
    return *this;
  }
  ModInt operator++(int) {
    const ModInt res(*this);
    ++*this;
    return res;
  }
  ModInt operator--(int) {
    const ModInt res(*this);
    --*this;
    return res;
  }
  ModInt inv() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  ModInt pow(ll n, bool inv = false) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    if (inv) ret = ret.inv();
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
  friend istream &operator>>(istream &is, ModInt &a) {
    ll t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
};
using mint = ModInt<MOD>;
mint mpow(ll x, ll n, bool inv = false) {
  mint ret(1), mul(x);
  while (n > 0) {
    if (n & 1) ret *= mul;
    mul *= mul;
    n >>= 1;
  }
  if (inv) ret = ret.inv();
  return ret;
}
mint fac[MAXCOMB], finv[MAXCOMB], inv[MAXCOMB];
void cinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAXCOMB; i++) {
    fac[i] = fac[i - 1] * i;
    inv[i] = inv[MOD % i] * -(MOD / i);
    finv[i] = finv[i - 1] * inv[i];
  }
}
mint cmb(int n, int k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * finv[k] * finv[n - k];
}
void Main() {
  ll N;
  cin >> N;
  v<> K(N), L(N), R(N);
  for (ll i = 0; i < N; i++) cin >> K[i] >> L[i] >> R[i];
  v<> isL(N);
  ll lnum = 0, rnum = 0;
  for (ll i = 0; i < N; i++) {
    if (L[i] > R[i]) isL[i] = 1, ++lnum;
    if (L[i] == R[i]) isL[i] = 0;
    if (L[i] < R[i]) isL[i] = -1, ++rnum;
  }
  ll ans = 0;
  vector<P> pes1, pes2;
  for (ll i = 0; i < N; i++) {
    if (isL[i] == 1) {
      pes1.emplace_back(R[i] - L[i], min(K[i] - 1, lnum));
      ans += R[i];
    }
    if (isL[i] == 0) {
      ans += L[i];
    }
    if (isL[i] == -1) {
      pes2.emplace_back(L[i] - R[i], min(N - K[i] - 1, rnum));
      ans += L[i];
    }
  }
  sort((pes1).rbegin(), (pes1).rend());
  sort((pes2).rbegin(), (pes2).rend());
  set<ll> se1, se2;
  for (ll i = 0; i < lnum; i++) {
    se1.emplace(i);
  }
  for (ll i = 0; i < rnum; i++) {
    se2.emplace(i);
  }
  for (ll i = 0; i < lnum; i++) {
    auto [a, b] = pes1[i];
    auto x = prev(se1.lower_bound(b));
    if (x == se1.begin()) continue;
    se1.erase(x);
    ans += b;
  }
  for (ll i = 0; i < rnum; i++) {
    auto [a, b] = pes2[i];
    auto x = prev(se2.lower_bound(b));
    if (x == se2.begin()) continue;
    se2.erase(x);
    ans += b;
  }
  cout << ans << "\n";
}
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll Qkai;
  cin >> Qkai;
  for (ll QQ = 0; QQ < Qkai; QQ++) Main();
}
