#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  ~debug() { cerr << endl; }
  template <class c>
  typename enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) {
    cerr << boolalpha << i;
    return *this;
  }
  template <class c>
  typename enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) {
    return *this << range(begin(i), end(i));
  }
  template <class c, class b>
  debug& operator<<(pair<b, c> d) {
    return *this << "(" << d.first << ", " << d.second << ")";
  }
  template <class c>
  debug& operator<<(rge<c> d) {
    *this << "[";
    for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it;
    return *this << "]";
  }
};
long double eps = (long double)1 / 1e6;
const long double pi = 3.14159265359;
long long inf = 1e18, mod1 = 1e9 + 7;
long long sqr(long long a) { return a * a; }
long long qb(long long a) { return a * a * a; }
long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); }
long long binpow(long long a, long long b, long long mod) {
  return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod
                    : sqr(binpow(a, b / 2, mod)) % mod)
           : 1;
}
long long binmult(long long a, long long b, long long mod) {
  return b ? (b % 2 ? (2 * binmult(a, b / 2, mod) + a) % mod
                    : (2 * binmult(a, b / 2, mod)) % mod)
           : 0;
}
const long long R = 1e4;
const long long tx[4] = {0, 0, -1, 1};
const long long ty[4] = {-1, 1, 0, 0};
const char rev_to[4] = {'E', 'W', 'N', 'S'};
const int N = 1e5 + 1;
const int M = 1e9 + 7;
int n;
int get_p(vector<int>& p, int v) {
  return (p[v] == v || p[v] == n || p[v] == n - 1 ? p[v]
                                                  : p[v] = get_p(p, p[v]));
}
signed main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<int> a(n), b(n), c(n);
    vector<pair<int, int> > ne, d, second;
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i] >> c[i];
      --a[i];
      if (b[i] > c[i]) {
        ne.push_back({b[i] - c[i], a[i]});
        ans += c[i];
      } else if (b[i] <= c[i]) {
        d.push_back({c[i] - b[i], a[i]});
        ans += b[i];
      }
    }
    sort(d.rbegin(), d.rend());
    sort(ne.rbegin(), ne.rend());
    vector<int> p(n + 2), cd(n);
    iota(p.begin(), p.end(), 0);
    int l = 0;
    for (auto& z : ne) {
      int ps = get_p(p, z.second);
      if (ps == -1) {
        d.push_back({0, z.second});
        continue;
      }
      cd[ps] = z.first;
      p[ps] = ps - 1;
      l++;
      ans += z.first;
    }
    sort(d.rbegin(), d.rend());
    iota(p.begin(), p.end(), 0);
    for (int i = 0; i < l; i++) {
      p[i] = l;
    }
    for (auto& z : d) {
      int ps = get_p(p, z.second + 1);
      if (ps == n) {
        continue;
      }
      cd[ps] = z.first;
      p[ps] = ps + 1;
      l++;
      ans += z.first;
    }
    cout << ans << "\n";
  }
  return 0;
}
