#include <bits/stdc++.h>
using namespace std;
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;
    int blueCamelsSize = 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);
        blueCamelsSize++;
      } else {
        result += L;
        redCams[K].push_back(R - L);
      }
    }
    priority_queue<int, vector<int>, greater<int>> bpq;
    priority_queue<int, vector<int>, greater<int>> rpq;
    for (int i = 0; i <= N; i++) {
      for (int camel : blueCams[i]) bpq.push(camel);
      while (bpq.size() > i) bpq.pop();
    }
    for (int i = N; i >= 0; i--) {
      for (int camel : redCams[i]) rpq.push(camel);
      while (rpq.size() > N - i - 1) rpq.pop();
    }
    while (!bpq.empty()) {
      result += bpq.top();
      bpq.pop();
    }
    while (!rpq.empty()) {
      result += rpq.top();
      rpq.pop();
    }
    std::cout << result << "\n";
  }
  return 0;
}
