#include <bits/stdc++.h>
using namespace std;
 
using ll = int64_t;
constexpr ll mod = ll(1e9 + 7);
 
/*
in2



4
3
1 1 0
2 10 0
2 10 0
4
5 0 1
5 0 1
5 0 1
3 0 10
3
1 0 10
2 0 9
3 0 8
4
1 10 0
1 0 10
2 0 9
3 0 8


ANS
20
10
19
37
 */
template <typename A, typename B>
string to_string(pair<A, B> p);
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
 
string to_string(const string& s) {
  return '"' + s + '"';
}
 
string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "true" : "false");
}
 
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
 
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
 
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
 
void debug_out() { cerr << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
 
// #ifdef COMPETITIVE_PROGRAMMING_LOCAL
// #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
// #else
#define dbg(...) if(0){}
// #endif


using tup = tuple<int,int>;
void AddToLo(int i, priority_queue<tup, vector<tup>, greater<tup>>& lo, vector<bool>& add_lo_ans) {
  while (i >= 0 && !add_lo_ans[i]) {
    add_lo_ans[i] = true;
    lo.emplace(INT_MIN, i);
    --i;
  }
}

void AddToLoFromTop(int i, priority_queue<tup, vector<tup>, greater<tup>>& lo, vector<tuple<ll,ll,ll,ll>>& ans) {
  if (get<2>(ans[i]) == -1) {
    lo.emplace(INT_MIN, i);
  } else {
    lo.emplace(get<3>(ans[i]), i);
  }
}

int main () {
  ios::sync_with_stdio(0); cin.tie(0);
 
  ll TT = 1;
  cin >> TT;
  vector<tuple<ll,ll,ll>> A;
  vector<vector<tuple<ll,ll,ll>>> before, after;
  vector<tuple<ll,ll,ll,ll>> ans;

  while (TT--) {
    ll n;
    cin >> n;
    A.resize(n);
    for (auto& [k,l,r] : A) cin >> k >> l >> r;
    before.clear();
    after.clear();
    before.resize(n+1); // need to put value [0,i] inclusive
    after.resize(n+1); // need to put value [i,n-1] inclusive
    dbg(A);
    for (int i = 0; i < n; ++i) {
      auto [k,l,r] = A[i];
      if (l > r) {
        before[k-1].emplace_back(l,r,i);
      } else if (l < r) {
        after[k].emplace_back(l,r,i);
      }
    }

    for (int i = 0; i <= n; ++i) {
      sort(before[i].begin(), before[i].end(), [](const auto& a, const auto& b) {
                                                 int da = get<0>(a) - get<1>(a);
                                                 int db = get<0>(b) - get<1>(b);
                                                 return da > db;
                                               });
      sort(after[i].begin(), after[i].end(), [](const auto& a, const auto& b) {
                                                 int da = get<1>(a) - get<0>(a);
                                                 int db = get<1>(b) - get<0>(b);
                                                 return da > db;
                                               });
    }
    ans.clear();
    ans.resize(n, {-1,-1,-1,-1});

    priority_queue<tup, vector<tup>, greater<tup>> lo;
    vector<bool> add_lo_ans(n);
    for (int k = 0; k <= n; ++k) {
      AddToLo(k, lo, add_lo_ans);
      for (auto [l,r,ii] : before[k]) {
        if (lo.empty()) break;
        auto [dx,i_min] = lo.top();
        if (dx >= l-r) break;
        lo.pop();
        ans[i_min] = {l, r, ii, l-r};
        dbg(k, i_min);
        lo.emplace(l-r, i_min);
      }
    }
    // lo.clear();
    while (lo.size()) lo.pop();

    // move max unused before forward to lower places
    for (int k = n-1; k >= 0; --k) {
      AddToLoFromTop(k, lo, ans);
      for (auto [l,r,ii] : after[k]) {
        if (lo.empty()) break;
        auto [dx,i_min] = lo.top();
        if (dx >= r-l) break;
        lo.pop();
        ans[i_min] = {l, r, ii, r-l};
        dbg("hi", k, i_min);
        lo.emplace(r-l, i_min);
      }
    }
    vector<bool> used(n);
    dbg(before);
    dbg(after);
    dbg(ans);

    ll out = 0;
    for (int ii = 0; ii < n; ++ii) {
      if (get<2>(ans[ii]) == -1) continue;
      int i = get<2>(ans[ii]);
      out += max(get<1>(A[i]), get<2>(A[i]));
      used[i] = true;
    }


    for (int i = 0; i < n; ++i) {
      if (!used[i]) {
        out += min(get<1>(A[i]), get<2>(A[i]));
      }
    }

    cout << out << endl;
  }
}


Measured memory_bytes: 32036