#include <bits/stdc++.h>
using namespace std;
int inf = 1000001000;
long long INF = 1e18;
long long mod = 1000000007;
long long MOD = INF;
int par[200010];
int rrank[200010];
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    rrank[i] = 0;
  }
}
int find(int x) {
  if (par[x] == x)
    return x;
  else
    return par[x] = find(par[x]);
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return;
  if (rrank[x] < rrank[y])
    par[x] = y;
  else {
    par[y] = x;
    if (rrank[x] == rrank[y]) rrank[x]++;
  }
}
bool same(int x, int y) { return find(x) == find(y); }
int main() {
  int t;
  cin >> t;
  for (int(ii) = 0; (ii) < (t); (ii)++) {
    int n;
    cin >> n;
    init(n + 1);
    long long o = 0, c = 0;
    vector<pair<pair<long long, long long>, pair<long long, long long>>> a(n);
    for (int(i) = 0; (i) < (n); (i)++)
      cin >> a[i].first.second >> a[i].second.first >> a[i].second.second;
    for (int(i) = 0; (i) < (n); (i)++)
      a[i].first.first = a[i].second.first - a[i].second.second;
    sort((a).begin(), (a).end());
    reverse((a).begin(), (a).end());
    for (int(i) = 0; (i) < (n); (i)++) {
      if (a[i].first.first < 0) break;
      if (find(a[i].first.second) == 0)
        c += a[i].second.second;
      else {
        c += a[i].second.first;
        o++;
        unite(a[i].first.second, a[i].first.second - 1);
      }
    }
    init(n + 2);
    reverse((a).begin(), (a).end());
    for (int(i) = 0; (i) < (o); (i)++) {
      unite(i + 1, o + 1);
    }
    for (int(i) = 0; (i) < (n); (i)++) {
      if (a[i].first.first >= 0) break;
      if (find(a[i].first.second + 1) == n + 1)
        c += a[i].second.first;
      else {
        c += a[i].second.second;
        unite(a[i].first.second + 2, a[i].first.second + 1);
      }
    }
    cout << c << endl;
  }
}
