#include <bits/stdc++.h>
using namespace std;
using lint = long long;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    lint ans = 0;
    auto cmp = [&](pair<int, int> a, pair<int, int> b) {
      if (a.first < 0 && b.first >= 0) {
        return true;
      }
      if (a.first >= 0 && b.first < 0) {
        return false;
      }
      if (a.first >= 0 && b.first >= 0) {
        if (a.second != b.second) {
          return a.second > b.second;
        }
        return a.first < b.first;
      }
      if (a.second != b.second) {
        return a.second < b.second;
      }
      return a.first < b.first;
    };
    priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> pq(
        cmp);
    for (int i = 0; i < n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      ans += r;
      pq.push({l - r, k});
    }
    for (int i = 0; i < n; i++) {
      while (!pq.empty() && pq.top().second <= i) {
        pq.pop();
      }
      if (pq.empty()) {
        break;
      }
      ans += pq.top().first;
      pq.pop();
    }
    cout << ans << "\n";
  }
}
