#include <bits/stdc++.h>
using namespace std;

const int N = 200005;

int n;
int k[N], l[N], r[N];

int lft[N];
int getLft(int u) {
  if (u == -1 || lft[u] == u) return u;
  return lft[u] = getLft(lft[u]);
}

void add(int u) {
  lft[getLft(u)] = getLft(getLft(u) - 1);
}

long long count(vector<pair<int, int>> camels, vector<int> k) {
  sort(camels.begin(), camels.end(), greater<pair<int, int>>());
  for (int i = 0; i < n; ++i) lft[i] = i;

  long long tot = 0;
  for (int i = 0; i < camels.size(); ++i) {
    int idx = camels[i].second;
    if (getLft(k[idx]) == -1) {
      add(n-1);
    } else {
      add(k[idx]);
      tot += camels[i].first;
    }
  }

  return tot;
}

int solve() {
  scanf("%d", &n);

  vector<pair<int, int>> pos, neg;
  vector<int> k1, k2;

  long long tot = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d %d %d", &k[i], &l[i], &r[i]);
    --k[i];
    k1.push_back(k[i]);
    k2.push_back(n - k[i] - 2);

    if (l[i] >= r[i]) {
      pos.emplace_back(l[i] - r[i], i);
      tot += r[i];
    } else {
      neg.emplace_back(r[i] - l[i], i);
      tot += l[i];
    }
  }

  tot += count(pos, k1) + count(neg, k2);

  printf("%lld\n", tot);
  return 0;
}

int main() {
  int t = 1;
  scanf("%d", &t);
  while (t--) solve();
  return 0;
}
