#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
const int inf = 1 << 30;
const int maxn = 2e5 + 5;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
int n;
vector<pair<int, int> > lft, rgt;
long long cal(vector<pair<int, int> > a) {
  int n = a.size();
  long long res = 0;
  set<int> pos;
  for (int i = (1); i <= (n); i++) pos.insert(i);
  for (auto x : a) {
    auto it = pos.upper_bound(x.second);
    if (it != pos.begin()) {
      it--;
      res += x.first;
      pos.erase(it);
    }
  }
  return res;
}
int main() {
  fastio();
  int _;
  for (cin >> _; _; _--) {
    cin >> n;
    long long ans = 0;
    lft.clear(), rgt.clear();
    for (int i = (1); i <= (n); i++) {
      int k, l, r;
      cin >> k >> l >> r;
      if (l > r)
        lft.push_back({l - r, k}), ans += r;
      else
        rgt.push_back({r - l, n - k}), ans += l;
    }
    sort(lft.begin(), lft.end());
    sort(rgt.begin(), rgt.end());
    ans = ans + cal(lft) + cal(rgt);
    cout << ans << endl;
  }
  return 0;
}
