#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " , ";
  __f(comma + 1, args...);
}
const long long SQRTN = 1003;
const long long LOGN = 22;
const long double PI = acos(-1);
const long long INF = 1000000000;
const long long MOD = 1000000007;
const long long FMOD = 998244353;
const long double eps = 1e-9;
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
T gcd(T a, T b) {
  return (b ? __gcd(a, b) : a);
}
template <typename T>
T lcm(T a, T b) {
  return (a * (b / gcd(a, b)));
}
long long add(long long a, long long b, long long c) {
  long long res = a + b;
  return (res >= c ? res - c : res);
}
long long mod_neg(long long a, long long b, long long c) {
  long long res;
  if (abs(a - b) < c)
    res = a - b;
  else
    res = (a - b) % c;
  return (res < 0 ? res + c : res);
}
long long mul(long long a, long long b, long long c) {
  long long res = (long long)a * b;
  return (res >= c ? res % c : res);
}
long long mulmod(long long a, long long b, long long m) {
  long long q = (long long)(((long LD)a * (long LD)b) / (long LD)m);
  long long r = a * b - q * m;
  if (r > m) r %= m;
  if (r < 0) r += m;
  return r;
}
template <typename T>
T expo(T e, T n) {
  T x = 1, p = e;
  while (n) {
    if (n & 1) x = x * p;
    p = p * p;
    n >>= 1;
  }
  return x;
}
template <typename T>
T power(T e, T n, T m) {
  T x = 1, p = e;
  while (n) {
    if (n & 1) x = mul(x, p, m);
    p = mul(p, p, m);
    n >>= 1;
  }
  return x;
}
template <typename T>
T extended_euclid(T a, T b, T& x, T& y) {
  T xx = 0, yy = 1;
  y = 0;
  x = 1;
  while (b) {
    T q = a / b, t = b;
    b = a % b;
    a = t;
    t = xx;
    xx = x - q * xx;
    x = t;
    t = yy;
    yy = y - q * yy;
    y = t;
  }
  return a;
}
template <typename T>
T mod_inverse(T a, T n) {
  T x, y, z = 0;
  T d = extended_euclid(a, n, x, y);
  return (d > 1 ? -1 : mod_neg(x, z, n));
}
void solve();
signed main() {
  cout << fixed << setprecision(10);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  for (long long testcase = 1; testcase < t + 1; testcase++) {
    solve();
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
std::vector<long long> del1[200001], del2[200001];
void solve() {
  long long n, ans = 0;
  cin >> n;
  long long k, l, r;
  for (long long i = 0; i < n; i++) {
    del1[i + 1].clear();
    del2[i + 1].clear();
  }
  for (long long i = 0; i < n; i++) {
    cin >> k >> l >> r;
    if (l > r)
      del1[k].push_back(l - r);
    else
      del2[k].push_back(r - l);
    ans += r;
  }
  priority_queue<long long, vector<long long>, greater<long long> > pq1;
  for (long long i = 1; i <= n; i++) {
    for (long long d : del1[i]) {
      pq1.push(d);
    }
    while (pq1.size() > i) {
      pq1.pop();
    }
  }
  long long used = pq1.size();
  priority_queue<long long> pq2;
  for (long long i = 1; i <= used; i++) {
    for (long long d : del2[i]) pq2.push(0);
  }
  for (long long i = used + 1; i <= n; i++) {
    for (long long d : del2[i]) {
      pq2.push(d);
    }
    while (pq2.size() + used > i and pq2.top() != 0) {
      pq2.pop();
    }
  }
  while (!pq1.empty()) {
    ans += pq1.top();
    pq1.pop();
  }
  while (!pq2.empty()) {
    ans -= pq2.top();
    pq2.pop();
  }
  cout << ans << endl;
}
