#include <bits/stdc++.h>

using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;

#define ALL(x) x.begin(), x.end()

constexpr llong mod = 1e9+7;
constexpr llong inf = mod * mod;

int main() {
  llong T;
  cin >> T;
  for (int i = 0; i < T; i++) {
    llong N;
    vector<vector<lpair>> v;

    cin >> N;
    v.resize(N + 1);
    for (int j = 0; j < N; ++j) {
      llong K, L, R;
      cin >> K >> L >> R;
      v[K].push_back(lpair(L, R));
    }

    llong ans = 0;
    priority_queue<llong> after;
    llong filled = 0;
    for (int j = 1; j <= N; ++j) {
      if (v[j].size() == 0) continue;
      sort(ALL(v[j]), greater<>());
      for (int k = 0; k < v[j].size() && after.size(); k++) {
        if (after.top() > 0) {
          ans += after.top();
          after.pop();
        }
        else {
          break;
        }
      }
      for (auto vv: v[j]) {
        if (filled < j) {
          filled++;
          ans += vv.first;
          after.push(vv.second - vv.first);
        }
        else {
          after.push(vv.second);
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}