#include <bits/stdc++.h>
using namespace std;
int t, storek[200010], storel[200010], storer[200010];
vector<long long> res;
bool used[200010];
vector<pair<int, int> > v;
signed main() {
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      int id = abs(b - c);
      if (a == n) {
        ans += b;
        continue;
      }
      if (id == 0) {
        ans += b;
        continue;
      }
      v.push_back(make_pair(id, i));
      storek[i] = a, storel[i] = b;
      storer[i] = c;
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    n = v.size();
    int recfront = 0, recback = n + 1;
    for (int i = 0; i < n; i++) {
      int now = v[i].second;
      int pos = storek[now], l = storel[now], r = storer[now];
      if (l - r < 0) {
        if (!used[pos + 1]) {
          used[pos + 1] = 1;
          ans += r;
          continue;
        }
        if (recback > pos) {
          used[recback - 1] = 1;
          recback--;
          ans += r;
          continue;
        }
        ans += l;
      }
      if (l - r > 0) {
        if (!used[pos]) {
          used[pos] = 1;
          ans += l;
          continue;
        }
        if (recfront < pos) {
          used[recfront + 1] = 1;
          recfront++;
          ans += l;
          continue;
        }
        ans += r;
      }
    }
    for (int i = 1; i <= n + 1; i++) used[i] = 0;
    v.clear();
    res.push_back(ans);
  }
  for (int i = 0; i < res.size(); i++) cout << res[i] << endl;
  return 0;
}
