#include <bits/stdc++.h>
using namespace std;
template <typename X>
ostream& operator<<(ostream& x, const vector<X>& v) {
  for (long long i = 0; i < v.size(); ++i) x << v[i] << " ";
  return x;
}
template <typename X>
ostream& operator<<(ostream& x, const set<X>& v) {
  for (auto it : v) x << it << " ";
  return x;
}
template <typename X, typename Y>
ostream& operator<<(ostream& x, const pair<X, Y>& v) {
  x << v.first << " " << v.second;
  return x;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v) {
  for (auto it : v) os << it.first << "=>" << it.second << endl;
  return os;
}
bool cp(pair<pair<long long, long long>, long long> a,
        pair<pair<long long, long long>, long long> b) {
  if (abs(a.first.first - a.first.second) ==
      abs(b.first.first - b.first.second)) {
    return a.second > b.second;
  }
  return abs(a.first.first - a.first.second) >
         abs(b.first.first - b.first.second);
}
long long const N = 2e5 + 10;
long long tree[4 * N], lazy[4 * N], a[N], n;
long long merge(long long x, long long y) { return x + y; }
long long cnt(long long l, long long r) { return r - l + 1; }
void build(long long i, long long l, long long r) {
  if (l == r) {
    tree[i] = a[l];
    return;
  }
  build((i << 1), l, ((l + r) >> 1));
  build(((i << 1) | 1), ((l + r) >> 1) + 1, r);
  tree[i] = merge(tree[(i << 1)], tree[((i << 1) | 1)]);
}
void propogate(long long i, long long l, long long r) {
  if (l != r) {
    lazy[(i << 1)] += lazy[i];
    lazy[((i << 1) | 1)] += lazy[i];
  }
  tree[i] += cnt(l, r) * lazy[i];
  lazy[i] = 0;
}
void update(long long i, long long l, long long r, long long ql, long long qr,
            long long val) {
  propogate(i, l, r);
  if (r < ql || l > qr) return;
  if (ql <= l && r <= qr) {
    lazy[i] += val;
    propogate(i, l, r);
    return;
  }
  update((i << 1), l, ((l + r) >> 1), ql, qr, val);
  update(((i << 1) | 1), ((l + r) >> 1) + 1, r, ql, qr, val);
  tree[i] = merge(tree[(i << 1)], tree[((i << 1) | 1)]);
}
long long query(long long i, long long l, long long r, long long ql,
                long long qr) {
  propogate(i, l, r);
  if (r < ql || l > qr) return 0;
  if (ql <= l && r <= qr) return tree[i];
  long long q1 = query((i << 1), l, ((l + r) >> 1), ql, qr);
  long long q2 = query(((i << 1) | 1), ((l + r) >> 1) + 1, r, ql, qr);
  return merge(q1, q2);
}
void solve() {
  cin >> n;
  vector<pair<pair<long long, long long>, long long>> v;
  for (long long i = 0; i < n; i++) {
    long long l, r, x;
    cin >> x >> l >> r;
    v.push_back({{l, r}, x});
  }
  for (long long i = 0; i <= 4 * n + 1; i++) {
    tree[i] = 0;
    lazy[i] = 0;
  }
  long long ans = 0;
  sort(v.begin(), v.end(), cp);
  long long L = 0, R = 0;
  for (long long i = 0; i < n; i++) {
    if (v[i].second == n) {
      ans += v[i].first.first;
      continue;
    }
    long long q = query(1, 1, n, v[i].second, v[i].second);
    if (v[i].first.first > v[i].first.second && q < v[i].second) {
      update(1, 1, n, v[i].second, v[i].second, 1);
      ans += v[i].first.first;
    } else {
      ans += v[i].first.second;
    }
  }
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
