#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define all(x) x.begin(),x.end()
#define STRINGIFY(n) #n
#define TOSTRING(n) STRINGIFY(n)
#define PREFIX "#" TOSTRING(__LINE__) "| "
#define debug(x) \
{ \
  std::cout << PREFIX << #x << " = " << x << std::endl; \
}

std::ostream& output_indent(std::ostream& os, int ind) {
  for(int i = 0; i < ind; i++) os << " ";
  return os;
}

template<class S, class T> std::ostream& operator<<(std::ostream& os, const std::pair<S, T>& p);
template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);

template<class S, class T> std::ostream& operator<<(std::ostream& os, const std::pair<S, T>& p) {
  return (os << "(" << p.first << ", " << p.second << ")");
}
template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
  os << "[";
  for(int i = 0;i < v.size();i++) os << v[i] << ", ";
  return (os << "]");
}

template<class T>
static inline std::vector<T> ndvec(size_t&& n, T val) { return std::vector<T>(n, std::forward<T>(val)); }
template<class... Tail>
static inline auto ndvec(size_t&& n, Tail&&... tail) {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(n, ndvec(std::forward<Tail>(tail)...));
}
template<class Cond> struct chain {
  Cond cond; chain(Cond cond) : cond(cond) {}
  template<class T> bool operator()(T& a, const T& b) const { if(cond(a, b)) { a = b; return true; } return false; }
};
template<class Cond> chain<Cond> make_chain(Cond cond) { return chain<Cond>(cond); }

void solve() {
  i64 N;
  cin >> N;
  std::set<i64> idx;
  rep(i,0,N) {
    idx.insert(i);
  }
  vector<pair<i64, pair<i64, i64>>> vec;
  i64 ans = 0;
  rep(i,0,N) {
    i64 k, l, r;
    cin >> k >> l >> r;
    k--;
    if(l < r) {
      ans += l;
      vec.push_back({ r - l, {k, -1} });
    }
    else {
      ans += r;
      vec.push_back({ l - r, {k, 1} });
    }
  }
  sort(all(vec));
  reverse(all(vec));
  for(auto v: vec) {
    i64 diff = v.first;
    i64 i = v.second.first;
    i64 mode = v.second.second;
    if(mode == 1) {
      auto iter = idx.upper_bound(i);
      if(iter == idx.begin()) {
        idx.erase(iter);
      }
      else {
        --iter;
        ans += diff;
        idx.erase(iter);
      }
    }
    else {
      auto iter = idx.upper_bound(i);
      if(iter == idx.end()) {
        --iter;
        idx.erase(iter);
      }
      else {
        ans += diff;
        idx.erase(iter);
      }
    }
  }
  cout << ans << endl;
}

int main() {
  i64 T;
  cin >> T;
  while(T--) {
    solve();
  }
}
