#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1000000007;
using ppi = pair<int, pair<int, int>>;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  for (int tc = 0; tc < T; ++tc) {
    int n;
    cin >> n;
    vector<ppi> a(n);
    set<pair<int, int>> lset;
    for (int i = 0; i < n; ++i) {
      cin >> a[i].first >> a[i].second.first >> a[i].second.second;
      --a[i].first;
      lset.insert({a[i].second.second - a[i].second.first, i});
    }
    sort(a.begin(), a.end());
    vector<bool> used(n, false);
    ll ans = 0;
    for (int i = 0, j = 0; i < n; ++i) {
      while (j < n && a[j].first < i) {
        if (!used[j]) {
          ans += a[j].second.second;
          lset.erase({a[j].second.second - a[j].second.first, j});
        }
        ++j;
      }
      if (lset.empty()) break;
      int idx = lset.begin()->second;
      ans += a[idx].second.first;
      used[idx] = true;
      lset.erase(lset.begin());
    }
    cout << ans << '\n';
  }
  return 0;
}
