#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    long long ans = 0;
    cin >> n;
    vector<pair<int, pair<long long, long long>>> a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i].first >> a[i].second.first >> a[i].second.second;
    vector<vector<pair<long long, int>>> add(n + 1);
    for (int i = 0; i < n; i++) {
      int k;
      long long l, r;
      a[i].first--;
      k = a[i].first;
      l = a[i].second.first;
      r = a[i].second.second;
      add[k].push_back({r - l, i});
    }
    priority_queue<pair<long long, int>> pq;
    vector<bool> used(n + 1, 0);
    for (int i = 0; i < n; i++) {
      if (!pq.empty() && pq.top().first >= 0) {
        int idx = pq.top().second;
        pq.pop();
        used[i] = 1;
        ans += a[idx].second.second;
      }
      for (auto u : add[i]) pq.push(u);
    }
    vector<pair<long long, int>> tmp;
    while (!pq.empty()) {
      tmp.push_back({-pq.top().first, pq.top().second});
      pq.pop();
    }
    for (auto u : tmp) pq.push(u);
    for (int i = n - 1; i >= 0; i--) {
      if (used[i]) continue;
      if (pq.empty()) break;
      int idx = pq.top().second;
      pq.pop();
      ans += a[idx].second.first;
    }
    cout << ans << "\n";
  }
}
