#include <bits/stdc++.h>
using namespace std;
using Edge = pair<int, int>;
using Graph = vector<vector<int>>;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int main() {
  long long T;
  cin >> T;
  for (int i = (0); i < (T); ++i) {
    long long N, ans = 0;
    cin >> N;
    long long K[N], L[N], R[N], ANS[N];
    for (int j = (0); j < (N); ++j) {
      cin >> K[j] >> L[j] >> R[j];
      ans += min(L[j], R[j]);
      L[j] -= min(L[j], R[j]);
      R[j] -= min(L[j], R[j]);
      K[j]--;
      ANS[j] = 0;
    }
    vector<pair<long long, long long>> LI, RI;
    for (int j = (0); j < (N); ++j) {
      if (L[j] > 0) LI.push_back(make_pair(K[j], L[j]));
      if (R[j] > 0) RI.push_back(make_pair(K[j], R[j]));
    }
    sort((LI).begin(), (LI).end());
    sort((RI).begin(), (RI).end());
    reverse((RI).begin(), (RI).end());
    for (int j = (0); j < (LI.size()); ++j) {
      chmax(ANS[LI[j].first], LI[j].second);
    }
    for (int j = (0); j < (RI.size()); ++j) {
      chmax(ANS[RI[j].first], RI[j].second);
    }
    for (int j = (0); j < (N); ++j) ans += ANS[j];
    cout << ans << '\n';
  }
  return 0;
}
