#include <bits/stdc++.h>
using namespace std;
const int max_n = 200222, inf = 1000111222;
struct tree {
  int t[max_n], last[max_n], curt;
  tree() { memset(t, 0, sizeof(t)); }
  void clr() { ++curt; }
  void clri(int i) {
    if (last[i] < curt) {
      last[i] = curt;
      t[i] = 0;
    }
  }
  void update(int i) {
    for (; i < max_n; i = (i | (i + 1))) {
      clri(i);
      ++t[i];
    }
  }
  int get_sum(int r) {
    int res = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1) {
      clri(r);
      res += t[r];
    }
    return res;
  }
};
int t, n;
tree tr;
long long solve(vector<pair<int, int>> &v) {
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  long long res = 0;
  tr.clr();
  for (auto p : v) {
    if (!p.second) {
      continue;
    }
    if (tr.get_sum(p.second - 1) < p.second) {
      res += p.first;
    } else {
      tr.update(p.second - 1);
    }
  }
  return res;
}
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    long long ans = 0;
    vector<pair<int, int>> a, b;
    for (int i = 0; i < n; ++i) {
      int k, l, r;
      scanf("%d%d%d", &k, &l, &r);
      ans += min(l, r);
      if (l > r) {
        a.push_back({l - r, k});
      } else {
        b.push_back({r - l, n - k});
      }
    }
    ans += solve(a);
    ans += solve(b);
    printf("%lld\n", ans);
  }
  return 0;
}
