#include <bits/stdc++.h>
using namespace std;
vector<int> v[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++) {
      int k, l, r;
      cin >> k >> l >> r;
      ans += r;
      v[k].emplace_back(l - r);
    }
    multiset<int> dp;
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      sort(v[i].begin(), v[i].end());
      reverse(v[i].begin(), v[i].end());
      int r = 0;
      for (int j = 0; j < v[i].size(); j++) {
        sum++;
        int val = v[i][j];
        if (int(dp.size()) > i) {
          if (*dp.begin() < val) {
            ans -= *dp.begin();
            dp.erase(dp.begin());
            ans += val;
            dp.insert(val);
          } else {
            r++;
          }
        } else {
          if (val >= 0) {
            dp.insert(val);
            ans += val;
          } else {
            r++;
          }
        }
        if (r > n - i) {
          r--;
          dp.insert(val);
          ans += val;
        }
      }
      v[i].clear();
    }
    cout << ans << "\n";
  }
}
