#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
ll solve() {
  int n;
  cin >> n;
  ll ans = 0;
  map<int, vector<int>> lmap;
  for (int i = 0; i < (n); ++i) {
    int k, l, r;
    cin >> k >> l >> r;
    if (l - r > 0) {
      lmap[k].push_back(r - l);
    }
    if (k == n) {
      ans += l;
    } else {
      ans += max(l, r);
    }
  }
  int waku = 0;
  priority_queue<int> que;
  for (int i = 0; i < (n - 1); ++i) {
    int num = i + 1;
    waku = num;
    auto lrs = lmap[i];
    for (auto lr : lrs) {
      que.push(lr);
    }
    if (que.size() <= waku) {
      continue;
    }
    while (que.size() >= waku) {
      int sub = que.top();
      ans += sub;
      que.pop();
    }
  }
  return ans;
}
int main() {
  int t;
  cin >> t;
  vector<ll> ans;
  for (int i = 0; i < (t); ++i) {
    int a = solve();
    ans.push_back(a);
  }
  for (auto v : ans) {
    cout << v << endl;
  }
  return 0;
}
