#include <bits/stdc++.h>
using namespace std;
using lint = long long;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    lint ans = 0;
    vector<vector<int>> left(n), right(n);
    for (int i = 0; i < n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      k--;
      ans += min(l, r);
      if (l - r > 0) {
        left[k].push_back(l - r);
      } else if (l - r < 0 && k + 1 < n) {
        right[k + 1].push_back(r - l);
      }
    }
    auto Calc = [&](int piv) {
      lint res = 0;
      priority_queue<int> pq;
      for (int i = 0; i < n; i++) {
        for (auto j : right[i]) {
          pq.push(j);
        }
        if (piv <= i && !pq.empty()) {
          res += pq.top();
          pq.pop();
        }
      }
      while (!pq.empty()) pq.pop();
      for (int i = n - 1; i >= 0; i--) {
        for (auto j : left[i]) {
          pq.push(j);
        }
        if (i < piv && !pq.empty()) {
          res += pq.top();
          pq.pop();
        }
      }
      return res;
    };
    int lo = 0, hi = n;
    while (lo < hi) {
      int md = (lo + hi) / 2;
      if (Calc(md) > Calc(md + 1)) {
        hi = md;
      } else {
        lo = md + 1;
      }
    }
    cout << ans + Calc(lo) << "\n";
  }  
}
