#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll solve() {
  int n;
  cin >> n;
  vector<priority_queue<ll>> camel(n);
  ll idol = 0;
  ll ans;
  for (int i = 0; i < n; i++) {
    ll k, l, r;
    cin >> k >> l >> r;
    k--;
    idol += l;
    camel[k].push(l - r);
  }
  ll index = -1;
  ll distance = 0;
  priority_queue<ll> que;
  for (int i = 0; i < n; i++) {
    que = camel[i];
    if (i == n - 1) continue;
    while (1) {
      if (index == i || que.empty()) break;
      if (que.top() < 0) break;
      que.pop();
      index++;
    }
    while (!que.empty()) {
      distance += que.top();
      que.pop();
    }
  }
  ans = idol - distance;
  cout << ans << endl;
  return ans;
}
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    solve();
  }
}
