#include <bits/stdc++.h>
const long double PI = acos(-1);
using namespace std;
inline long long mod(long long n, long long m = 1000000007) {
  long long ret = n % m;
  if (ret < 0) ret += m;
  return ret;
}
long long exp(long long n, long long k) {
  if (k == 0) return 1;
  if (k == 1) return n;
  long long ax = exp(n, k / 2);
  ax = mod(ax * ax);
  if (k % 2) ax = mod(ax * n);
  return ax;
}
long long gcd(long long a, long long b) {
  if (a == 0)
    return b;
  else
    return gcd(b % a, a);
}
struct Camel {
  long long L, R, K, id;
};
tuple<long long, long long, long long, long long> st[4 * 212345];
Camel v[212345];
void build(long long cur, long long l, long long r) {
  if (l == r)
    st[cur] = make_tuple(-1e18, 1e18, -1, l);
  else {
    long long m = (l + r) >> 1;
    build(2 * cur, l, m);
    build(2 * cur + 1, m + 1, r);
    st[cur] = min(st[2 * cur], st[2 * cur + 1]);
  }
}
tuple<long long, long long, long long, long long> get(long long cur,
                                                      long long l, long long r,
                                                      long long a,
                                                      long long b) {
  if (b < l || r < a) return make_tuple(1e18, 1e18, -1, 1e18);
  if (a <= l && r <= b) return st[cur];
  long long m = (l + r) >> 1;
  return min(get(2 * cur, l, m, a, b), get(2 * cur + 1, m + 1, r, a, b));
}
void update(long long cur, long long l, long long r, long long x,
            long long id) {
  if (l == r)
    st[cur] = make_tuple(v[id].L, -v[id].R, id, x);
  else {
    long long m = (l + r) >> 1;
    if (x <= m)
      update(2 * cur, l, m, x, id);
    else
      update(2 * cur + 1, m + 1, r, x, id);
    st[cur] = min(st[2 * cur], st[2 * cur + 1]);
  }
}
bool put_left(long long x, long long n, queue<long long>& notfull) {
  long long l, r, id, tree_idx;
  tie(l, r, id, tree_idx) = get(1, 1, n, 1, v[x].K);
  r = -r;
  if (id == -1) {
    update(1, 1, n, tree_idx, x);
    return true;
  }
  if (v[x].L + r > v[x].R + l) {
    update(1, 1, n, tree_idx, x);
    notfull.push(id);
    return true;
  }
  return false;
}
bool put_right(long long x, long long n, queue<long long>& notfull) {
  long long l, r, id, tree_idx;
  tie(l, r, id, tree_idx) = get(1, 1, n, v[x].K + 1, n);
  r = -r;
  if (id == -1) {
    update(1, 1, n, tree_idx, x);
    return true;
  }
  if (v[x].L + r > v[x].R + l) {
    update(1, 1, n, tree_idx, x);
    notfull.push(id);
    return true;
  }
  return false;
}
void solution() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> v[i].K >> v[i].L >> v[i].R;
    v[i].id = i;
  }
  build(1, 1, n);
  sort(v + 1, v + 1 + n, [&](const Camel a, const Camel b) {
    auto aa = make_tuple(a.K, -a.L, a.R);
    auto bb = make_tuple(b.K, -b.L, b.R);
    return aa <= bb;
  });
  vector<long long> fullfill;
  for (long long i = 1; i <= n; i++) {
    fullfill.push_back(i);
  }
  queue<long long> notfull;
  for (long long x : fullfill) {
    if (v[x].L >= v[x].R) {
      if (put_left(x, n, notfull))
        continue;
      else if (put_right(x, n, notfull))
        continue;
      else
        notfull.push(x);
    } else {
      if (put_right(x, n, notfull))
        continue;
      else if (put_left(x, n, notfull))
        continue;
      else
        notfull.push(x);
    }
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    long long l, r, id, tree_idx;
    tie(l, r, id, tree_idx) = get(1, 1, n, i, i);
    long long cur;
    if (id == -1) {
      cur = notfull.front();
      notfull.pop();
    } else {
      cur = id;
    }
    if (i <= v[cur].K)
      ans += v[cur].L;
    else
      ans += v[cur].R;
  }
  cout << ans << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  long long t = 1;
  cin >> t;
  while (t--) solution();
}
