#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    ll ans = 0;
    vector<vector<ll>> x(n, vector<ll>(3)), stock(n);
    for (int i = 0; i < n; i++) {
      cin >> x[i][0] >> x[i][1] >> x[i][2];
      x[i][0]--;
      if (x[i][1] > x[i][2] || x[i][0] == n - 1) {
        stock[x[i][0]].emplace_back((x[i][1] - x[i][2]) * 2);
        ans += x[i][1];
      } else {
        ans += x[i][2];
      }
    }
    priority_queue<ll> pq;
    for (int i = n - 1; i >= 0; i--) {
      for (int j = 0; j < stock[i].size(); j++) {
        pq.push(stock[i][j]);
      }
      if (!pq.empty()) pq.pop();
    }
    while (!pq.empty()) {
      ans -= pq.top();
      pq.pop();
    }
    cout << ans << '\n';
  }
}
