#include <bits/stdc++.h>
using namespace std;
long long int t, n, m, i, j, k, l, arr[200009], brr[200009], crr[200009], r, a,
    b;
vector<pair<long long int, long long int> > v, v1;
int main() {
  scanf("%lld", &t);
  while (t--) {
    scanf("%lld", &n);
    m = 0;
    for (i = 1; i <= n; i++) {
      scanf("%lld %lld %lld", &k, &l, &r);
      if (l > r)
        v.push_back({k, -(l - r)});
      else if (r > l)
        v1.push_back({-(k + 1), -(r - l)});
      m += min(l, r);
    }
    sort(v.begin(), v.end());
    sort(v1.begin(), v1.end());
    l = 0;
    for (i = 0; i < v.size(); i++) {
      a = v[i].first;
      if (a <= l) continue;
      b = -v[i].second;
      m += b;
      l++;
    }
    l = n + 1;
    for (i = 0; i < v1.size(); i++) {
      a = -v1[i].first;
      if (a >= l) continue;
      b = -v1[i].second;
      m += b;
      l--;
    }
    printf("%lld\n", m);
    v.clear();
    v1.clear();
  }
  return 0;
}
