#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct camel {
  int k;
  int d;
};
int main() {
  int t;
  cin >> t;
  for (int ti = 0; ti < t; ++ti) {
    int n;
    cin >> n;
    vector<camel> vc_l, vc_r;
    ll sum = 0;
    for (int ci = 0; ci < n; ++ci) {
      int k, l, r;
      cin >> k >> l >> r;
      if (l > r) {
        sum += r;
        vc_l.emplace_back(camel{k, l - r});
      } else if (l == r) {
        sum += r;
      } else {
        sum += l;
        vc_r.emplace_back(camel{k, r - l});
      }
    }
    sort(vc_l.begin(), vc_l.end(), [](const camel &a, const camel &b) {
      return a.k < b.k || (a.k == b.k && a.d > b.d);
    });
    int k = 0;
    for (const auto &c : vc_l) {
      if (k < c.k) {
        sum += c.d;
        k++;
      }
    }
    sort(vc_r.begin(), vc_r.end(),
         [](const camel &a, const camel &b) { return a.k > b.k; });
    k = n;
    for (const auto &c : vc_r) {
      if (k > c.k) {
        sum += c.d;
        k--;
      }
    }
    cout << sum << endl;
  }
  return 0;
}
