#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
vector<pair<int, int>> a, b;
priority_queue<int> Q;
int main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    a.clear();
    b.clear();
    int n;
    cin >> n;
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      if (l > r || k == n)
        ans += r, a.push_back(make_pair(k, l - r));
      else
        ans += l, b.push_back(make_pair(n - k + 1, r - l));
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    for (auto& g : a) {
      if (Q.size() < g.first)
        Q.push(g.second);
      else if (Q.top() < g.second)
        Q.pop(), Q.push(g.second);
    }
    while (Q.size()) ans += Q.top(), Q.pop();
    for (auto& g : b) {
      if (Q.size() < g.first)
        Q.push(g.second);
      else if (Q.top() < g.second)
        Q.pop(), Q.push(g.second);
    }
    while (Q.size()) ans += Q.top(), Q.pop();
    cout << ans << endl;
  }
  return 0;
}
