#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;

vector<int> in[200200];
int k[200200], l[200200], r[200200];

int main()
{
	int Test; scanf ("%d", &Test); while (Test--){
		int n, x = 0; scanf ("%d", &n);
		long long base = 0;
		for (int i = 0; i < n; i++){
			scanf ("%d %d %d", &k[i], &l[i], &r[i]);
			if (l[i] >= r[i]) x++;
		}

		for (int i = 0; i < n; i++){
			if (l[i] >= r[i]){
				base += r[i];
				if (k[i] > x) k[i] = x;
				in[k[i]].push_back(l[i] - r[i]);
			}
			else{
				if (k[i] <= x){
					base += r[i];
					continue;
				}
				in[k[i] + 1].push_back(r[i] - l[i]);
				base += l[i];
			}
		}

		priority_queue<int> Q;
		for (int i = x; i >= 1; i--){
			for (auto &v : in[i]) Q.push(v);
			if (!Q.empty()) base += Q.top(), Q.pop();
			in[i].clear();
		}
		while (!Q.empty()) Q.pop();
		for (int i = x + 1; i <= n; i++){
			for (auto &v : in[i]) Q.push(v);
			if (!Q.empty()) base += Q.top(), Q.pop();
			in[i].clear();
		}
		in[n + 1].clear();

		printf ("%lld\n", base);
	}
	return 0;
}