#include <bits/stdc++.h>
using namespace std;
int sum[200005];
vector<int> v[200005];
multiset<int> dp2[200005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long ans = 0;
    for (int i = 1; i <= n; i++) v[i].clear();
    for (int i = 1; i <= n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      ans += r;
      v[k].emplace_back(l - r);
    }
    for (int i = 1; i <= n; i++) sum[i] = 0, dp2[i].clear();
    multiset<int> vis;
    for (int i = 1; i <= n; i++) vis.insert(i);
    multiset<pair<int, int> > dp;
    for (int i = 1; i <= n; i++) {
      sort(v[i].begin(), v[i].end());
      reverse(v[i].begin(), v[i].end());
      for (int j = 0; j < v[i].size(); j++) {
        int val = v[i][j];
        if (int(dp.size()) > i) {
          if (dp.begin()->first < val) {
            ans -= dp.begin()->first;
            dp2[dp.begin()->second].insert(dp.begin()->first);
            sum[dp.begin()->second]++;
            dp.erase(dp.begin());
            ans += val;
            dp.insert(make_pair(val, i));
          } else {
            dp2[i].insert(val);
            sum[i]++;
          }
        } else {
          if (vis.upper_bound(i) != vis.begin() && val >= 0) {
            auto it = (--(vis.upper_bound(i)));
            vis.erase(it);
            dp.insert(make_pair(val, i));
            ans += val;
          } else {
            dp2[i].insert(val);
            sum[i]++;
          }
        }
      }
      v[i].clear();
    }
    multiset<pair<int, int> > val;
    for (int i = n; i >= 1; i--) {
      if (i != n) sum[i] += sum[i + 1];
      for (auto it : dp2[i]) {
        val.insert(make_pair(it, i));
      }
      while (n - i < sum[i]) {
        int idx = val.rbegin()->second;
        ans += val.rbegin()->first;
        sum[i]--;
        val.erase(--val.end());
        break;
      }
    }
    cout << ans << "\n";
  }
}
