#include <bits/stdc++.h>
using namespace std;
int Solve(unordered_map<int, vector<int>>& cams, int N) {
  priority_queue<int> pq;
  int result = 0;
  for (int i = 0; i <= N; i++) {
    for (int cam : cams[i]) {
      result += cam;
      pq.push(-cam);
    }
    while (pq.size() > i) {
      result += pq.top();
      pq.pop();
    }
  }
  return result;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int testcases;
  cin >> testcases;
  while (testcases--) {
    int N;
    cin >> N;
    std::unordered_map<int, vector<int>> blueCams;
    std::unordered_map<int, vector<int>> redCams;
    int result = 0;
    for (int i = 0; i < N; i++) {
      int K, L, R;
      cin >> K >> L >> R;
      if (L >= R) {
        result += R;
        blueCams[K].push_back(L - R);
      } else {
        result += L;
        redCams[N - K].push_back(R - L);
      }
    }
    int final = result + Solve(blueCams, N) + Solve(redCams, N);
    std::cout << final << "\n";
  }
  return 0;
}
