#include <bits/stdc++.h>
using namespace std;
template <class T>
void print(const T& t) {
  cout << t << "\n";
}
template <class T, class... Ts>
void print(const T& t, const Ts&... ts) {
  cout << t;
  if (sizeof...(ts)) cout << " ";
  print(ts...);
}
long long calc(vector<pair<int, long long>>& vec) {
  int n = vec.size();
  for (int i = 0; i < (n); i++) vec[i].first *= -1;
  sort(vec.begin(), vec.end());
  for (int i = 0; i < (n); i++) vec[i].first *= -1;
  long long s = 0;
  int i = 0, i0 = vec[0].first;
  multiset<long long> st;
  while (i0) {
    while (i < n and vec[i].first == i0) st.insert(-vec[i++].second);
    if (st.size()) {
      s -= *st.begin();
      st.erase(st.begin());
    }
    i0--;
  }
  return s;
}
void solve(vector<int>& k, vector<long long>& l, vector<long long> r) {
  int n = k.size();
  vector<pair<int, long long>> lft, rgt;
  long long s = 0;
  for (int i = 0; i < (n); i++) {
    if (r[i] < l[i]) {
      lft.push_back({k[i], l[i] - r[i]});
      s += r[i];
    } else if (l[i] < r[i]) {
      rgt.push_back({n - k[i], r[i] - l[i]});
      s += l[i];
    } else {
      s += l[i];
    }
  }
  print(s + calc(rgt) + calc(lft));
}
int main() {
  int t;
  cin >> t;
  for (int _ = 0; _ < (t); _++) {
    int n;
    cin >> n;
    vector<int> k(n);
    vector<long long> l(n), r(n);
    for (int i = 0; i < (n); i++) {
      cin >> k[i] >> l[i] >> r[i];
    }
    solve(k, l, r);
  }
}
