#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
vector<pair<long long, long long> > v[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long n, ans = 0;
    cin >> n;
    for (int i = 0, ThxDem = n; i < ThxDem; ++i) v[i].clear();
    for (int i = 0, ThxDem = n; i < ThxDem; ++i) {
      int k, l, r;
      cin >> k >> l >> r;
      ans += r;
      v[k - 1].push_back({l - r, k - 1});
    }
    multiset<pair<long long, long long> > did;
    for (int i = 0, ThxDem = n; i < ThxDem; ++i) {
      for (auto x : v[i]) {
        if (int(did.size()) < i + 1)
          did.insert(x);
        else if (did.begin()->first < x.first)
          did.erase(did.begin()), did.insert(x);
      }
    }
    vector<pair<long long, long long> > h;
    for (auto x : did) h.push_back(x);
    sort(h.begin(), h.end());
    reverse(h.begin(), h.end());
    for (int i = 0, ThxDem = n; i < ThxDem; ++i) {
      if (h[i].second >= i)
        ans += h[i].first;
      else if (h[i].first >= 0)
        ans += h[i].first;
    }
    cout << ans << "\n";
  }
}
