#include <bits/stdc++.h>
using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;
constexpr llong mod = 1e9 + 7;
constexpr llong inf = mod * mod;
int main() {
  llong T;
  cin >> T;
  while (T--) {
    llong N;
    vector<vector<lpair>> v;
    cin >> N;
    v.resize(N + 1);
    for (int i = 0; i < N; i++) {
      llong K, L, R;
      cin >> K >> L >> R;
      v[K].push_back(lpair(L, R));
    }
    llong ans = 0;
    priority_queue<llong> que;
    for (int i = 1; i <= N; ++i) {
      if ((int)que.size() && que.top() >= 0) {
        ans += que.top();
        que.pop();
      }
      for (auto vv : v[i]) {
        ans += vv.first;
        que.push(vv.second - vv.first);
      }
      while ((int)que.size() > i) {
        ans += que.top();
        que.pop();
      }
    }
    cout << ans << endl;
  }
  return 0;
}
