#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<vector<pair<int, int>>> f(2);
    vector<vector<int>> v(2);
    vector<int> cnt(2);
    ll sum = 0;
    for (int i = 0; i < n; ++i) {
      int k, l, r;
      cin >> k >> l >> r;
      if (l >= r) {
        f[0].emplace_back(k, r - l);
        sum += r;
      } else {
        if (n - k == 0) {
          sum += l;
          continue;
        }
        f[1].emplace_back(n - k, l - r);
        sum += l;
      }
    }
    for (auto& p : f) sort(p.begin(), p.end());
    for (int j = 0; j < 2; ++j)
      for (int i = 0; i < f[j].size(); ++i)
        if (f[j][i].first >= i + 1) {
          ++cnt[j];
          v[j].emplace_back(f[j][i].second);
        }
    for (auto& p : v) sort(p.begin(), p.end());
    for (int j = 0; j < 2; ++j)
      for (int i = 0; i < cnt[j]; ++i) sum -= v[j][i];
    cout << sum << '\n';
  }
}
