#include <bits/stdc++.h>
using namespace std;
long long N, M, H, W, Q, K, A, B;
string S;
const long long MOD = (1e+9) + 7;
const long long INF = (1LL << 58);
int main() {
  cin >> Q;
  for (int _ = 0; _ < Q; _++) {
    cin >> N;
    vector<long long> k(N), p(N), ord(N), left(N + 1), right(N + 1);
    for (int i = 0; i < N + 1; i++) left[i] = right[i] = i;
    long long res = 0;
    for (int i = 0; i < N; i++) {
      cin >> k[i] >> A >> B;
      p[i] = A - B;
      ord[i] = i;
      res += B;
    }
    sort(ord.begin(), ord.end(), [&](int x, int y) { return p[x] > p[y]; });
    for (int i = 0; i < N; i++) {
      int id = ord[i], place = k[id];
      if (p[id] <= 0) break;
      if (left[place] != 0) {
        --left[place];
        while (left[place] != place) {
          place = left[place] = left[left[place]];
        }
        res += p[id];
      }
    }
    for (int i = N - 1; i >= 0; --i) {
      int id = ord[i], place = k[id];
      if (p[id] >= 0) break;
      if (right[place] != N) {
        ++right[place];
        while (right[place] != place) {
          place = right[place] = right[right[place]];
        }
      } else {
        res += p[id];
      }
    }
    cout << res << endl;
  }
}
