#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 9;
const long long mod = 1e9 + 7;
struct camel {
  long long k, L, R;
};
camel a[N];
long long n, i, ans;
vector<camel> v1, v2;
bool lf1(camel x, camel y) { return x.k < y.k; }
bool lf2(camel x, camel y) { return x.k > y.k; }
long long cal(priority_queue<pair<long long, long long> > pq) {
  long long res = 0;
  while (!pq.empty()) {
    long long C = pq.top().first;
    res += C;
    pq.pop();
  }
  return res;
}
void oper1() {
  priority_queue<pair<long long, long long> > pq;
  for (auto i : v1) {
    long long k = i.k;
    if (pq.size() < k)
      pq.push({i.L, i.R});
    else {
      pair<long long, long long> C = pq.top();
      if (!pq.empty() && C.first < i.L) {
        ans += C.second;
        pq.pop();
        pq.push({i.L, i.R});
      } else
        ans += i.R;
    }
  }
  ans += cal(pq);
}
void oper2() {
  priority_queue<pair<long long, long long> > pq;
  for (auto i : v2) {
    long long k = i.k;
    if (n - pq.size() > k)
      pq.push({i.R, i.L});
    else {
      pair<long long, long long> C = pq.top();
      if (!pq.empty() && C.first < i.R) {
        ans += C.second;
        pq.pop();
        pq.push({i.R, i.L});
      } else
        ans += i.L;
    }
  }
  ans += cal(pq);
}
long long t;
int main() {
  if (fopen("tst"
            ".inp",
            "r")) {
    freopen(
        "tst"
        ".inp",
        "r", stdin);
  }
  cin >> t;
  while (t--) {
    v1.clear();
    v2.clear();
    ans = 0;
    cin >> n;
    for (i = 1; i <= n; i++) {
      cin >> a[i].k >> a[i].L >> a[i].R;
      if (a[i].L == a[i].R || a[i].k == n)
        ans += a[i].L;
      else if (a[i].L > a[i].R)
        v1.push_back(a[i]);
      else if (a[i].L < a[i].R)
        v2.push_back(a[i]);
    }
    sort(v1.begin(), v1.end(), lf1);
    sort(v2.begin(), v2.end(), lf2);
    oper1();
    oper2();
    cout << ans << "\n";
  }
}
