#include<bits/stdc++.h>
using namespace std;
using ll = long long;

ll solve(const int N, vector<pair<int, int>> v) {
  sort(v.begin(), v.end());
  v.emplace_back(N, 0);
  priority_queue<int, vector<int>, greater<>> q;
  int idx = 0;
  for (int i = 0; i <= N; i++) {
    while (v[idx].first == i) if (i) q.push(v[idx++].second); else idx++;
    while (i < q.size()) q.pop();
  }
  ll res = 0;
  while (q.size()) {
    res += q.top();
    q.pop();
  }
  return res;
}

int main() {
  int T; cin >> T;
  while (T--) {
    ll res = 0;
    int N; cin >> N;
    vector<pair<int, int>> l, r;
    for (int i = 0; i < N; i++) {
      int K, L, R; cin >> K >> L >> R;
      res += min(L, R);
      if (L >= R) l.emplace_back(K, L - R);
      else r.emplace_back(N - K, R - L);
    }
    res += solve(N, l) + solve(N, r);
    cout << res << endl;
  }
}
      
