/*
  [BAPC'07] STIOCK
  by: Jan Kuipers
*/

using namespace std;

#include <iostream>
#include <queue>

int main () {

  int runs;
  cin>>runs;
  while (runs--) {
    int N;
    cin>>N;
    vector<int> get(N), price(N), sell(N);
    for (int i=0; i<N; i++)
      cin>>get[i]>>price[i]>>sell[i];

    int res=0;
    priority_queue<pair<int,int> > pq;
    for (int i=N-1; i>=0; i--) {
      pq.push(make_pair(price[i],sell[i]));
      while (pq.size() && get[i]) {
	int p = pq.top().first;
	int n = pq.top().second;
	pq.pop();
	
	int s = min(get[i], n);
	res+=s*p;
	get[i]-=s;
	n-=s;

	if (n>0) pq.push(make_pair(p,n));
      }
    }

    cout<<res<<endl;
  }

  return 0;  
}
