#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
const long long int mod = 998244353;
const long long int inf = 2.19e15 + 1;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-10;
template <class T, class U>
bool chmin(T& a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U>
bool chmax(T& a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
long long int gcd(long long int a, long long int b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T>
void SO(T& ve) {
  sort(ve.begin(), ve.end());
}
template <class T>
void REV(T& ve) {
  reverse(ve.begin(), ve.end());
}
template <class T>
long long int LBI(const vector<T>& ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T>
long long int UBI(const vector<T>& ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}
long long int solve(vector<pair<long long int, int>>& in) {
  int i, n = in.size();
  SO(in);
  set<int> is;
  for (i = 1; i <= n; i++) {
    is.insert(i);
  }
  long long int ans = 0;
  for (auto it : in) {
    auto itr = is.upper_bound(it.second);
    if (itr == is.begin()) {
      continue;
    }
    is.erase(prev(itr));
    ans += it.first;
  }
  return ans;
}
int main(void) {
  int T;
  cin >> T;
  while (T--) {
    int i, n;
    cin >> n;
    long long int ans = 0;
    vector<pair<long long int, int>> Lc;
    vector<pair<long long int, int>> Rc;
    for (i = 0; i < n; i++) {
      int K;
      long long int L, R;
      cin >> K >> L >> R;
      if (L > R) {
        ans += R;
        Lc.push_back(make_pair(L - R, K));
      } else {
        ans += L;
        Rc.push_back(make_pair(R - L, n - K));
      }
    }
    cout << ans + solve(Lc) + solve(Rc) << endl;
  }
  return 0;
}
