#include <bits/stdc++.h>
using namespace std;
long long slow(int n, vector<int> k, vector<long long> l, vector<long long> r) {
  long long res = 0;
  vector<int> p(n);
  iota((p).begin(), (p).end(), 0);
  do {
    long long cur = 0;
    for (int i = 0; i < n; ++i) {
      cur += p[i] <= k[i] ? l[i] : r[i];
    }
    res = max(res, cur);
  } while (next_permutation((p).begin(), (p).end()));
  return res;
}
long long solveA(int n, vector<int> k, vector<long long> a) {
  assert((int)(k).size() == (int)(a).size());
  for (long long x : a) {
    assert(x >= 0);
  }
  for (int x : k) {
    assert(x >= 0 && x < n);
  }
  vector<vector<long long>> by_k(n);
  for (int i = 0; i < (int)(k).size(); ++i) {
    by_k[k[i]].push_back(a[i]);
  }
  multiset<long long> cur;
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    for (long long x : by_k[i]) {
      sum += x;
      cur.insert(x);
    }
    while ((int)(cur).size() > i + 1) {
      long long x = *cur.begin();
      sum -= x;
      cur.erase(cur.begin());
    }
  }
  return sum;
}
long long solve(int n, vector<int> k, vector<long long> l,
                vector<long long> r) {
  long long res = 0;
  int cnt = 0;
  vector<int> k_l, k_r;
  vector<long long> a_l, a_r;
  for (int i = 0; i < n; ++i) {
    if (l[i] >= r[i]) {
      ++cnt;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (l[i] >= r[i]) {
      res += r[i];
      k_l.push_back(min(cnt - 1, k[i]));
      a_l.push_back(l[i] - r[i]);
    } else {
      res += l[i];
      if (k[i] + 1 == n) {
        continue;
      }
      k_r.push_back(n - max(cnt, k[i] + 1) - 1);
      a_r.push_back(r[i] - l[i]);
    }
  }
  res += solveA(cnt, k_l, a_l);
  res += solveA(n - cnt, k_r, a_r);
  return res;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    vector<int> k(n);
    vector<long long> l(n), r(n);
    for (int i = 0; i < n; ++i) {
      scanf("%d %I64d %I64d", &k[i], &l[i], &r[i]);
      --k[i];
    }
    long long res = solve(n, k, l, r);
    printf("%I64d\n", res);
  }
  return 0;
}
