#include <bits/stdc++.h>
using namespace std;
int T;
vector<int> PL[210000], MI[210000];
long long anslist[210000];
int main(void) {
  cin >> T;
  int x = 1;
  while (x <= T) {
    int ans = 0;
    int N;
    int K, L, R;
    cin >> N;
    for (int i = 0; i < N; i++) {
      cin >> K >> L >> R;
      if (L > R) {
        ans += L;
        PL[K].push_back(L - R);
      } else {
        ans += R;
        MI[K].push_back(R - L);
      }
    }
    priority_queue<int> PLC, MIC;
    int ple = 1;
    int anum = 0;
    for (int i = 1; i <= N; i++) {
      int SPL = PL[i].size();
      for (int j = 0; j < SPL; j++) {
        PLC.push(PL[i][j]);
        anum++;
      }
      PL[i].clear();
      if (anum > ple) {
        int dtimes = anum - ple;
        for (int i = 1; i <= dtimes; i++) {
          int dval = PLC.top();
          ans -= dval;
          PLC.pop();
        }
        while (!PLC.empty()) {
          PLC.pop();
        }
        ple = 0;
        anum = 0;
      }
      ple++;
    }
    ple = 0;
    anum = 0;
    for (int i = N; i >= 1; i--) {
      int SMI = MI[i].size();
      for (int j = 0; j < SMI; j++) {
        MIC.push(MI[i][j]);
        anum++;
      }
      MI[i].clear();
      if (anum > ple) {
        int dtimes = anum - ple;
        for (int i = 1; i <= dtimes; i++) {
          int dval = MIC.top();
          ans -= dval;
          MIC.pop();
        }
        while (!MIC.empty()) {
          MIC.pop();
        }
        ple = -1;
        anum = 0;
      }
      ple++;
    }
    anslist[x] = ans;
    x++;
  }
  for (int i = 1; i <= T; i++) cout << anslist[i] << endl;
  return 0;
}
