#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
const long long int INF = (long long int)1e15;
struct Data {
  long long int K;
  long long int L;
  long long int R;
  long long int index;
};
long long int solve() {
  long long int N;
  cin >> N;
  vector<Data> F, S, A;
  for (long long int(i) = (0); (i) < (N); ++(i)) {
    Data d;
    cin >> d.K >> d.L >> d.R;
    d.index = i;
    if (d.L == d.R) A.push_back(d);
    if (d.L < d.R)
      S.push_back(d);
    else
      F.push_back(d);
  }
  long long int ret = 0;
  for (long long int(i) = (0); (i) < (A.size()); ++(i)) {
    ret += A[i].R;
  }
  vector<long long int> test(N, -1);
  {
    priority_queue<Data, vector<Data>, function<bool(Data, Data)>> q(
        [](Data a, Data b) {
          if (a.K == b.K) return abs(a.R - a.L) < abs(b.R - b.L);
          return a.K < b.K;
        });
    for (long long int(i) = (0); (i) < (F.size()); ++(i)) {
      q.push(F[i]);
    }
    priority_queue<Data, vector<Data>, function<bool(Data, Data)>> q0(
        [](Data a, Data b) { return abs(a.R - a.L) < abs(b.R - b.L); });
    long long int index = F.size();
    while (index > 0) {
      while (!q.empty() && q.top().K >= index) {
        auto t = q.top();
        q.pop();
        q0.push(t);
      }
      index--;
      if (q0.empty()) continue;
      auto d = q0.top();
      q0.pop();
      ret += d.L;
    }
    while (!q0.empty()) {
      ret += q0.top().R;
      q0.pop();
    }
    while (!q.empty()) {
      ret += q.top().R;
      q.pop();
    }
  }
  {
    priority_queue<Data, vector<Data>, function<bool(Data, Data)>> q(
        [](Data a, Data b) {
          if (a.K == b.K) return abs(a.R - a.L) < abs(b.R - b.L);
          return a.K > b.K;
        });
    for (long long int(i) = (0); (i) < (S.size()); ++(i)) {
      q.push(S[i]);
    }
    priority_queue<Data, vector<Data>, function<bool(Data, Data)>> q0(
        [](Data a, Data b) { return abs(a.R - a.L) < abs(b.R - b.L); });
    long long int index = A.size() + F.size() + 1;
    while (index <= N) {
      while (!q.empty() && q.top().K < index) {
        auto t = q.top();
        q.pop();
        q0.push(t);
      }
      index++;
      if (q0.empty()) continue;
      auto d = q0.top();
      q0.pop();
      ret += d.R;
    }
    while (!q0.empty()) {
      ret += q0.top().L;
      q0.pop();
    }
    while (!q.empty()) {
      ret += q.top().L;
      q.pop();
    }
  }
  return ret;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long int Q;
  cin >> Q;
  for (long long int(_) = (0); (_) < (Q); ++(_)) {
    cout << solve() << endl;
  }
  return 0;
}
