#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;
};
long long n, i, k, L, R, 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<long long> pq) {
  long long res = 0;
  while (!pq.empty()) res += pq.top(), pq.pop();
  return res;
}
void oper1() {
  priority_queue<long long> pq;
  for (auto i : v1) {
    long long k = i.k;
    if (pq.size() < k)
      pq.push(i.L);
    else {
      if (!pq.empty() && pq.top() < i.L) {
        ans += pq.top();
        pq.pop();
        pq.push(i.L);
      } else
        ans += i.R;
    }
  }
  ans += cal(pq);
}
void oper2() {
  priority_queue<long long> pq;
  for (auto i : v2) {
    long long k = i.k;
    if (n - pq.size() > k)
      pq.push(i.R);
    else {
      if (!pq.empty() && pq.top() < i.R) {
        ans += pq.top();
        pq.pop();
        pq.push(i.R);
      } else
        ans += i.L;
    }
  }
  ans += cal(pq);
}
long long t;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  if (fopen("tes"
            ".inp",
            "r")) {
    freopen(
        "tes"
        ".inp",
        "r", stdin);
  }
  cin >> t;
  while (t--) {
    v1.clear();
    v2.clear();
    ans = 0;
    cin >> n;
    for (i = 1; i <= n; i++) {
      cin >> k >> L >> R;
      if (L > R)
        v1.push_back({k, L, R});
      else if (L < R)
        v2.push_back({k, L, R});
      else
        ans += L;
    }
    sort(v1.begin(), v1.end(), lf1);
    sort(v2.begin(), v2.end(), lf2);
    oper1();
    oper2();
    cout << ans << "\n";
  }
}
