#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    int N;
    cin >> N;
    vector<int> K(N), L(N), R(N);
    for (int i = 0; i < N; i++) {
      cin >> K[i] >> L[i] >> R[i];
    }
    vector<int> pt(N, 0);
    long long int score = 0;
    for (int i = 0; i < N; i++) {
      int target = K[i];
      if (pt[target] == 0) {
        pt[target] = i;
        score += L[i];
      } else if (L[pt[target]] < L[i]) {
        score -= L[pt[target]];
        score += L[i];
        pt[target] = i;
      }
    }
    cout << score << endl;
  }
  return 0;
}
