#include <bits/stdc++.h>
using namespace std;
template <typename T>
void out(T x) {
  cout << x << endl;
  exit(0);
}
template <typename T>
struct bit1 {
  T add(T x, T y) { return x + y; }
  int n;
  vector<T> t;
  void init(int n) {
    this->n = n;
    t.resize(n + 10);
  }
  T qry(int i) {
    T res = 0;
    for (; i > 0; i -= i & -i) res = add(res, t[i]);
    return res;
  }
  void upd(int i, T dx) {
    for (; i <= n; i += i & -i) t[i] = add(t[i], dx);
  }
};
using ll = long long;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;
ll solve() {
  int n;
  cin >> n;
  bit1<int> inds;
  inds.init(n + 10);
  auto qry = [&](int l, int r) { return inds.qry(r) - inds.qry(l - 1); };
  auto inc = [&](int i) { inds.upd(i, 1); };
  vector<int> k(n), l(n), r(n);
  ll res = 0;
  priority_queue<pair<ll, int>> left, right;
  for (int i = 0; i < n; i++) {
    cin >> k[i] >> l[i] >> r[i];
    if (l[i] >= r[i]) {
      left.push({l[i] - r[i], i});
    } else {
      right.push({r[i] - l[i], i});
    }
  }
  while (left.size()) {
    auto cur = left.top();
    left.pop();
    int i = cur.second;
    int K = k[i];
    if (qry(1, K) < K) {
      inc(K);
      res += l[i];
    } else {
      res += r[i];
    }
  }
  inds = {};
  inds.init(n + 10);
  while (right.size()) {
    auto cur = right.top();
    right.pop();
    int i = cur.second;
    int K = k[i];
    if (qry(K + 1, n) < n - K) {
      inc(K + 1);
      res += r[i];
    } else {
      res += l[i];
    }
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cout << solve() << "\n";
  }
  return 0;
}
