#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e5;
const long long MOD = 1e9 + 7;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int main() {
  fast();
  int t;
  cin >> t;
  while (t--) {
    int n, k, l, r;
    cin >> n;
    int ans = 0;
    vector<pair<int, pair<int, int>>> goodl, goodr;
    for (int i = 0; i < n; ++i) {
      cin >> k >> l >> r;
      if (k == n)
        ans += l;
      else if (l > r) {
        goodl.push_back({l - r, {l, k}});
      } else if (r > l) {
        goodr.push_back({r - l, {r, k}});
      } else {
        ans += l;
      }
    }
    vector<int> myleft(goodl.size() + 2);
    vector<int> myright(goodl.size() + 2);
    vector<bool> taken(goodl.size() + 1, false);
    for (int i = 0; i < myleft.size(); ++i) {
      myleft[i] = i - 1;
      myright[i] = i + 1;
    }
    for (auto x : goodl) {
      int idx = x.second.second;
      if (taken[idx]) idx = myleft[idx];
      if (idx <= 0) {
        ans += x.second.first - x.first;
      } else {
        taken[idx] = true;
        int left = myleft[idx], right = myright[idx];
        myleft[right] = left;
        myright[left] = right;
        ans += x.second.first;
      }
    }
    myleft = vector<int>(n + 2);
    myright = vector<int>(n + 2);
    taken = vector<bool>(n + 2, false);
    for (int i = 0; i < myleft.size(); ++i) {
      myleft[i] = i - 1;
      myright[i] = i + 1;
    }
    for (auto x : goodr) {
      int idx = x.second.second;
      if (taken[idx]) idx = myright[idx];
      if (idx > n) {
        ans += x.second.first - x.first;
      } else {
        taken[idx] = true;
        int left = myleft[idx], right = myright[idx];
        myleft[right] = left;
        myright[left] = right;
        ans += x.second.first;
      }
    }
    cout << ans << "\n";
  }
}
