#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long int MOD = 1e9 + 7;
const int N = 2e5 + 5;
int ara[N], rec1[N], rec2[N];
int get(int x) {
  int ret = 0;
  while (x) {
    int bit = __builtin_popcount(x);
    x %= bit;
    ret++;
  }
  return ret;
}
int k[N], l[N], r[N];
vector<int> lt[N], rt[N];
bool comp(int x, int y) {
  if (l[x] > r[x]) return l[x] > l[y];
  return r[x] > r[y];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) lt[i].clear(), rt[i].clear();
    for (int i = 1; i <= n; i++) {
      cin >> k[i] >> l[i] >> r[i];
      if (k[i] == n) r[i] = 0;
      if (l[i] > r[i])
        lt[k[i]].push_back(i);
      else
        rt[n - k[i]].push_back(i);
    }
    int baki1 = 0, baki2 = 0;
    long long int ans = 0;
    for (int i = 1; i <= n; i++) {
      baki1++;
      baki2++;
      sort(lt[i].begin(), lt[i].end(), comp);
      sort(rt[i].begin(), rt[i].end(), comp);
      for (auto x : lt[i]) {
        if (baki1)
          ans += l[x];
        else
          ans += r[x];
        if (baki1) baki1--;
      }
      for (auto x : rt[i]) {
        if (baki2)
          ans += r[x];
        else
          ans += l[x];
        if (baki2) baki2--;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
