#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  for (int i = 0; i < (t); ++i) {
    int n;
    ll ans = 0;
    cin >> n;
    vector<bool> c(n + 1, true);
    vector<int> k(n), l(n), r(n);
    vector<P> q(n);
    for (int i = 0; i < (n); ++i) {
      cin >> k[i] >> l[i] >> r[i];
      q[i].first = abs(l[i] - r[i]);
      q[i].second = i;
    }
    sort(q.begin(), q.end());
    reverse(q.begin(), q.end());
    for (int i = 0; i < (n); ++i) {
      int x = q[i].second;
      if (l[x] > r[x]) {
        int y = k[x] - 1;
        while (1) {
          if (y < 0) {
            ans += r[x];
            break;
          }
          if (c[y]) {
            c[y] = false;
            ans += l[x];
            break;
          }
          --y;
        }
      } else if (l[x] < r[x]) {
        int y = k[x];
        while (1) {
          if (y == n) {
            ans += l[x];
            break;
          }
          if (c[y]) {
            c[y] = false;
            ans += r[x];
            break;
          }
          ++y;
        }
      } else {
        ans += l[x];
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
