#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    long long happiness = 0;
    vector<vector<int>> st(n + 1);
    vector<vector<int>> ed(n + 1);
    for (int i = 0; i < n; i++) {
      int x, y, z;
      scanf("%d%d%d", &x, &y, &z);
      if (x == n) {
        happiness += y;
        continue;
      }
      if (y > z) {
        happiness += z;
        y -= z;
        st[x].push_back(y);
      } else if (z > y) {
        happiness += y;
        z -= y;
        ed[n - x + 1].push_back(z);
      } else {
        happiness += y;
      }
    }
    int st_count = 0;
    for (int i = 1; i <= n; i++) {
      sort(st[i].begin(), st[i].end(), greater<int>());
      int sz = st[i].size();
      int k = 0;
      while (k < sz && i - st_count > 0) {
        happiness += st[i][k];
        k++;
        st_count++;
      }
    }
    int ed_count = 0;
    for (int i = 1; i <= n; i++) {
      sort(ed[i].begin(), ed[i].end(), greater<int>());
      int sz = ed[i].size();
      int k = 0;
      while (k < sz && i - ed_count > 0) {
        happiness += ed[i][k];
        k++;
        ed_count++;
      }
    }
    printf("%lld\n", happiness);
  }
}
