#include <bits/stdc++.h>
using namespace std;
class Link {
 public:
  Link(long long int n) : left(n), right(n), filled(n, false) {
    for (long long int i = 0; i < ((long long int)(n)); i++) {
      left[i] = i - 1;
      right[i] = i + 1;
    }
  }
  bool Fill(long long int pos, bool dir_right) {
    if (pos < 0 || pos >= ((long long int)(filled).size())) {
      return false;
    }
    if (!filled[pos]) {
      filled[pos] = true;
      if (right[pos] < ((long long int)(filled).size())) {
        left[right[pos]] = left[pos];
      }
      if (left[pos] >= 0) {
        right[left[pos]] = right[pos];
      }
      if (right[pos] < ((long long int)(filled).size())) {
        right[pos] = right[right[pos]];
      }
      if (left[pos] >= 0) {
        left[pos] = left[left[pos]];
      }
      return true;
    } else {
      if (dir_right) {
        return Fill(right[pos], dir_right);
      } else {
        return Fill(left[pos], dir_right);
      }
    }
  }
  vector<long long int> left;
  vector<long long int> right;
  vector<bool> filled;
};
inline void solve() {
  long long int n;
  long long int base = 0;
  cin >> n;
  vector<pair<long long int, pair<long long int, long long int>>> v;
  for (long long int i = 0; i < ((long long int)(n)); i++) {
    long long int k, l, r;
    cin >> k >> l >> r;
    k--;
    base += min(l, r);
    long long int diff = abs(l - r);
    if (l > r) {
      v.push_back({diff, {k, 0}});
    } else {
      v.push_back({diff, {k + 1, 1}});
    }
  }
  sort((v).begin(), (v).end());
  reverse((v).begin(), (v).end());
  Link link_left(n);
  Link link_right(n);
  long long int res = base;
  for (long long int i = 0; i < ((long long int)(n)); i++) {
    if (v[i].second.second) {
      if (link_right.Fill(v[i].second.first, v[i].second.second)) {
        res += v[i].first;
      }
    } else {
      if (link_left.Fill(v[i].second.first, v[i].second.second)) {
        res += v[i].first;
      }
    }
  }
  cout << res << endl;
}
int main() {
  long long int t;
  cin >> t;
  for (long long int i = 0; i < ((long long int)(t)); i++) {
    solve();
  }
  return 0;
}
