#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
void update(int x, int delta, vector<int>& BIT) {
  for (; x <= n; x += x & -x) BIT[x] += delta;
}
int query(int x, vector<int>& BIT) {
  int sum = 0;
  for (; x > 0; x -= x & -x) sum += BIT[x];
  return sum;
}
int back_query(int x, vector<int>& BIT) {
  return (query(n, BIT) - query(n - x, BIT));
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int tests = 1;
  cin >> tests;
  for (int test = 1; test <= tests; ++test) {
    cin >> n;
    vector<int> da[n];
    vector<pair<int, int> > up, down;
    vector<int> filled(n + 1, -1);
    vector<int> BIT(n + 1, 0);
    for (int i = 1; i <= n; ++i) update(i, 1, BIT);
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      int k, l, r;
      cin >> k >> l >> r;
      da[i].push_back(k);
      da[i].push_back(l);
      da[i].push_back(r);
      if (l == r || k == n) {
        ans += l;
      } else if (l > r) {
        up.push_back({l - r, i});
      } else {
        down.push_back({r - l, i});
      }
    }
    sort(up.begin(), up.end());
    reverse(up.begin(), up.end());
    sort(down.begin(), down.end());
    reverse(down.begin(), down.end());
    for (int i = 0; i < int(up.size()); ++i) {
      if (query(da[up[i].second][0], BIT) > 0) {
        int st, low = 1, high = da[up[i].second][0];
        while (low <= high) {
          int mid = (low + high) / 2;
          if (query(mid, BIT) > 0) {
            st = mid;
            low = mid + 1;
          } else
            high = mid - 1;
        }
        filled[st] = up[i].second;
        update(st, 1, BIT);
        ans += da[up[i].second][1];
      } else {
        ans += da[up[i].second][2];
      }
    }
    vector<int> pos;
    vector<pair<int, int> > cam;
    for (int i = 1; i <= n; ++i) {
      if (filled[i] == -1)
        pos.push_back(i);
      else
        cam.push_back({i, filled[i]});
    }
    reverse(cam.begin(), cam.end());
    int cpos = 0;
    if (!cam.empty()) {
      for (int i = 0; i < int(pos.size()); ++i) {
        if (pos[i] < cam[cpos].first) {
          filled[pos[i]] = cam[cpos].second;
          filled[cam[cpos].first] = -1;
          update(cam[cpos].first, -1, BIT);
          update(pos[i], 1, BIT);
          ++cpos;
        } else
          break;
        if (cpos == int(cam.size())) break;
      }
    }
    for (int i = 0; i < int(down.size()); ++i) {
      if (back_query(n - da[down[i].second][0], BIT) > 0) {
        int st, low = n, high = n - da[down[i].second][0];
        while (low >= high) {
          int mid = (low + high) / 2;
          if (back_query(mid, BIT) > 0) {
            st = mid;
            high = mid + 1;
          } else
            low = mid - 1;
        }
        filled[st] = down[i].second;
        update(st, 1, BIT);
        ans += da[down[i].second][2];
      } else {
        ans += da[down[i].second][1];
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
