#include <bits/stdc++.h>
using namespace std;
struct C {
  long long k, l;
};
vector<C> a, b;
int main() {
  int tt, T;
  scanf("%d", &T);
  for (tt = 1; tt <= T; ++tt) {
    int n;
    scanf("%d", &n);
    a.clear();
    b.clear();
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
      long long x, y, z;
      scanf("%lld%lld%lld", &x, &y, &z);
      if (x == n || y == z) {
        ans += y;
        continue;
      }
      long long t = min(y, z);
      ans += t;
      y -= t;
      z -= t;
      if (z == 0)
        a.push_back({x, y});
      else
        b.push_back({x, z});
    }
    sort(a.begin(), a.end(), [&](C &a, C &b) -> bool {
      return a.k == b.k ? a.l > b.l : a.k < b.k;
    });
    sort(b.begin(), b.end(), [&](C &a, C &b) -> bool {
      return a.k == b.k ? a.l > b.l : a.k > b.k;
    });
    int l = 0, r = 0;
    for (auto c : a) {
      if (l >= c.k)
        continue;
      else {
        ++l;
        ans += c.l;
      }
    }
    for (auto c : b) {
      if (r >= n - c.k)
        continue;
      else {
        ++r;
        ans += c.l;
      }
    }
    printf("%lld\n", ans);
  }
}
