#include <bits/stdc++.h>
using namespace std;
int a[5000002], b[5000002];
int yy(int n) {
  int aa = 0;
  while (n) {
    aa++;
    n /= 2;
  }
  return pow(2, aa);
}
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    set<pair<pair<int, int>, int>, greater<pair<pair<int, int>, int>>> s;
    int n;
    for (int i = 0; i < 4 * n; i++) a[i] = b[i] = 0;
    cin >> n;
    long long sum = 0;
    for (int j = 0; j < n; j++) {
      int l, k, r;
      cin >> k >> l >> r;
      sum += long long(r);
      if (l > r)
        s.insert(make_pair(make_pair(abs(l - r), k), 1));
      else
        s.insert(make_pair(make_pair(abs(l - r), k), -1));
    }
    auto it = s.begin();
    int x = yy(n);
    long long aw = 0;
    for (int j = 0; j < s.size(); j++) {
      int y = x - 1 + it->first.second, k = 2 * x - it->first.second;
      int ans = 0;
      if (it->first.second == n) {
        aw += long long(it->first.first * it->second);
        it++;
        continue;
      }
      if (it->second > 0) {
        while (y) {
          ans += a[y];
          while (!(y % 2)) {
            y = y / 2;
          }
          y--;
        }
        y = x - 1 + it->first.second;
        if (ans < it->first.second) {
          aw += long long(it->first.first);
          while (y) {
            a[y]++;
            y /= 2;
          }
        }
      } else {
        while (k) {
          ans += b[k];
          while (!(k % 2)) {
            k = k / 2;
          }
          k--;
        }
        k = 2 * x - it->first.second;
        if (ans < n - (it->first.second) + 1) {
          while (k) {
            b[k]++;
            k /= 2;
          }
        } else
          aw -= long long(it->first.first);
      }
      it++;
    }
    aw += sum;
    cout << aw << endl;
    s.clear();
  }
  return 0;
}
