#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long expo(long long base, long long exponent, long long mod) {
  long long ans = 1;
  while (exponent != 0) {
    if (exponent & 1) ans = (1LL * ans * base) % mod;
    base = (1LL * base * base) % mod;
    exponent >>= 1;
  }
  return ans % mod;
}
vector<bool> prime(90000002, true);
void Sieve() {
  for (int p = 2; p * p <= 90000001; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= 90000001; i += p) prime[i] = false;
    }
  }
}
long long get_val(vector<long long>& ft, long long index) {
  long long res = 0;
  for (; index >= 0; index = (index & (index + 1)) - 1) res += ft[index];
  return res;
}
void update(vector<long long>& ft, long long index, long long val) {
  for (; index < (long long)ft.size(); index |= index + 1) ft[index] += val;
}
bool mysort(const pair<pair<long long, long long>, long long>& a,
            const pair<pair<long long, long long>, long long>& b) {
  return max(a.first.first, a.first.second) >
         max(b.first.first, b.first.second);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long tests = 1;
  cin >> tests;
  while (tests--) {
    long long i, j, n, ans = 0, ans2 = 0;
    cin >> n;
    vector<long long> k(n), l(n), r(n);
    vector<pair<pair<long long, long long>, long long>> v;
    vector<long long> ft(n + 1, 0);
    for (long long i = 0; i <= n - 1; i++) {
      cin >> k[i] >> l[i] >> r[i];
      v.push_back({{l[i], r[i]}, k[i]});
    }
    sort(v.begin(), v.end(), mysort);
    set<long long> st;
    for (long long i = 1; i <= n; i++) st.insert(i);
    for (i = 0; i < n; i++) {
      pair<long long, long long> p1 = v[i].first;
      long long p2 = v[i].second;
      auto itr = st.lower_bound(p2);
      if (p1.first >= p1.second) {
        if (*itr == p2) {
          ans2 += p1.first;
          st.erase(*itr);
        } else if (itr == st.begin()) {
          ans2 += p1.second;
          st.erase(*itr);
        } else {
          itr--;
          ans2 += p1.first;
          st.erase(*itr);
        }
      } else {
        if (itr == st.end()) {
          ans2 += p1.first;
        } else if (*itr > p2) {
          ans2 += p1.second;
          st.erase(*itr);
        } else {
          itr++;
          if (itr != st.end()) {
            ans2 += p1.second;
            st.erase(*itr);
          } else {
            itr--;
            ans2 += p1.first;
          }
        }
      }
      if (p1.first >= p1.second) {
        long long tmp = get_val(ft, p2);
        if (tmp < p2) {
          ans += p1.first;
          update(ft, p2, ft[p2] + 1);
        } else {
          ans += p1.second;
        }
      } else {
        long long tmp = get_val(ft, n) - get_val(ft, p2);
        if (tmp > 0) {
          if (p2 < n) {
            ans += p1.second;
            update(ft, p2 + 1, ft[p2 + 1] + 1);
          } else {
            ans += p1.first;
          }
        } else {
          ans += p1.first;
        }
      }
    }
    cout << max(ans, ans2) << "\n";
  }
  return 0;
}
