#include <bits/stdc++.h>
using namespace std;
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<int, int>, greater<pair<int, int>>> s;
    int n;
    vector<int> a(500003), b(500003);
    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);
      s.insert(make_pair(l - r, k));
    }
    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->second, k = 2 * x - y - 1;
      int ans = 0;
      if (it->second == n) {
        aw += it->first;
        it++;
        continue;
      }
      if (it->first > 0) {
        while (y) {
          ans += a[y];
          while (!(y % 2)) {
            y = y / 2;
          }
          y--;
        }
        y = x - 1 + it->second;
        if (ans < it->second) {
          aw += it->first;
          while (y) {
            a[y]++;
            y /= 2;
          }
        }
      } else if (it->first < 0) {
        while (k) {
          ans += b[k];
          while (!(k % 2)) {
            k = k / 2;
          }
          k--;
        }
        k = 2 * x - y - 1;
        if (ans < n - (it->second) + 1) {
          while (k) {
            b[k]++;
            k /= 2;
          }
        } else
          aw += it->first;
      }
      it++;
    }
    aw += sum;
    cout << aw << endl;
    s.clear();
  }
  return 0;
}
