#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1e18;
int main() {
  long long t;
  cin >> t;
  for (long long test = 0; test < (t); ++test) {
    long long n;
    cin >> n;
    priority_queue<
        pair<pair<long long, long long>, pair<long long, long long> > >
        p;
    for (long long i = 0; i < (n); ++i) {
      long long k, l, r;
      cin >> k >> l >> r;
      long long a = abs(l - r);
      long long fugo;
      if (l - r >= 0)
        fugo = 1;
      else
        fugo = 0;
      p.push({{a, l}, {fugo, k}});
    }
    vector<pair<long long, long long> > aki(n + 2);
    for (long long i = 0; i < (n + 2); ++i) {
      aki[i] = {i, i};
    }
    long long ans = 0;
    while (!p.empty()) {
      auto now = p.top();
      p.pop();
      int fugo = now.second.first;
      long long k = now.second.second;
      long long a = now.first.first;
      long long l = now.first.second;
      if (fugo == 0) {
        if (aki[k + 1].second != n + 1) {
          aki[k + 1].second = aki[k + 2].second;
          ans += l + a;
        } else {
          ans += l;
          aki[k].first = aki[k - 1].first;
        }
      } else {
        if (aki[k].first != 0) {
          aki[k].first = aki[k - 1].first;
          ans += l;
        } else {
          aki[k + 1].second = aki[k + 2].second;
          ans += l + a;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
