#include <bits/stdc++.h>
using namespace std;
int n;
long long sum;
inline void solve() {
  vector<pair<int, int> > v1, v2;
  set<int> s1, s2;
  cin >> n;
  sum = 0;
  for (int i = 1, l, r, k; i <= n; ++i) {
    cin >> k >> l >> r;
    sum += r;
    if (l > r)
      v1.push_back({l - r, k});
    else
      v2.push_back({l - r, k});
  }
  for (int i = 1; i <= v1.size(); ++i) s1.insert(i);
  for (int i = v1.size() + 1; i <= n; ++i) s2.insert(i);
  sort(v1.rbegin(), v1.rend());
  sort(v2.begin(), v2.end());
  for (int i = 0; i < v1.size(); ++i) {
    pair<int, int> p = v1[i];
    set<int>::iterator it = s1.upper_bound(p.second);
    if (it != s1.begin()) {
      --it;
      s1.erase(it);
      sum += p.first;
    }
  }
  for (int i = 0; i < v2.size(); ++i) {
    pair<int, int> p = v2[i];
    set<int>::iterator it = s2.upper_bound(p.second);
    if (it == s2.end())
      sum += p.first;
    else
      s2.erase(it);
  }
  cout << sum << '\n';
}
int main() {
  freopen("b.in", "r", stdin);
  freopen("b.out", "w", stdout);
  ios_base::sync_with_stdio(false);
  int T;
  cin >> T;
  for (; T--;) solve();
}
