#include <bits/stdc++.h>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
template <ostream& out = cout, class... T>
void printde(T&&...) {}
template <ostream& out = cout, class... T>
void printdbe(T&&...) {}
template <ostream& out = cout, class... T>
void printdwe(T&&...) {}
template <ostream& out = cout, class... T>
void printdu(T&&...) {}
template <ostream& out = cout, class... T>
void printe(T&&...) {}
template <ostream& out = cout, class... T>
void printe0(T&&...){};
template <template <class T, class = std::allocator<T>> class V, class E>
istream& operator>>(istream& in, V<E>& v) {
  for (auto& e : v) {
    in >> e;
  }
  return in;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  for (int _ = 0; _ < t; _++) {
    int n;
    cin >> n;
    vector<int> ks(n), ls(n), rs(n);
    for (int i = 0; i < n; i++) {
      cin >> ks[i] >> ls[i] >> rs[i];
      ks[i]--;
    }
    vector<int> lls(n), rrs(n);
    for (int i = 0; i < n; i++) {
      lls[i] = i;
      rrs[i] = i;
    }
    vector<int> is(n);
    iota(is.begin(), is.end(), 0);
    sort(is.begin(), is.end(),
         [&](int i, int j) { return abs(ls[i] - rs[i]) > abs(ls[j] - rs[j]); });
    ll ans = 0;
    for (auto& i : is) {
      if (ls[i] < rs[i]) {
        int pos = ks[i] + 1;
        if (pos == n) {
          ans += ls[i];
        } else {
          int idx = rrs[pos];
          if (idx < n) {
            rrs[idx] = idx + 1 < n ? rrs[idx + 1] : n;
            ans += rs[i];
          } else {
            ans += ls[i];
          }
        }
      } else {
        int pos = ks[i];
        int idx = lls[pos];
        if (idx >= 0) {
          lls[idx] = idx - 1 >= 0 ? lls[idx - 1] : -1;
          ans += ls[i];
        } else {
          ans += rs[i];
        }
      }
    }
    cout << ans << '\n';
  }
}
