#include <bits/stdc++.h>
int main() {
  uint32_t T = 0;
  std::cin >> T;
  for (size_t t = 0; t < T; t++) {
    uint32_t N = 0;
    std::cin >> N;
    std::vector<std::vector<std::pair<int32_t, uint32_t>>> data(N);
    uint64_t max = 0;
    for (size_t i = 0; i < N; i++) {
      uint32_t K = 0, L = 0, R = 0;
      std::cin >> K >> L >> R;
      if (K == N || L == R)
        max += L;
      else
        data[K].push_back({static_cast<int32_t>(L) - R, R});
    }
    uint32_t left_fix = 0;
    for (size_t k = 1; k < data.size(); k++) {
      if (data[k].size() == 0)
        continue;
      else if (data[k].size() == 1) {
        if (data[k][0].first > 0) {
          left_fix++;
          max += data[k][0].second + data[k][0].first;
        } else {
          max += data[k][0].second;
        }
      } else {
        std::sort(data[k].rbegin(), data[k].rend());
        uint32_t right_fix = 0;
        for (size_t i = 0; i < data[k].size(); i++) {
          uint32_t left_cap = k - left_fix, right_cap = N - k - right_fix;
          if (left_cap != 0 && (right_cap == 0 || data[k][i].first > 0)) {
            left_fix++;
            max += data[k][i].second + data[k][i].first;
          } else {
            right_fix++;
            max += data[k][i].second;
          }
        }
      }
    }
    std::cout << max << std::endl;
  }
  return 0;
}
