#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
constexpr int INF = INT_MAX / 2;
constexpr long long LINF = LLONG_MAX / 2;
constexpr double eps = DBL_EPSILON;
constexpr double PI = 3.141592653589793238462643383279;
long long esp_new;
template <class T>
class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> {
};
template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U>
inline bool chmin(T& lhs, const U& rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
inline long long gcd(long long a, long long b) {
  while (b) {
    long long c = a;
    a = b;
    b = c % b;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
bool isprime(long long n) {
  if (n == 1) return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}
template <typename T>
T mypow(T a, unsigned int b) {
  T res(1), now(a);
  while (b) {
    if (b & 1) res *= now;
    b >>= 1;
    now *= a;
  }
  return res;
}
long long modpow(long long a, unsigned int b, unsigned int mod) {
  long long res(1), now(a % mod);
  while (b) {
    if (b & 1) {
      res *= now;
      res %= mod;
    }
    b >>= 1;
    now *= now;
    now %= mod;
  }
  return res;
}
template <typename T>
void printArray(std::vector<T>& vec) {
  for (int i = 0; i < (long long)((int)vec.size() - 1); i++)
    std::cout << vec[i] << " ";
  if (!vec.empty()) std::cout << vec.back() << std::endl;
}
template <typename T>
void printArray(T l, T r) {
  T rprev = r;
  rprev--;
  for (T i = l; i != rprev; i++) {
    std::cout << *i << " ";
  }
  std::cout << *rprev << std::endl;
}
int t, n;
int main() {
  std::cin >> t;
  for (int _ = 0; _ < (long long)(t); _++) {
    std::cin >> n;
    std::vector<std::pair<std::pair<int, int>, int>> vec[2];
    for (int i = 0; i < (long long)(n); i++) {
      int k, l, r;
      std::cin >> k >> l >> r;
      k--;
      if (l > r) {
        vec[0].emplace_back(std::make_pair(l - r, k), r);
      } else {
        vec[1].emplace_back(std::make_pair(r - l, k), l);
      }
    }
    std::sort(vec[0].begin(), vec[0].end());
    std::sort(vec[1].begin(), vec[1].end());
    std::set<int> st;
    for (int i = 0; i < (long long)(vec[0].size()); i++) st.insert(i);
    long long ans = 0;
    for (int i = 0; i < (long long)(vec[0].size()); i++) {
      int d = vec[0][i].first.first, nk = vec[0][i].first.second,
          b = vec[0][i].second;
      if (nk < *st.begin())
        ans += b;
      else {
        st.erase(--st.upper_bound(nk));
        ans += d + b;
      }
    }
    st.clear();
    for (int i = vec[0].size(); i < n; i++) st.insert(i);
    for (int i = 0; i < (long long)(vec[1].size()); i++) {
      int d = vec[1][i].first.first, nk = vec[1][i].first.second,
          b = vec[1][i].second;
      if (*st.rbegin() <= nk)
        ans += b;
      else {
        st.erase(st.lower_bound(nk));
        ans += d + b;
      }
    }
    std::cout << ans << std::endl;
  }
  return 0;
}
