#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9 + 7;
int t, n, k, a, b;
vector<int> vec[2][200009];
long long ans;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n;
    ans = 0;
    for (int i = 1; i <= n; i++) {
      vec[0][i].clear();
      vec[1][i].clear();
    }
    for (int i = 1; i <= n; i++) {
      cin >> k >> a >> b;
      if (a < b) {
        vec[1][k + 1].push_back(b - a);
      } else if (a > b) {
        vec[0][k].push_back(a - b);
      }
      ans += min(a, b);
    }
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      cnt++;
      sort(vec[0][i].begin(), vec[0][i].end());
      reverse(vec[0][i].begin(), vec[0][i].end());
      int sz = vec[0][i].size();
      for (int j = 0; j < sz; j++) {
        if (cnt == 0) break;
        ans += vec[0][i][j];
        cnt--;
      }
    }
    cnt = 0;
    for (int i = n; i >= 1; i--) {
      cnt++;
      sort(vec[1][i].begin(), vec[1][i].end());
      reverse(vec[1][i].begin(), vec[1][i].end());
      int sz = vec[1][i].size();
      for (int j = 0; j < sz; j++) {
        if (cnt == 0) break;
        ans += vec[1][i][j];
        cnt--;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
