#include <bits/stdc++.h>
const long long INF = 1LL << 60;
using namespace std;
template <class T = int>
T in() {
  T x;
  cin >> x;
  return (x);
}
template <class T>
void print(T& x) {
  cout << x << '\n';
}
const int MOD = (int)1e9 + 7;
const int mod = (int)1e9 + 7;
const int MAX = 510000;
long long fac[MAX], finv[MAX], inv[MAX];
void COMint() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
long long COM(int n, int k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
long long gcd(long long a, long long b) {
  if (a < 0) a = -a;
  if (b < 0) b = -b;
  if (b == 0) return a;
  if (a > b) {
    swap(a, b);
  }
  return gcd(a, b % a);
}
long long lcm(long long a, long long b) {
  if (a < 0) a = -a;
  if (b < 0) b = -b;
  long long g;
  g = gcd(a, b);
  return b / g * a;
}
bool compare_by_b(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}
bool compare_by_a(pair<int, int> a, pair<int, int> b) {
  if (a.first != b.first) {
    return a.first < b.first;
  } else {
    return a.second < b.second;
  }
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
long long RS(long long N, long long pair<int, int>, long long m) {
  if (pair<int, int> == 0) {
    return 1;
  } else {
    if (pair<int, int> % 2 == 0) {
      long long t = RS(N, pair<int, int> / 2, m);
      return t * t % m;
    } else {
      return N * RS(N, pair<int, int> - 1, m) % m;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  int T = in();
  for (int i = 0; i < (T); ++i) {
    long long ans = 0;
    long long N;
    cin >> N;
    vector<pair<int, int>> camel_p;
    vector<pair<int, int>> camel_n;
    for (int i = 0; i < (N); ++i) {
      int k, l, r;
      cin >> k >> l >> r;
      ans += min(l, r);
      if (l >= r) {
        camel_p.emplace_back(k, l - r);
      } else {
        camel_n.emplace_back(N - k, r - l);
      }
    }
    sort(camel_p.begin(), camel_p.end());
    sort(camel_n.begin(), camel_n.end());
    int p_n = camel_p.size();
    int n_n = camel_n.size();
    int now = camel_n[i].first;
    priority_queue<int> q_n;
    for (int i = 0; i < (n_n); ++i) {
      if (camel_n[i].first != now) {
        while (now < q_n.size()) {
          ans -= q_n.top();
          q_n.pop();
        }
        now = camel_n[i].first;
      }
      q_n.push(camel_n[i].second);
      ans += camel_n[i].second;
    }
    while (now < q_n.size()) {
      ans -= q_n.top();
      q_n.pop();
    }
    priority_queue<int> q;
    now = camel_p[i].first;
    for (int i = 0; i < (p_n); ++i) {
      if (camel_p[i].first != now) {
        while (now < q.size()) {
          ans -= q.top();
          q.pop();
        }
        now = camel_p[i].first;
      }
      ans += camel_p[i].second;
      q.push(camel_p[i].second);
    }
    while (now < q.size()) {
      ans -= q.top();
      q.pop();
    }
    cout << ans << endl;
  }
  return 0;
}
