#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

ll int get(int n, auto &pack) {
   ll res = 0;
   set <int> idx;
   for (int i = 1; i <= n; i++) {
      idx.insert(i);
   }

   for (auto [v, p] : pack) {
      auto x = idx.upper_bound(p);
      x--;
      if (x != idx.end()) {
         res += v;
         idx.erase(x);
      }
   }

   return res;
}

void solve() {
   int n;
   cin >> n;
   ll ans = 0;
   vector <pair <int, int>> pos, neg;
   for (int i = 0; i < n; i++) {
      int k, l, r;
      cin >> k >> l >> r;
      if (l > r)
         pos.emplace_back(l - r, k);
      if (l < r)
         neg.emplace_back(r - l, n - k);
      ans += min(l, r);
   }
   sort(pos.rbegin(), pos.rend());
   sort(neg.rbegin(), neg.rend());

   cout << ans + get(n, pos) + get(n, neg) << '\n';
}

int main() {
#ifndef ONLINE_JUDGE
   freopen("inp.txt", "r", stdin);      freopen("outp.txt", "w", stdout);
#endif
   ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);

   int t;
   cin >> t;
   while (t--)
      solve();

   return 0;
}