#include <bits/stdc++.h>
using namespace std;
using cd = complex<double>;
const double pi = acos(-1);
vector<long long> ft(1, 1LL);
long long seg[1 << 20][2];
long long N = 1;
void update(long long pos, long long i) {
  for (seg[pos += N][i]++; pos > 1; pos /= 2)
    seg[pos / 2][i] = seg[pos][i] + seg[pos ^ 1][i];
}
long long sum(long long l, long long r, long long i) {
  long long ret = 0;
  for (l += N, r += N; l < r; l /= 2, r /= 2) {
    if (l & 1) ret += seg[l++][i];
    if ((r & 1) ^ 1) ret += seg[r--][i];
  }
  if (l == r) ret += seg[l][i];
  return ret;
}
void solve() {
  long long n, ans = 0;
  cin >> n;
  while (N < n) N *= 2;
  vector<pair<long long, long long> > v;
  for (long long i = 0; i < n; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    if (b < c) a *= -1;
    v.push_back({abs(b - c), a});
    ans += max(b, c);
  }
  long long mi = 0, ma = n + 1;
  sort(v.begin(), v.end());
  for (auto i : v) {
    if (i.second < 0) {
      i.second = abs(i.second);
      if (i.second + 1 >= ma) {
        ans -= i.first;
        update(i.second, 0);
      } else {
        update(i.second + 1, 1);
        if (sum(i.second + 1, n, 1) == n - i.second) ma = i.second + 1;
      }
    } else {
      if (i.second <= mi) {
        ans -= i.first;
        update(i.second + 1, 1);
      } else {
        update(i.second, 0);
        if (sum(0, i.second, 0) == i.second) mi = i.second;
      }
    }
  }
  cout << ans << '\n';
  for (long long i = 0; i < 2 * N + 2; i++) seg[i][0] = seg[i][1] = 0;
  N = 1;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
