#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  for (int t = 0; t < (T); ++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] && K[i] < N)
        right.emplace(K[i] + 1, R[i] - L[i]);
      ans += min(L[i], R[i]);
    }
    auto it_left = left.begin();
    auto it_right = right.rbegin();
    for (int i = 1; i <= N; i++) {
      while (it_left != left.end() && (*it_left).first <= i) {
        left2.emplace((*it_left).second, (*it_left).first);
        it_left++;
      }
      while (left2.size() > i) left2.erase(left2.begin());
    }
    for (int i = N; i >= 1; i--) {
      while (it_right != right.rend() && (*it_right).first >= i) {
        right2.emplace((*it_right).second, (*it_right).first);
        it_right++;
      }
      while (right2.size() > (N - i + 1)) right2.erase(*(right2.begin()));
    }
    for (auto it = left2.begin(); it != left2.end(); it++) ans += (*it).first;
    for (auto it = right2.begin(); it != right2.end(); it++) ans += (*it).first;
    cout << ans << endl;
  }
  return 0;
}
