#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<ll, ll>;
using vvii = vector<vector<int> >;
using vvll = vector<vector<ll> >;
const ll inf = 1LL << 60;
struct camel {
  ll left;
  ll right;
  ll diff;
  ll K;
  camel(ll l, ll r, ll d, ll k) { left = l, right = r, diff = d, K = k; }
};
bool comp(const camel& a, const camel& b) {
  if (a.diff == b.diff) {
    if (a.K == b.K) {
      if (a.left == b.left) {
        return a.right < b.right;
      }
      return a.left < b.left;
    }
    return a.K < b.K;
  }
  return a.diff < b.diff;
}
auto comp_pair_second = [](const pll p1, const pll p2) {
  return p1.second > p2.second;
};
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int T;
  cin >> T;
  vector<ll> vec(T);
  for (int _ = 0; _ < T; _++) {
    ll ans = 0;
    int N;
    cin >> N;
    vector<pll> LL, RR;
    priority_queue<pll, vector<pll>, decltype(comp_pair_second)> forL{
        comp_pair_second},
        forR{comp_pair_second};
    for (int i = 0; i < N; i++) {
      ll k, l, r;
      cin >> k >> l >> r;
      k--;
      if (l > r) {
        ans += r;
        LL.push_back(pll(k, l - r));
      } else {
        ans += l;
        RR.push_back(pll(N - k - 2, r - l));
      }
    }
    sort(LL.begin(), LL.end());
    sort(RR.begin(), RR.end());
    int itrl = 0;
    for (int i = 0; i < N; i++) {
      while (LL[itrl].first < i && itrl < LL.size()) itrl++;
      while (LL[itrl].first == i && itrl < LL.size()) {
        forL.push(LL[itrl]);
        itrl++;
      }
      while (forL.size() > i + 1) {
        forL.pop();
      }
    }
    while (!forL.empty()) {
      ans += forL.top().second;
      forL.pop();
    }
    int itrr = 0;
    for (int i = 0; i < N; i++) {
      while (LL[itrr].first < i && itrr < RR.size()) itrr++;
      while (RR[itrr].first == i && itrr < RR.size()) {
        forR.push(RR[itrr]);
        itrr++;
      }
      while (forR.size() > i + 1) {
        forR.pop();
      }
    }
    while (!forR.empty()) {
      ans += forR.top().second;
      forR.pop();
    }
    vec[_] = ans;
  }
  for (int i = 0; i < T; i++) {
    cout << vec[i] << endl;
  }
}
