#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  while (T--) {
    int N;
    cin >> N;
    vector<pii> v1, v2;
    ll result = 0;
    for (int i = 0; i < (N); ++i) {
      int K, L, R;
      cin >> K >> L >> R;
      int D = L - R;
      if (D > 0) {
        v1.emplace_back(K - 1, D);
      } else {
        if (N - K - 1 >= 0) {
          v2.emplace_back(N - K - 1, -D);
        }
      }
      result += min(L, R);
    }
    function<ll(vector<pii>)> f = [&](vector<pii> v) {
      sort((v).rbegin(), (v).rend());
      ll ret = 0;
      int cnt = 0;
      for (int i = 0; i < (N); ++i) {
        priority_queue<int> q;
        while (!v.empty() && v.back().first == i) {
          q.push(v.back().second);
          v.pop_back();
        }
        while (!q.empty() && cnt < i + 1) {
          ret += q.top();
          q.pop();
          ++cnt;
        }
      }
      return ret;
    };
    result += f(v1) + f(v2);
    cout << result << '\n';
  }
  cout << flush;
  return 0;
}
