#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<ll, ll>;
const int oo = 1e9 + 7;
const ll mod = 1e9 + 7, maxn = (2 * 1e5) + 10, maxm = (1 << 18);
const ld PI = acos(-1);
bool check(ll a, ll b) { return a > b; }
int main() {
  ios_base::sync_with_stdio(false);
  int tt;
  cin >> tt;
  while (tt--) {
    ll n;
    cin >> n;
    vector<vector<ll>> rs(maxn), ls(maxn);
    vector<ll> ans(n + 1, -1);
    for (int i = 0; i < n; i++) {
      ll l, r, k;
      cin >> k >> l >> r;
      k--;
      rs[k + 1].push_back(r);
      ls[k].push_back(l);
    }
    multiset<ll> tmp;
    for (int i = 0; i < n; i++) {
      for (auto it : rs[i]) tmp.insert(it);
      if (tmp.empty()) continue;
      ans[i] = *prev(tmp.end());
    }
    tmp.clear();
    ll res = 0;
    for (int i = n - 1; i >= 0; i++) {
      for (auto it : ls[i]) tmp.insert(it);
      if (tmp.empty()) continue;
      ans[i] = max(ans[i], ll(*prev(tmp.end())));
      res += ans[i];
    }
    cout << res << endl;
  }
  return 0;
}
