#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using mii = map<int, int>;
using pqll = priority_queue<long long>;
using pqllg = priority_queue<long long, vector<long long>, greater<long long>>;
using mll = map<long long, long long>;
using pll = pair<long long, long long>;
using sll = set<long long>;
using vpll = vector<pair<long long, long long>>;
using mlv = map<long long, vector<long long>>;
long long divup(long long a, long long b);
long long kaijou(long long i);
long long P(long long n, long long k);
long long C(long long n, long long k);
long long GCD(long long a, long long b);
long long LCM(long long a, long long b);
bool prime(long long N);
double distance(vector<long long> p, vector<long long> q, long long n);
void press(vector<long long> &v);
void ranking(vector<long long> &v);
void erase(vector<long long> &v, long long i);
void unique(vector<long long> &v);
void printv(vector<long long> v);
vector<long long> keta(ll x);
long long modpow(long long a, long long n, long long mod);
long long modinv(long long a, long long mod);
vector<long long> inputv(long long n);
vector<long long> yakusuu(int n);
map<long long, long long> soinsuu(long long n);
vector<vector<long long>> maze(long long i, long long j, vector<string> &s);
vector<long long> eratos(long long n);
set<long long> eraset(long long n);
long long divup(long long a, long long b) {
  long long x = abs(a);
  long long y = abs(b);
  long long z = (x + y - 1) / y;
  if ((a < 0 && b > 0) || (a > 0 && b < 0))
    return -z;
  else if (a == 0)
    return 0;
  else
    return z;
}
long long kaijou(long long i) {
  if (i == 0) return 1;
  long long j = 1;
  for (long long k = 1; k <= i; k++) {
    j *= k;
  }
  return j;
}
long long P(long long n, long long k) {
  if (n < k) return 0;
  long long y = 1;
  for (long long i = 0; i < k; i++) {
    y *= (n - i);
  }
  return y;
}
long long C(long long n, long long k) {
  if (n < k) return 0;
  return P(n, k) / kaijou(k);
}
long long GCD(long long a, long long b) {
  if (a < b) swap(a, b);
  long long d = a % b;
  if (d == 0) {
    return b;
  }
  return GCD(b, d);
}
long long LCM(long long a, long long b) { return (a / GCD(a, b)) * b; }
bool prime(long long N) {
  if (N == 1) {
    return false;
  }
  if (N < 0) return false;
  long long p = sqrt(N);
  for (long long i = 2; i <= p; i++) {
    if (N % i == 0) {
      return false;
    }
  }
  return true;
}
double distance(vector<long long> p, vector<long long> q, long long n) {
  double x = 0;
  for (long long i = 0; i < n; i++) {
    x += pow((p.at(i) - q.at(i)), 2);
  }
  return sqrt(x);
}
void press(vector<long long> &v) {
  long long n = v.size();
  vector<long long> w(n);
  map<long long, long long> m;
  for (auto &p : v) {
    m[p] = 0;
  }
  long long i = 0;
  for (auto &p : m) {
    p.second = i;
    i++;
  }
  for (long long i = 0; i < n; i++) {
    w.at(i) = m[v.at(i)];
  }
  v = w;
  return;
}
void ranking(vector<long long> &v) {
  long long n = v.size();
  map<long long, long long> m;
  long long i;
  for (i = 0; i < n; i++) {
    m[v.at(i)] = i;
  }
  vector<long long> w(n);
  i = 0;
  for (auto &p : m) {
    v.at(i) = p.second;
    i++;
  }
  return;
}
void erase(vector<long long> &v, long long i) {
  long long n = v.size();
  if (i > n - 1) return;
  for (long long j = i; j < n - 1; j++) {
    v.at(j) = v.at(j + 1);
  }
  v.pop_back();
  return;
}
void unique(vector<long long> &v) {
  long long n = v.size();
  set<long long> s;
  long long i = 0;
  while (i < n) {
    if (s.count(v.at(i))) {
      erase(v, i);
      n--;
    } else {
      s.insert(v.at(i));
      i++;
    }
  }
  return;
}
void printv(vector<long long> v) {
  cout << "{ ";
  for (auto &p : v) {
    cout << p << ",";
  }
  cout << "}" << endl;
}
vector<ll> keta(ll x) {
  if (x == 0) return {0};
  ll n = log10(x) + 1;
  vll w(n, 0);
  for (ll i = 0; i < n; i++) {
    ll p;
    p = x % 10;
    x = x / 10;
    w[n - 1 - i] = p;
  }
  return w;
}
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }
vector<long long> inputv(long long n) {
  vector<long long> v(n);
  for (long long i = 0; i < n; i++) {
    cin >> v[i];
  }
  return v;
}
vector<long long> yakusuu(long long n) {
  vector<long long> ret;
  for (long long i = 1; i <= sqrt(n); ++i) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) {
        ret.push_back(n / i);
      }
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}
map<long long, long long> soinsuu(long long n) {
  map<long long, long long> m;
  long long p = sqrt(n);
  while (n % 2 == 0) {
    n /= 2;
    if (m.count(2)) {
      m[2]++;
    } else {
      m[2] = 1;
    }
  }
  for (long long i = 3; i * i <= n; i += 2) {
    while (n % i == 0) {
      n /= i;
      if (m.count(i)) {
        m[i]++;
      } else {
        m[i] = 1;
      }
    }
  }
  if (n != 1) m[n] = 1;
  return m;
}
vector<vector<long long>> maze(ll i, ll j, vector<string> &s) {
  ll h = s.size();
  ll w = s[0].size();
  queue<vector<long long>> q;
  vector<vector<long long>> dis(h, vll(w, -1));
  q.push({i, j});
  dis[i][j] = 0;
  while (!q.empty()) {
    auto v = q.front();
    q.pop();
    if (v[0] > 0 && s[v[0] - 1][v[1]] == '.' && dis[v[0] - 1][v[1]] == -1) {
      dis[v[0] - 1][v[1]] = dis[v[0]][v[1]] + 1;
      q.push({v[0] - 1, v[1]});
    }
    if (v[1] > 0 && s[v[0]][v[1] - 1] == '.' && dis[v[0]][v[1] - 1] == -1) {
      dis[v[0]][v[1] - 1] = dis[v[0]][v[1]] + 1;
      q.push({v[0], v[1] - 1});
    }
    if (v[0] < h - 1 && s[v[0] + 1][v[1]] == '.' && dis[v[0] + 1][v[1]] == -1) {
      dis[v[0] + 1][v[1]] = dis[v[0]][v[1]] + 1;
      q.push({v[0] + 1, v[1]});
    }
    if (v[1] < w - 1 && s[v[0]][v[1] + 1] == '.' && dis[v[0]][v[1] + 1] == -1) {
      dis[v[0]][v[1] + 1] = dis[v[0]][v[1]] + 1;
      q.push({v[0], v[1] + 1});
    }
  }
  return dis;
}
long long modC(long long n, long long k, long long mod) {
  if (n < k) return 0;
  long long p = 1, q = 1;
  for (long long i = 0; i < k; i++) {
    p = p * (n - i) % mod;
    q = q * (i + 1) % mod;
  }
  return p * modinv(q, mod) % mod;
}
long long POW(long long a, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) res = res * a;
    a = a * a;
    n >>= 1;
  }
  return res;
}
vector<long long> eratos(long long n) {
  if (n < 2) return {};
  vll v(n - 1);
  for (long long i = 0; i < n - 1; i++) {
    v[i] = i + 2;
  }
  ll i = 0;
  while (i < n - 1) {
    ll p = v[i];
    for (ll j = i + 1; j < n - 1; j++) {
      if (v[j] % p == 0) {
        v.erase(v.begin() + j);
        n--;
      }
    }
    i++;
  }
  v.resize(n - 1);
  return v;
}
set<long long> eraset(long long n) {
  set<long long> s;
  vll v = eratos(n);
  for (auto &t : v) {
    s.insert(t);
  }
  return s;
}
vll line(ll x1, ll y1, ll x2, ll y2) {
  vector<ll> v(3);
  v[0] = y1 - y2;
  v[1] = x2 - x1;
  v[2] = -x1 * (y1 - y2) + y1 * (x1 - x2);
  return v;
}
double dis(vll v, ll x, ll y) {
  double s = sqrt(v[0] * v[0] + v[1] * v[1]);
  return (double)abs(v[0] * x + v[1] * y + v[2]) / s;
}
void maxin(ll &a, ll b) {
  a = max(a, b);
  return;
}
void minin(ll &a, ll b) {
  a = min(a, b);
  return;
}
map<long long, long long> countv(vll v) {
  map<long long, long long> m;
  for (auto &g : v) {
    if (m.count(g))
      m[g]++;
    else
      m[g] = 1;
  }
  return m;
}
const ll MAX = 510000;
long long fac[MAX], finv[MAX], inv[MAX];
void cominit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}
long long commod(ll n, ll k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
long long pmod(ll n, ll k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * finv[n - k] % mod;
}
long long next_combination(long long sub) {
  long long x = sub & -sub, y = sub + x;
  return (((sub & ~y) / x) >> 1) | y;
}
void warshall(vector<vector<long long>> &v) {
  ll n = v.size();
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      for (long long k = 0; k < n; k++) {
        v[j][k] = min(v[j][k], v[j][i] + v[i][k]);
      }
    }
  }
  return;
}
vvll comb(100, vll(100, -1));
long long com(long long n, long long k) {
  if (n < 0 || k < 0) return -1;
  if (n < k) return comb[n][k] = 0;
  if (comb[n][k] != -1) return comb[n][k];
  ll res;
  if (n - k < k)
    res = com(n, n - k);
  else if (k == 0)
    res = 1;
  else
    res = com(n - 1, k - 1) + com(n - 1, k);
  comb[n][k] = res;
  return res;
}
long long com2(long long n, long long k) {
  if (n < 0 || k < 0) return -1;
  if (n < k) return comb[n][k] = 0;
  ll res;
  if (n - k < k)
    res = com(n, n - k);
  else if (k == 0)
    res = 1;
  else
    res = com(n - 1, k - 1) + com(n - 1, k);
  return res;
}
void comset() {
  for (long long i = 0; i < 100; i++)
    for (long long j = 0; j < 100; j++) com(i, j);
}
string bits(long long n, long long k) {
  string s = "";
  for (long long i = 0; i < k; i++) {
    char c = '0' + (n % 2);
    s += c;
    n /= 2;
  }
  reverse(s.begin(), s.end());
  return s;
}
struct UF {
  vector<long long> par, size;
  UF(long long N) : par(N), size(N) {
    for (long long i = 0; i < N; i++) {
      par[i] = i;
      size[i] = 1;
    }
  }
  long long root(long long x) {
    if (par[x] == x) return x;
    return par[x] = root(par[x]);
  }
  void unite(long long x, long long y) {
    long long rx = root(x);
    long long ry = root(y);
    if (rx == ry) return;
    par[rx] = ry;
    size[ry] += size[rx];
    size[rx] = 0;
  }
  bool same(long long x, long long y) {
    long long rx = root(x);
    long long ry = root(y);
    return rx == ry;
  }
};
struct mint {
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod) x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
void debug(ll &a) { cout << a << endl; }
void debug(vll &a) {
  cout << "debug vector" << endl;
  cout << "{ ";
  for (auto t : a) {
    cout << t << ",";
  }
  cout << " }" << endl;
  cout << "debug finished" << endl;
}
void debug(set<long long> &a) {
  cout << "debug set" << endl;
  for (auto t : a) {
    cout << t << endl;
  }
  cout << "debug finished" << endl;
}
void debug(map<long long, long long> a) {
  cout << "debug map" << endl;
  for (auto t : a) {
    cout << t.first << " " << t.second << endl;
  }
  cout << "debug finished" << endl;
}
void debug(queue<long long> a) {
  cout << "debug queue" << endl;
  while (!a.empty()) {
    ll t = a.front();
    a.pop();
    cout << t << endl;
  }
  cout << "debug finished" << endl;
}
struct STmax {
  ll size;
  ll m;
  vector<long long> seg;
  STmax(long long n) : size(n), m(n), seg(0) {
    m = 1;
    while (m < n) {
      m *= 2;
    }
    size = m * 2 - 1;
    for (long long i = 0; i < size; i++) {
      seg.push_back(-99999999977);
    }
  }
  void update(ll i, ll k) {
    ll v = i + m - 1;
    seg[v] = k;
    while (v > 0) {
      v = (v - 1) / 2;
      seg[v] = max(seg[v * 2 + 1], seg[v * 2 + 2]);
    }
  }
  ll query(ll a, ll b, ll k, ll l, ll r) {
    if (r <= a || b <= l) return -99999999977;
    if (a <= l && r <= b) return seg[k];
    ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
  ll largest(ll a, ll b) {
    if (a >= b) return -99999999977;
    return query(a, b, 0, 0, m);
  }
};
struct STmin {
  ll size;
  ll m;
  vector<long long> seg;
  STmin(long long n) : size(n), m(n), seg(0) {
    m = 1;
    while (m < n) {
      m *= 2;
    }
    size = m * 2 - 1;
    for (long long i = 0; i < size; i++) {
      seg.push_back(99999999977);
    }
  }
  void update(ll i, ll k) {
    ll v = i + m - 1;
    seg[v] = k;
    while (v > 0) {
      v = (v - 1) / 2;
      seg[v] = min(seg[v * 2 + 1], seg[v * 2 + 2]);
    }
  }
  ll query(ll a, ll b, ll k, ll l, ll r) {
    if (r <= a || b <= l) return 99999999977;
    if (a <= l && r <= b) return seg[k];
    ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
  ll smallest(ll a, ll b) {
    if (a >= b) return 99999999977;
    return query(a, b, 0, 0, m);
  }
};
struct STsum {
  ll size;
  ll m;
  vector<long long> seg;
  STsum(long long n) : size(n), m(n), seg(0) {
    m = 1;
    while (m < n) {
      m *= 2;
    }
    size = m * 2 - 1;
    for (long long i = 0; i < size; i++) {
      seg.push_back(0);
    }
  }
  void update(ll i, ll k) {
    ll v = i + m - 1;
    seg[v] = k;
    while (v > 0) {
      v = (v - 1) / 2;
      seg[v] = seg[v * 2 + 1] + seg[v * 2 + 2];
    }
  }
  ll query(ll a, ll b, ll k, ll l, ll r) {
    if (r <= a || b <= l) return 0;
    if (a <= l && r <= b) return seg[k];
    ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return vl + vr;
  }
  ll sum(ll a, ll b) {
    if (a >= b) return 0;
    return query(a, b, 0, 0, m);
  }
};
ll const dekai = 9223372036854775807;
ll dx[4] = {-1, 0, 1, 0};
ll dy[4] = {0, -1, 0, 1};
ll ddx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
ll ddy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
ll sgn(ll n) {
  if (n == 0) return 0;
  if (n < 0) return -1;
  return 1;
}
int main() {
  ll t;
  cin >> t;
  vll ans(t);
  for (long long cnt = 0; cnt < t; cnt++) {
    ll res = 0;
    ll n;
    cin >> n;
    vpll a, b;
    for (long long i = 0; i < n; i++) {
      ll k;
      cin >> k;
      ll h, g;
      cin >> h >> g;
      res += min(h, g);
      ll l = h - g;
      if (l >= 0)
        a.push_back({l, k});
      else
        b.push_back({-l, k});
    }
    ll as = a.size();
    ll bs = b.size();
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    sll s, u;
    for (ll i = 1; i < as + 1; i++) {
      s.insert(i);
    }
    for (long long i = 0; i < as; i++) {
      ll pt = a[i].first;
      ll lim = a[i].second;
      auto it = upper_bound(s.begin(), s.end(), lim);
      if (it == s.begin())
        continue;
      else {
        it--;
        s.erase(it);
        res += pt;
      }
    }
    for (ll i = as + 1; i <= n; i++) {
      u.insert(i);
    }
    for (long long i = 0; i < bs; i++) {
      ll pt = b[i].first;
      ll lim = b[i].second;
      auto it = upper_bound(u.begin(), u.end(), lim);
      if (it == u.end()) {
        continue;
      } else {
        u.erase(it);
        res += pt;
      }
    }
    ans[cnt] = res;
  }
  for (long long i = 0; i < t; i++) cout << ans[i] << endl;
}
