#include <bits/stdc++.h>
using namespace std;
long long int linsert(vector<long long int>& list, long long int idx) {
  if (idx < 0 || idx >= (long long int)list.size()) return -1;
  if (idx == list[idx]) {
    if (idx == 0)
      list[idx] = -1;
    else
      list[idx] = list[idx - 1];
    return idx;
  } else if (list[idx] == -1) {
    return -1;
  } else {
    long long int newidx = linsert(list, list[idx]);
    if (newidx == -1)
      list[idx] = -1;
    else
      list[idx] = list[newidx];
    return newidx;
  }
}
long long int rinsert(vector<long long int>& list, long long int idx) {
  if (idx < 0 || idx >= (long long int)list.size()) return -1;
  if (idx == list[idx]) {
    if (idx == (long long int)list.size() - 1)
      list[idx] = -1;
    else
      list[idx] = list[idx + 1];
    return idx;
  } else if (list[idx] == -1) {
    return -1;
  } else {
    long long int newidx = rinsert(list, list[idx]);
    if (newidx == -1)
      list[idx] = -1;
    else
      list[idx] = list[newidx];
    return newidx;
  }
}
int main() {
  long long int T, N, res, k, l, r;
  cin >> T;
  vector<long long int> ans(T, 0);
  for (long long int t = 0; t < T; t++) {
    vector<tuple<long long int, long long int, long long int, long long int>>
        lcamels, rcamels;
    cin >> N;
    vector<long long int> cmlist(N);
    for (long long int i = 0; i < N; i++) cmlist[i] = i;
    for (long long int n = 0; n < N; n++) {
      cin >> k >> l >> r;
      if (l >= r)
        lcamels.emplace_back(r - l, k - 1, l, r);
      else
        rcamels.emplace_back(l - r, k - 1, l, r);
    }
    sort(lcamels.begin(), lcamels.end());
    sort(rcamels.begin(), rcamels.end());
    for (auto c : lcamels) {
      res = linsert(cmlist, get<1>(c));
      if (res == -1)
        ans[t] += get<3>(c);
      else
        ans[t] += get<2>(c);
    }
    for (auto c : rcamels) {
      res = rinsert(cmlist, get<1>(c) + 1);
      if (res == -1)
        ans[t] += get<2>(c);
      else
        ans[t] += get<3>(c);
    }
  }
  for (long long int t = 0; t < T; t++) cout << ans[t] << endl;
  return 0;
}
