#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<long long int, long long int>& a,
         pair<long long int, long long int>& b) {
  return a.first > b.first;
}
long long int solve(vector<pair<long long int, long long int>>& v,
                    long long int n) {
  set<long long int> free;
  long long int ans = 0;
  for (int i = 1; i <= n; i++) free.insert(i);
  for (auto [val, pos] : v) {
    auto it = free.upper_bound(pos);
    if (it != free.begin()) {
      it--;
      ans += val;
      free.erase(*it);
    }
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    long long int i, k[n + 1], l[n + 1], r[n + 1], ans = 0, diff, rank, sign,
                                                   cur;
    set<long long int> s;
    vector<pair<long long int, long long int>> v1, v2;
    for (i = 1; i <= n; i++) s.insert(i);
    for (i = 1; i <= n; i++) cin >> k[i] >> l[i] >> r[i];
    for (i = 1; i <= n; i++) {
      if (l[i] == r[i])
        ans += r[i];
      else if (l[i] > r[i])
        v1.push_back({l[i] - r[i], k[i]}), ans += r[i];
      else if (l[i] < r[i])
        v1.push_back({r[i] - l[i], n - k[i]}), ans += l[i];
    }
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    cout << ans + solve(v1, n) + solve(v2, n) << "\n";
  }
  return 0;
}
