#include <bits/stdc++.h>
using namespace std;
vector<int> F, L;
priority_queue<pair<pair<int, int>, int> > f, l;
pair<pair<int, int>, int> arr[200005];
int main() {
  int t, n, i, j, T = 1;
  scanf("%d", &T);
  pair<pair<int, int>, int> p;
  for (t = 1; t <= T; t++) {
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
      scanf("%d %d %d", &arr[i].second, &arr[i].first.first,
            &arr[i].first.second);
      if (arr[i].first.first > arr[i].first.second)
        f.push(make_pair(
            make_pair(-arr[i].second, arr[i].first.first - arr[i].first.second),
            i));
      else if (arr[i].first.first < arr[i].first.second)
        l.push(make_pair(make_pair(arr[i].second,
                                   -(arr[i].first.first - arr[i].first.second)),
                         i));
      arr[i].second--;
    }
    while (!l.empty()) {
      p = l.top();
      l.pop();
      if (n - p.first.first <= L.size()) {
        F.push_back(p.second);
      } else {
        L.push_back(p.second);
      }
    }
    while (F.size() > 0) {
      L.push_back(F[F.size() - 1]);
      F.pop_back();
    }
    while (!f.empty()) {
      p = f.top();
      f.pop();
      if (-p.first.first <= F.size()) {
        L.push_back(p.second);
      } else {
        F.push_back(p.second);
      }
    }
    for (i = 0; i < n; i++) {
      if (arr[i].first.first == arr[i].first.second) L.push_back(i);
    }
    while (L.size() > 0) {
      F.push_back(L[L.size() - 1]);
      L.pop_back();
    }
    long long ans = 0;
    for (i = F.size() - 1; i >= 0; i--) {
      if (i <= arr[F[i]].second)
        ans = ans + arr[F[i]].first.first;
      else
        ans = ans + arr[F[i]].first.second;
      F.pop_back();
    }
    printf("%lld\n", ans);
  }
  return 0;
}
