#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long calc(vector<pair<int, int>> &a) {
  int n = a.size();
  set<int> free;
  long long ans = 0;
  for (int i = 1; i <= n; i++) free.insert(i);
  for (auto p : a) {
    auto it = free.upper_bound(p.second);
    if (it != free.begin()) {
      ans += p.first;
      --it;
      free.erase(it);
    }
  }
  return ans;
}
void solve() {
  int n;
  cin >> n;
  vector<pair<int, int>> pos, neg;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int k, l, r;
    cin >> k >> l >> r;
    if (l > r) {
      ans += r;
      pos.push_back({l - r, k});
    } else {
      ans += l;
      neg.push_back({r - l, n - k});
    }
  }
  cout << ans + calc(pos) + calc(neg) << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tt;
  cin >> tt;
  for (int tc = 1; tc <= tt; ++tc) {
    solve();
  }
}
