#include <bits/stdc++.h>
using namespace std;
struct FenwickTreeOneBasedIndexing {
  vector<int> bit;
  int n;
  FenwickTreeOneBasedIndexing(int n) {
    this->n = n + 1;
    bit.assign(n + 1, 0);
  }
  FenwickTreeOneBasedIndexing(vector<int> a)
      : FenwickTreeOneBasedIndexing(a.size()) {
    for (size_t i = 0; i < a.size(); i++) add(i, a[i]);
  }
  int sum(int idx) {
    int ret = 0;
    for (++idx; idx > 0; idx -= idx & -idx) ret += bit[idx];
    return ret;
  }
  int sum(int l, int r) { return sum(r) - sum(l - 1); }
  void add(int idx, int delta) {
    for (++idx; idx < n; idx += idx & -idx) bit[idx] += delta;
  }
};
struct node {
  int k, l, r, rev;
  node(int _k, int _l, int _r, int _rev) { k = _k, l = _l, r = _r, rev = _rev; }
  bool operator<(const node& m) const {
    if (!rev) return (l - r) > (m.l - m.r);
    return (r - l) > (m.r - m.l);
  }
};
int getFirst(FenwickTreeOneBasedIndexing& f, int k, int sz) {
  int l = 0, r = k, res = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    int sm = f.sum(mid, k);
    if (sm < (k - mid + 1)) {
      res = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  return res;
}
int getLast(FenwickTreeOneBasedIndexing& f, int k, int sz) {
  int l = k, r = sz - 1, res = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    int sm = f.sum(mid, sz - 1);
    if (sm < (sz - mid)) {
      res = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  return res;
}
void solve() {
  int n;
  cin >> n;
  vector<node> L, R;
  for (int i = 0; i < n; i++) {
    int k, l, r;
    cin >> k >> l >> r;
    if (l >= r) {
      L.push_back(node(k - 1, l, r, 0));
    } else
      R.push_back(node(k - 1, l, r, 1));
  }
  sort(L.begin(), L.end());
  sort(R.begin(), R.end());
  long long score = 0;
  FenwickTreeOneBasedIndexing left(L.size());
  FenwickTreeOneBasedIndexing right(R.size());
  for (int i = 0; i < L.size(); i++) {
    int ki = L[i].k;
    ki = min(ki, (int)L.size() - 1);
    int idx = getFirst(left, ki, L.size());
    if (idx == -1)
      score += L[i].r;
    else {
      score += L[i].l;
      left.add(idx, 1);
    }
  }
  for (int i = 0; i < R.size(); i++) {
    int ki = R[i].k;
    ki -= L.size();
    ki = max(ki, 0);
    int idx = getLast(right, ki + 1, R.size());
    if (idx == -1)
      score += R[i].l;
    else {
      score += R[i].r;
      right.add(idx, 1);
    }
  }
  cout << score << endl;
}
int main() {
  int t;
  for (cin >> t; t--;) {
    solve();
  }
  return 0;
}
