#include <bits/stdc++.h>
using namespace std;
long long t, n;
vector<pair<long long, long long> > order;
long long bit[500005];
void update(long long x, long long val) {
  long long idx = x;
  while (idx <= n) {
    bit[idx] = max(bit[idx], val);
    idx += idx & (-idx);
  }
}
long long query(long long x) {
  long long idx = x, ans = 0;
  while (idx > 0) {
    ans = max(bit[idx], ans);
    idx -= (idx) & (-idx);
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> t;
  while (t--) {
    cin >> n;
    long long ans = 0;
    for (long long i = 1; i <= n; ++i) bit[i] = 0;
    order.clear();
    for (long long i = 1; i <= n; ++i) {
      long long k, v1, v2;
      cin >> k >> v1 >> v2;
      if (k == n) {
        ans += v1;
        continue;
      }
      ans += min(v1, v2);
      if (v1 > v2)
        order.push_back(make_pair(abs(v1 - v2), k));
      else if (v1 < v2)
        order.push_back(make_pair(abs(v2 - v1), (k - n)));
    }
    sort(order.begin(), order.end());
    reverse(order.begin(), order.end());
    if (!order.empty())
      for (auto x : order) {
        if (x.second > 0) {
          long long val = query(x.second);
          if (val < x.second) {
            ans += x.first;
            update(x.second, 1);
          }
        } else {
          long long val = query(n) - query(1 - x.second);
          if (val < -x.second) {
            ans += x.first;
            update(n, 1);
          }
        }
      }
    cout << ans << "\n";
  }
}
