#include <bits/stdc++.h>
using namespace std;
const long long int maxn = 2e5 + 5;
long long int k[maxn], l[maxn], r[maxn];
vector<long long int> v;
long long int left2[maxn], right2[maxn];
bool comp(long long int i, long long int j) {
  return abs(r[i] - l[i]) > abs(r[j] - l[j]);
}
void solve() {
  long long int n;
  cin >> n;
  v.clear();
  for (long long int i = 1; i <= n; i++) {
    v.push_back(i);
    cin >> k[i] >> l[i] >> r[i];
    left2[i] = i;
    right2[i] = i;
  }
  sort(v.begin(), v.end(), comp);
  long long int ans = 0;
  for (long long int i : v) {
    if (r[i] > l[i]) {
      if (right2[k[i]] >= n)
        ans += l[i];
      else {
        left2[right2[k[i]]] = left2[k[i]];
        right2[k[i]] = right2[right2[k[i]]];
        ans += r[i];
      }
    } else {
      if (right2[k[i]] == left2[k[i]]) {
        ans += l[i];
        right2[k[i]] = right2[right2[k[i]]];
        left2[k[i]] = left2[left2[k[i]]];
      } else if (left2[k[i]] < 1)
        ans += r[i];
      else {
        right2[left2[k[i]]] = right2[k[i]];
        left2[k[i]] = left2[left2[k[i]]];
        ans += l[i];
      }
    }
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
