#include <bits/stdc++.h>
using namespace std;
pair<long long, pair<long long, long long>> a[200001];
int p[200001];
int p2[200001];
bool check[200001];
int Find(int x) {
  if (x == p[x])
    return x;
  else
    return p[x] = Find(p[x]);
}
void merge(int x, int y) {
  x = Find(x);
  y = Find(y);
  if (x < y) {
    p[y] = x;
  } else {
    p[x] = y;
  }
}
int Find2(int x) {
  if (x == p2[x])
    return x;
  else
    return p2[x] = Find2(p2[x]);
}
void merge2(int x, int y) {
  x = Find2(x);
  y = Find2(y);
  if (x < y) {
    p2[x] = y;
  } else
    p2[y] = x;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      p[i] = i;
      p2[i] = i;
      check[i] = false;
    }
    priority_queue<pair<long long, int>> q;
    for (int i = 0; i < n; i++) {
      cin >> a[i].first >> a[i].second.first >> a[i].second.second;
      q.push({abs(a[i].second.first - a[i].second.second), i});
    }
    long long ans = 0;
    while (!q.empty()) {
      int x = q.top().second;
      q.pop();
      if (a[x].second.first > a[x].second.second) {
        int y = Find(a[x].first);
        if (y == 0) continue;
        check[x] = true;
        ans += a[x].second.first;
        merge(y, y - 1);
      } else if (a[x].second.first < a[x].second.second) {
        int y = Find2(a[x].first + 1);
        if (y == 0) continue;
        check[x] = true;
        ans += a[x].second.second;
        merge(y, y + 1);
      }
    }
    for (int i = 0; i < n; i++) {
      if (!check[i]) {
        ans += min(a[i].second.second, a[i].second.first);
      }
    }
    cout << ans << '\n';
  }
}
