#include <bits/stdc++.h>
int n, t;
int K[200000];
int L[200000];
int R[200000];
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) scanf("%d%d%d", K + i, L + i, R + i);
    std::vector<std::pair<int, int> > v(n);
    for (int i = 0; i < n; ++i)
      v[i] = std::pair<int, int>(K[i] - 1, L[i] - R[i]);
    std::sort(v.begin(), v.end());
    long long int r = 0;
    for (int i = 0; i < n; ++i) r += std::min(L[i], R[i]);
    std::priority_queue<int, std::vector<int>, std::less<int> > q0;
    for (int i = 0, j = 0; i < n && j < n; ++i) {
      while (v[j].first == i) {
        if (v[j].second > 0) q0.push(v[j].second);
        ++j;
      }
      while (q0.size() > i + 1) q0.pop();
    }
    while (!q0.empty()) {
      r += q0.top();
      q0.pop();
    }
    std::priority_queue<int, std::vector<int>, std::greater<int> > q1;
    for (int i = n - 1, j = n - 1; i >= 0 && j >= 0; --i) {
      while (v[j].first == i) {
        if (v[j].second < 0) q1.push(v[j].second);
        --j;
      }
      while (q1.size() > n - i - 1) q1.pop();
    }
    while (!q1.empty()) {
      r -= q1.top();
      q1.pop();
    }
    printf("%lld\n", r);
  }
  return 0;
}
