Language: C++

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
//#pragma warning(disable : 4996)

#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#endif

#include <limits.h>
#include <math.h>
#include <time.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
//#define print(x) cout << (x) << '\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
#define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) REP(i, v.size()) cout << v[i] << ((i == v.size() - 1) ? "\n" : " ")
template <typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template <typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 500050;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll LINF = 1e18 + 1;
constexpr int INF = 1e9 + 1;

template<class VAL> struct RBST {
	VAL SUM_UNITY = 0;                              // to be set

	unsigned int randInt() {
		static unsigned int tx = 123456789, ty = 362436069, tz = 521288629, tw = 88675123;
		unsigned int tt = (tx ^ (tx << 11));
		tx = ty; ty = tz; tz = tw;
		return (tw = (tw ^ (tw >> 19)) ^ (tt ^ (tt >> 8)));
	}

	struct NODE {
		NODE *left, *right;
		VAL val;                        // the value of the node
		int size;                       // the size of the subtree 
		VAL sum;                        // the value-sum of the subtree

		NODE() : val(SUM_UNITY), size(1), sum(SUM_UNITY) {
			left = right = NULL;
		}

		NODE(VAL v) : val(v), size(1), sum(v) {
			left = right = NULL;
		}

		/* additional update */
		inline void update() {

		}

		/* additional lazy-propagation */
		inline void push() {

			/* ex: reverse */
			/*
			if (this->rev) {
			swap(this->left, this->right);
			if (this->left) this->left->rev ^= true;
			if (this->right) this->right->rev ^= true;
			this->rev = false;
			}
			*/
		}
	};


	///////////////////////
	// root
	///////////////////////

	NODE* root;
	RBST() : root(NULL) { }
	RBST(NODE* node) : root(node) { }


	///////////////////////
	// basic operations
	///////////////////////

	/* size */
	inline int size(NODE *node) {
		return !node ? 0 : node->size;
	}
	inline int size() {
		return this->size(this->root);
	}

	/* sum */
	inline VAL sum(NODE *node) {
		return !node ? SUM_UNITY : node->sum;
	}
	inline VAL sum() {
		return this->sum(this->root);
	}

	/* update, push */
	inline NODE* update(NODE *node) {
		node->size = size(node->left) + size(node->right) + 1;
		node->sum = sum(node->left) + sum(node->right) + node->val;
		node->update();
		return node;
	}

	inline void push(NODE *node) {
		if (!node) return;
		node->push();
	}

	/* lower_bound */
	inline int lowerBound(NODE *node, VAL val) {
		push(node);
		if (!node) return 0;
		if (val <= node->val) return lowerBound(node->left, val);
		else return size(node->left) + lowerBound(node->right, val) + 1;
	}
	inline int lowerBound(VAL val) {
		return this->lowerBound(this->root, val);
	}

	/* upper_bound */
	inline int upperBound(NODE *node, VAL val) {
		push(node);
		if (!node) return 0;
		if (val >= node->val) return size(node->left) + upperBound(node->right, val) + 1;
		else return upperBound(node->left, val);
	}
	inline int upperBound(VAL val) {
		return this->upperBound(this->root, val);
	}

	/* count */
	inline int count(VAL val) {
		return upperBound(val) - lowerBound(val);
	}

	/* get --- k: 0-index */
	inline VAL get(NODE *node, int k) {
		push(node);
		if (!node) return -1;
		if (k == size(node->left)) return node->val;
		if (k < size(node->left)) return get(node->left, k);
		else return get(node->right, k - size(node->left) - 1);
	}
	inline VAL get(int k) {
		return get(this->root, k);
	}


	///////////////////////
	// merge-split
	///////////////////////

	NODE* merge(NODE *left, NODE *right) {
		push(left);
		push(right);
		if (!left || !right) {
			if (left) return left;
			else return right;
		}
		if (randInt() % (left->size + right->size) < left->size) {
			left->right = merge(left->right, right);
			return update(left);
		}
		else {
			right->left = merge(left, right->left);
			return update(right);
		}
	}
	void merge(RBST add) {
		this->root = this->merge(this->root, add.root);
	}
	pair<NODE*, NODE*> split(NODE* node, int k) { // [0, k), [k, n)
		push(node);
		if (!node) return make_pair(node, node);
		if (k <= size(node->left)) {
			pair<NODE*, NODE*> sub = split(node->left, k);
			node->left = sub.second;
			return make_pair(sub.first, update(node));
		}
		else {
			pair<NODE*, NODE*> sub = split(node->right, k - size(node->left) - 1);
			node->right = sub.first;
			return make_pair(update(node), sub.second);
		}
	}
	RBST split(int k) {
		pair<NODE*, NODE*> sub = split(this->root, k);
		this->root = sub.first;
		return RBST(sub.second);
	}


	///////////////////////
	// insert-erase
	///////////////////////

	void insert(const VAL val) {
		pair<NODE*, NODE*> sub = this->split(this->root, this->lowerBound(val));
		this->root = this->merge(this->merge(sub.first, new NODE(val)), sub.second);
	}

	void erase(const VAL val) {
		if (!this->count(val)) return;
		pair<NODE*, NODE*> sub = this->split(this->root, this->lowerBound(val));
		this->root = this->merge(sub.first, this->split(sub.second, 1).second);
	}


	///////////////////////
	// debug
	///////////////////////

	void print(NODE *node) {
		if (!node) return;
		push(node);
		print(node->left);
		cout << node->val << " ";
		print(node->right);
	}
	void print() {
		cout << "{";
		print(this->root);
		cout << "}" << endl;
	}
};
vector<int>ids[200020];
void solve() {
	int N;
	cin >> N;
	//auto f = [](int a, int b) {return a + b; };
	//SegmentTree<int>seg(N+1,0,f);
	REP(i, N + 1)ids[i].clear();
	vector<int>K(N), L(N), R(N);
	REP(i, N) {
		cin >> K[i] >> L[i] >> R[i];
		ids[K[i]].push_back(i);
		//K[i]--;
	}
	using arr3 = array<int, 3>;
	vector<arr3>v(N);
	REP(i, N) {
		v[i] = {K[i],R[i]-L[i],i};
	}
	VSORT(v);
	int cnt = 0;
	priority_queue<pii>que;//左側に配置したものを入れる
	int ans = 0;
	int res = 0;
	RBST<int>st;//右側に配置したものを入れる
	priority_queue<pii>wait;//これから追加するもの
	REPR(i, N + 1, 1) {
		for (auto j : ids[i]) {
			wait.push({ L[j] - R[j],j });
		}
		if (wait.empty())continue;
		else {
			pii p = wait.top(); wait.pop();
			cnt++;
			int j = p.second;
			res += L[j];
			que.push({ R[j] - L[j],j });
		}
	}
	while (!wait.empty()) {
		pii p = wait.top(); wait.pop();
		int j = p.second;
		res += R[j];
		st.insert(K[j]);
	}

	//FOR(i, 0, N) {
	//	if (cnt < v[i][0]) {
	//		cnt++;
	//		res += L[v[i][2]];
	//		que.push({ R[i] - L[i],v[i][2]});
	//	}
	//	else {
	//		res += R[v[i][2]];
	//		st.insert(v[i][0]);
	//	}
	//}
	//DEBUG(cnt);
	ans = res;
	deque<int>deq;
	FORR(i, N + 1, cnt + 1)deq.push_front(i);

	while (cnt) {
		//DEBUG(cnt);
		//st.print();
		if (que.empty())break;
		deq.push_front(cnt);
		while (!que.empty()) {
			pii p = que.top(); que.pop();
			int i = p.second;
			//cerr << K[i] << " " << L[i] << " " << R[i] << endl;
			st.insert(K[i]);
			int l = st.lowerBound(K[i]);
			if (deq[l] <= K[i]) {
				//st.print();
				//cerr << "K[i]:" << K[i] << endl;
				st.erase(K[i]);
				//st.print();
			}
			else {
				res += R[i];
				res -= L[i];
				break;
			}
		}
		chmax(ans, res);
		cnt--;
	}
	cout << ans << '\n';
}



signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int q;
	cin >> q;
	while (q--)
	solve();
}


Compiler output:
a.cc:300:25: error: macro "REPR" passed 3 arguments, but takes just 2
  300 |         REPR(i, N + 1, 1) {
      |                         ^
a.cc:41:9: note: macro "REPR" defined here
   41 | #define REPR(i, n) for (int i = n - 1; i >= 0; --i)
      |         ^~~~
a.cc: In function 'void solve()':
a.cc:300:9: error: 'REPR' was not declared in this scope
  300 |         REPR(i, N + 1, 1) {
      |         ^~~~