#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  for (int z = 0; z < T; z++) {
    int N;
    cin >> N;
    vector<pair<long long, pair<long long, long long>>> camels(N);
    long long ans = 0;
    for (int i = 0; i < N; i++) {
      long long K, L, R;
      cin >> K >> L >> R;
      camels[i] = {K, {L, R}};
      ans += L;
    }
    sort(camels.begin(), camels.end());
    int pos = 0;
    priority_queue<pair<long long, int>> pq;
    for (int i = 1; i <= N; i++) {
      while (pos <= N - 1 && camels[pos].first == i - 1) {
        pq.push({camels[pos].second.second - camels[pos].second.first, pos});
        pos++;
      }
      if (pq.empty()) continue;
      if (pq.top().first > 0 || pos >= i) {
        ans += pq.top().first;
        pq.pop();
      }
    }
    cout << ans << endl;
  }
  return 0;
}
