/*
 * TODOEJ: write actual solution.
 *
 * First idea:
 * Sort the days according to price. Now for each day, you have to sell the maximum amount possible and adjust the amount left for the previous days.
 * Naive way of doing this is starting with the current day, while you have space left to sell, adjust the space left to sell and go to the previous day.
 * This leads to an O(n^2) solution.
 * This can be improved, using an UnionFind type of structure, where find(x) returns the first day before x where there is stock left.
 * Complexity: O(n*alpha(n))
 */

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;

#define MP make_pair
#define SZ(v) ((int)(v).size())
#define PB push_back
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define REPSZ(i,v) REP(i,SZ(v))

typedef pair<int,int> PII;
typedef vector<PII> VPII;

int n;
int get[100000],price[100000],sell[100000];

PII val[100000]; int rank[100000];
PII find(int a) { if(val[a].first==a) return val[a]; return val[a]=find(val[a].first); }
void unite(int a,int b) {
	a=find(a).first;
	if(b==-1) { val[a].second=-1; return; }
	b=find(b).first;
	if(a==b) return;
	if(rank[a]<rank[b]) {
		val[a].first=b;
		val[b].second=min(val[b].second,val[a].second);
	} else {
		val[b].first=a; 
		val[a].second=min(val[a].second,val[b].second);
	}
	if(rank[a]==rank[b]) ++rank[a];
}


void run() {
	scanf("%d",&n);
	REP(i,n) scanf("%d%d%d",&get[i],&price[i],&sell[i]);

	REP(i,n) val[i].first=i,val[i].second=i,rank[i]=0;

	VPII order; REP(i,n) order.PB(MP(price[i],i)); sort(order.rbegin(),order.rend());
	
	int ret=0;
	REPSZ(i,order) {
		int cur=order[i].second;
		while(sell[cur]>0) {
			int prev=find(cur).second;
//			printf("cur=%d, prev=%d\n",cur,prev);
			if(prev==-1) break;
			int now=min(get[prev],sell[cur]);
//			printf("get=%d, sell=%d, now=%d, price=%d\n",get[prev],sell[cur],now,price[cur]);
//			assert(now>0);
			ret+=price[cur]*now;
			get[prev]-=now; sell[cur]-=now;
			if(get[prev]==0) unite(prev,prev-1);
		}
	}
	printf("%d\n",ret);
}

int main() {
	int n; scanf("%d",&n); REP(i,n) run();
	
	return 0;
}
