#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
constexpr ll mod = 1e9 + 7;
constexpr ll inf = LLONG_MAX;
constexpr long double eps = DBL_EPSILON;
constexpr long double pi = 3.141592653589793238462643383279;
ll fact(int i) {
  if (i == 0) return 1;
  return (fact(i - 1)) * i;
}
ll gcd(ll a, ll b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int keta(ll n) {
  if (n == 0) return 1;
  int count = 0;
  while (n != 0) {
    n /= 10;
    count++;
  }
  return count;
}
ll ketasum(ll n) {
  ll sum = 0;
  while (n != 0) {
    sum += n % 10;
    n /= 10;
  }
  return sum;
}
int main() {
  int t;
  cin >> t;
  for (int z = 0; z < (int)(t); z++) {
    int n;
    cin >> n;
    vector<vector<int>> v(n, vector<int>(4));
    for (int i = 0; i < (int)(n); i++) {
      for (int j = 1; j <= (int)(3); j++) cin >> v[i][j];
      int x = v[i][2] - v[i][3];
      if (x >= 0)
        v[i][0] = (n + 1 - v[i][1]) * x;
      else
        v[i][0] = v[i][1] * x;
    }
    sort(v.begin(), v.end(),
         [](const vector<int> &alpha, const vector<int> &beta) {
           return alpha[0] < beta[0];
         });
    reverse((v).begin(), (v).end());
    for (int i = 0; i < (int)(n); i++) {
      if (i > 0)
        if (v[i][0] == v[i - 1][0]) {
          if (v[i][1] <= i + 1 && v[i - 1][i] <= i + 1)
            continue;
          else if (v[i][1] > i + 1 && v[i - 1][1] > i + 1)
            continue;
          else if (v[i][1] <= i + 1 && v[i - 1][1] > i + 1) {
            if (v[i][2] - v[i][3] < v[i - 1][2] - v[i - 1][3])
              swap(v[i], v[i - 1]);
          } else if (v[i][1] > i + 1 && v[i - 1][1] <= i + 1) {
            if (v[i][2] - v[i][3] > v[i - 1][2] - v[i - 1][3])
              swap(v[i], v[i - 1]);
          }
        }
    }
    ll ans = 0;
    for (int i = 0; i < (int)(n); i++) {
      if (i + 1 <= v[i][1])
        ans += v[i][2];
      else
        ans += v[i][3];
    }
    cout << ans << endl;
  }
  return 0;
}
