# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int t, n, m, x, y, z, dis[50005];
vector <pii> v[50005];
bitset <50005> vis;
queue <int> q;
int main () {
ios::sync_with_stdio (0);
cin.tie (0);
cout.tie (0);
cin >> t;
while (t --) {
cin >> n;
for (int i = 1; i < 50003; ++ i)
v[i].clear ();
m = 0;
while (n --) {
cin >> x >> y >> z;
v[x].push_back ({++ y, z});
m = max (m, y);
}
++ m;
v[0].push_back ({1, 0});
for (int i = 1; i < m; ++ i)
v[i].push_back ({i + 1, 0}), v[i + 1].push_back ({i, -1});
fill (dis + 1, dis + 1 + m, -1e18), vis.reset ();
q.push (0);
while (! q.empty ()) {
x = q.front ();
q.pop ();
vis[x] = 0;
for (pii& i : v[x])
if (i.second + dis[x] > dis[i.first]) {
dis[i.first] = dis[x] + i.second;
if (! vis[i.first])
q.push (i.first), vis[i.first] = 1;
}
}
cout << dis[m];
if (t)
cout << '\n';
}
return 0;
}