#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long powmod(long long x, long long y) {
  if (y == 0) return 1;
  long long p = powmod(x, y / 2) % 998244353;
  p = (p * p) % 998244353;
  return (y % 2 == 0) ? p : (x * p) % 998244353;
}
long long inverse(long long a) { return powmod(a, 998244353 - 2); }
const long long limbin = 500001;
long long fact[limbin], factinv[limbin], coefficientflag = 0;
void Util_nCr() {
  fact[0] = 1, factinv[0] = 1;
  for (long long i = 1; i < limbin; i++) {
    fact[i] = (fact[i - 1] * i) % 998244353;
    factinv[i] = (factinv[i - 1] * inverse(i)) % 998244353;
  }
}
long long nCr(long long n, long long r) {
  if (!coefficientflag) coefficientflag = 1, Util_nCr();
  if (n < r) return 0;
  return ((fact[n] * inverse(fact[n - r]) % 998244353 * inverse(fact[r])) %
          998244353);
}
void solve();
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  for (long long i = 0; i < t; i++) {
    solve();
  }
}
bool comp1(pair<long long, long long> l, pair<long long, long long> m) {
  if (l.second == m.second) return l.first > m.first;
  return l.second < m.second;
}
bool comp2(pair<long long, long long> l, pair<long long, long long> m) {
  if (l.second == m.second) return l.first < m.first;
  return l.second > m.second;
}
void solve() {
  long long n;
  cin >> n;
  long long sum = 0;
  vector<pair<long long, long long> > pos, neg;
  for (long long i = 0; i < n; i++) {
    long long l, r, k;
    cin >> k >> l >> r;
    sum += r;
    if (l >= r)
      pos.push_back({l - r, k});
    else
      neg.push_back({l - r, k});
  }
  sort(pos.begin(), pos.end(), comp1);
  sort(neg.begin(), neg.end(), comp2);
  long long start = 0;
  for (auto i : pos) {
    if (start < i.second)
      start++, sum += i.first;
    else
      continue;
  }
  long long end = n;
  for (auto i : neg) {
    if (end > i.second)
      end--;
    else
      sum += i.first;
  }
  cout << sum << '\n';
}
