#include <bits/stdc++.h>
using namespace std;
const long long INF = 1010000000000000017LL;
const long long MOD = 1000000007LL;
struct camel {
  camel(long long _k, long long _l, long long _r) : k(_k), l(_l), r(_r){};
  long long k;
  long long l;
  long long r;
};
int main() {
  std::cout << std::fixed << std::setprecision(10);
  long long T, N;
  cin >> T;
  for (long long i = 0; i < T; i++) {
    cin >> N;
    vector<camel> L;
    vector<camel> R;
    vector<camel> S;
    long long tt = 0;
    for (long long j = 0; j < N; j++) {
      long long k, l, r;
      cin >> k >> l >> r;
      if (l > r) {
        L.push_back(camel(k, l, r));
      } else if (r > l) {
        R.push_back(camel(k, l, r));
      } else {
        tt += l;
      }
    }
    sort(L.begin(), L.end(),
         [](const camel& a, const camel& b) -> bool { return a.k < b.k; });
    sort(R.begin(), R.end(),
         [](const camel& a, const camel& b) -> bool { return a.k > b.k; });
    long long curL = 1;
    for (int i = 0; i < L.size(); i++) {
      if (curL <= L[i].k) {
        tt += L[i].l;
      } else {
        tt += L[i].r;
      }
      curL++;
    }
    long long curR = N;
    for (int i = 0; i < R.size(); i++) {
      if (curR > R[i].k) {
        tt += R[i].r;
      } else {
        tt += R[i].l;
      }
      curR--;
    }
    cout << tt << endl;
  }
}
