#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 7;
int k[MAXN], L[MAXN], R[MAXN];
long long solve(vector<pair<int, int> >& vec) {
  int n = vec.size();
  set<int> s;
  for (int i = 1; i <= n; i++) s.insert(i);
  sort(vec.begin(), vec.end());
  long long res = 0;
  for (pair<int, int> x : vec) {
    auto it = s.upper_bound(x.second);
    if (it == s.begin()) continue;
    it--;
    s.erase(it);
    res += x.first;
  }
  return res;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    long long ans = 0;
    vector<pair<int, int> > vec1, vec2;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d%d", &k[i], &L[i], &R[i]);
      if (L[i] >= R[i])
        vec1.push_back({L[i] - R[i], k[i]}), ans += R[i];
      else
        vec2.push_back({R[i] - L[i], n - k[i]}), ans += L[i];
    }
    ans += solve(vec1);
    ans += solve(vec2);
    printf("%lld\n", ans);
  }
  return 0;
}
