#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
int seg[524288];
void add(int x) {
  x += 262144;
  seg[x]++;
  while (x) {
    x /= 2;
    seg[x] = seg[x * 2] + seg[x * 2 + 1];
  }
}
int rsq(int s, int t, int k = 1, int l = 0, int r = 262143) {
  if (t < l || r < s) return 0;
  if (s <= l && r <= t) {
    return seg[k];
  }
  int lv = rsq(s, t, k * 2, l, (l + r) / 2);
  int rv = rsq(s, t, k * 2 + 1, (l + r) / 2 + 1, r);
  return lv + rv;
}
void init(int N) {
  int n = N + 262149;
  int d = 262144;
  while (n) {
    for (int i = d; i <= n; i++) {
      seg[i] = 0;
    }
    d /= 2;
    n /= 2;
    seg[n * 2 + 1] = 0;
  }
}
int main() {
  int T, N, K, L, R;
  cin >> T;
  while (T--) {
    cin >> N;
    vector<pair<long long, long long>> LU, RU;
    init(N);
    long long S = 0;
    for (int i = 0; i < N; i++) {
      cin >> K >> L >> R;
      if (K == N) {
        S += L;
      } else if (L > R) {
        LU.push_back({L - R, K});
        S += R;
      } else {
        RU.push_back({R - L, N - K});
        S += L;
      }
    }
    sort(LU.begin(), LU.end());
    reverse(LU.begin(), LU.end());
    sort(RU.begin(), RU.end());
    reverse(RU.begin(), RU.end());
    int x;
    for (auto P : LU) {
      x = P.second;
      if (rsq(0, x) >= x) {
        continue;
      }
      add(x);
      S += P.first;
    }
    init(N);
    for (auto P : RU) {
      x = P.second;
      if (rsq(0, x) >= x) {
        continue;
      }
      add(x);
      S += P.first;
    }
    cout << S << "\n";
  }
}
