#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
struct Data {
  bool left;
  ll bound;
  ll toku;
};
bool operator<(const Data& lhs, const Data& rhs) { return lhs.toku < rhs.toku; }
int main() {
  int testcases;
  cin >> testcases;
  for (int((ttt)) = (0); ((ttt)) < ((testcases)); ((ttt))++) {
    int n;
    cin >> n;
    vector<ll> k(n), l(n), r(n);
    for (int((i)) = (0); ((i)) < ((n)); ((i))++) {
      cin >> k[i] >> l[i] >> r[i];
      k[i]--;
    }
    ll sumx = 0;
    for (int((i)) = (0); ((i)) < ((n)); ((i))++) {
      sumx += min(l[i], r[i]);
    }
    priority_queue<Data> pq;
    for (int((i)) = (0); ((i)) < ((n)); ((i))++) {
      if (l[i] > r[i])
        pq.push({true, k[i], l[i] - r[i]});
      else {
        pq.push({false, k[i], r[i] - l[i]});
      }
    }
    int left_avail = n - 1;
    int right_avail = 0;
    vector<bool> used(n, false);
    vector<int> left_pos(n);
    vector<int> right_pos(n);
    iota(left_pos.begin(), left_pos.end(), 0);
    iota(right_pos.begin(), right_pos.end(), 0);
    while (!pq.empty()) {
      auto top = pq.top();
      pq.pop();
      if (top.left) {
        int pos = left_pos[top.bound];
        if (pos == -1) continue;
        left_pos[pos] = (pos == 0 ? -1 : left_pos[pos - 1]);
        sumx += top.toku;
      } else {
        if (top.bound == n - 1) continue;
        int pos = right_pos[top.bound + 1];
        if (pos == n) continue;
        right_pos[pos] = (pos == n - 1 ? n : right_pos[pos + 1]);
        sumx += top.toku;
      }
    }
    cout << sumx << endl;
  }
  return 0;
}
