#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
int f(vector<P> list) {
  int n = list.size();
  for (int i = 0; i < (n); i++) list[i].first = min(list[i].first, n);
  vector<vector<int> > ps(n + 1);
  for (int i = 0; i < n; i++) {
    ps[list[i].first].push_back(list[i].second);
  }
  priority_queue<int> que;
  int sum = 0;
  for (int i = n; i >= 1; i--) {
    for (int j : ps[i]) {
      que.push(j);
    }
    if (!que.empty()) {
      sum += que.top();
      que.pop();
    }
  }
  return sum;
}
void solve() {
  int N;
  cin >> N;
  vector<P> pl, pr;
  int ans = 0;
  for (int i = 0; i < (N); i++) {
    int k, l, r;
    cin >> k >> l >> r;
    int m = min(l, r);
    l -= m;
    r -= m;
    ans += m;
    if (l > 0)
      pl.emplace_back(k, l);
    else
      pr.emplace_back(N - k, r);
  }
  ans += f(pl);
  ans += f(pr);
  cout << ans << endl;
}
int main() {
  int T;
  cin >> T;
  for (int i = 0; i < (T); i++) solve();
}
