#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  auto doit = [] (vector<pair<int, int>>& v) -> long long {
    sort(v.rbegin(), v.rend());
    int pos = (int) v.size() - 1;
    long long res = 0;
    priority_queue<long long> que;
    int it = 0;
    while (pos >= 0) {
      while (it < (int) v.size() && v[it].first >= pos) {
        que.push(v[it++].second);
      }
      if (!que.empty()) {
        res += que.top();
        que.pop();
      }
      pos--;
    }
    return res;
  };
  auto solve = [&] () -> long long {
    int n;
    cin >> n;
    vector<pair<int, int>> type1, type2;
    long long res = 0;
    for (int i = 0; i < n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      if (l >= r) {
        k--;
        res += r;
        type1.emplace_back(k, l - r);
      } else {
        k = n - k;
        k--;
        res += l;
        type2.emplace_back(k, r - l);
      }
    }
    return res + doit(type1) + doit(type2);
  };
  int t;
  cin >> t;
  while (t--) cout << solve() << '\n';
  return 0;
}