#include <bits/stdc++.h>
using namespace std;
priority_queue<int> pq;
void apply(bool flag, int l, int r, long long &sum) {
  if (flag) {
    sum += r;
    if (r - l < pq.top()) {
      sum += pq.top();
      pq.pop();
      sum += -r + l;
      pq.push(-l + r);
    }
  } else {
    sum += l;
    pq.push(-l + r);
  }
}
void clr() {
  while (!pq.empty()) {
    pq.pop();
  }
  pq.push(-2 * 1000000000);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t, n, i, l, r, k, sz;
  long long sum;
  cin >> t;
  while (t-- > 00) {
    vector<pair<int, pair<int, int>>> a[2];
    cin >> n;
    for (i = 1; i <= n; ++i) {
      cin >> k >> l >> r;
      a[l < r].push_back({k, {l, r}});
    }
    sort(a[0].begin(), a[0].end());
    sort(a[1].begin(), a[1].end());
    sum = 0;
    clr();
    sz = a[0].size();
    for (i = 0; i < sz; ++i) {
      l = a[0][i].second.first;
      r = a[0][i].second.second;
      k = a[0][i].first;
      apply(i + 1 > k, l, r, sum);
    }
    clr();
    sz = a[1].size();
    for (i = sz - 1; i >= 0; --i) {
      l = a[1][i].second.first;
      r = a[1][i].second.second;
      k = a[1][i].first;
      swap(l, r);
      apply(n - sz + i - 1 < k, l, r, sum);
    }
    cout << sum << "\n";
  }
  return 0;
}
