#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n;
    cin >> n;
    int k[n], l[n], r[n];
    for (int i = 0; i < n; i++) {
      cin >> k[i] >> l[i] >> r[i];
      k[i]--;
    }
    tuple<int, int, bool> a[n];
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      ans += min(l[i], r[i]);
      if (l[i] >= r[i]) {
        a[i] = tuple<int, int, bool>(l[i] - r[i], k[i], true);
      } else {
        a[i] = tuple<int, int, bool>(r[i] - l[i], k[i], false);
      }
    }
    sort(a, a + n);
    reverse(a, a + n);
    int b = 0;
    int c = n - 1;
    for (int i = 0; i < n; i++) {
      int d;
      int e;
      bool f;
      tie(d, e, f) = a[i];
      if (f) {
        if (b <= e) {
          ans += d;
          b++;
        } else {
          c--;
        }
      } else {
        if (c >= e) {
          ans += d;
          c--;
        } else {
          b++;
        }
      }
    }
    cout << ans << endl;
  }
}
