#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int t;
  cin >> t;
  for (int ti = 0; ti < t; ti++) {
    int n, ans = 0;
    cin >> n;
    tuple<int, int, int> tup[n];
    for (int ki = 0; ki < n; ki++) {
      int k, l, r;
      cin >> k >> l >> r;
      if (l > r) {
        tup[ki] = make_tuple(l - r, k, 0);
        ans += r;
      } else {
        tup[ki] = make_tuple(r - l, k, 1);
        ans += l;
      }
    }
    sort(tup, tup + n, greater<tuple<int, int, int>>());
    set<int> lorder, rorder;
    for (int i = 1; i <= n; i++) {
      lorder.insert(i);
      rorder.insert(i);
    }
    for (auto skl : tup) {
      if (get<2>(skl) == 0) {
        auto itr = lorder.upper_bound(get<1>(skl));
        if (itr == lorder.begin()) continue;
        itr--;
        ans += get<0>(skl);
        lorder.erase(*itr);
      } else {
        auto itr = rorder.upper_bound((n - get<1>(skl)));
        if (itr == rorder.begin()) continue;
        itr--;
        ans += get<0>(skl);
        rorder.erase(*itr);
      }
    }
    cout << ans << endl;
  }
}
