#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  for (int t = 0; t < (T); ++t) {
    printf("test: %d\n", t);
    int N;
    cin >> N;
    long long ans = 0;
    vector<int> K(N), L(N), R(N);
    for (int i = 0; i < (N); ++i) cin >> K[i] >> L[i] >> R[i];
    multiset<pair<int, int> > left, right, left2, right2;
    for (int i = 0; i < (N); ++i) {
      if (L[i] > R[i])
        left.emplace(K[i], L[i] - R[i]);
      else if (L[i] < R[i])
        right.emplace(K[i], R[i] - L[i]);
      ans += min(L[i], R[i]);
    }
    int i = 1, j = N;
    auto it_left = left.begin();
    auto it_right = right.rbegin();
    while (true) {
      while (it_left != left.end() && (*it_left).first <= i) {
        pair<int, int> p = *it_left;
        left2.emplace(p.second, p.first);
        it_left++;
      }
      while (left2.size() > 0 && (*left2.rbegin()).second > i)
        left2.erase(*(left2.rbegin()));
      while (it_right != right.rend() && (*it_right).first >= j) {
        pair<int, int> p = *it_right;
        right2.emplace(p.second, p.first);
        it_right++;
      }
      while (right2.size() > 0 && (*right2.rbegin()).second < j)
        right2.erase(*(right2.rbegin()));
      if (left2.size() == 0 && right2.size() == 0) break;
      pair<int, int> l = *(left2.rbegin());
      pair<int, int> r = *(right2.rbegin());
      bool isLeft = true;
      if (left2.size() == 0)
        isLeft = false;
      else if (right2.size() == 0)
        isLeft = true;
      else
        isLeft = l.first >= r.first;
      if (!isLeft) {
        ans += r.first;
        right2.erase(*(right2.rbegin()));
        j--;
        i = min(i, j);
      } else if (right2.size() == 0) {
        ans += l.first;
        left2.erase(*(left2.rbegin()));
        i++;
        j = max(i, j);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
