#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1000000007;
const ll INF = 1000000000000000;
vector<ll> par(100000);
vector<ll> ran(100000);
void init(ll n) {
  for (long long i = 0; i < (n); ++i) {
    par.at(i) = i;
    ran.at(i) = 0;
  }
}
ll find(ll x, ll N) {
  if (par.at(x) == x || par.at(x) == -1 || par.at(x) == N) {
    return x;
  } else {
    return par.at(x) = find(par.at(x), N);
  }
}
int main() {
  ll T;
  cin >> T;
  for (long long z = 0; z < (T); ++z) {
    ll N, ans = 0;
    cin >> N;
    priority_queue<P> A, B;
    vector<ll> C(N, 0);
    init(N);
    for (long long i = 0; i < (N); ++i) {
      ll k, l, r;
      cin >> k >> l >> r;
      k--;
      if (l >= r) {
        A.push(P(l - r, k));
        ans += r;
      } else {
        B.push(P(r - l, k));
        ans += l;
      }
    }
    ll a = A.size(), b = B.size();
    for (long long i = 0; i < (a); ++i) {
      ll l = A.top().first, k = A.top().second;
      A.pop();
      if (par.at(find(min(k, a - 1), N)) == -1) {
        C.at(find(a - 1, N)) = 0;
        par.at(a - 1)--;
      } else {
        C.at(find(min(k, a - 1), N)) = l;
        par.at(find(min(k, a - 1), N))--;
      }
    }
    for (long long i = 0; i < (b); ++i) {
      ll r = B.top().first, k = B.top().second;
      B.pop();
      if (k == N - 1) {
        C.at(find(N - b, N)) = 0;
        par.at(N - b)++;
      } else if (par.at(find(max(k, N - b), N)) == N) {
        C.at(find(N - b, N)) = 0;
        par.at(N - b)++;
      } else {
        C.at(find(max(k, N - b), N)) = r;
        par.at(find(max(k, N - b), N))++;
      }
    }
    for (long long i = 0; i < (N); ++i) {
      ans += C.at(i);
    }
    cout << ans << endl;
  }
}
