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

import java.util.*;

public class Jan {
  
  public class Offer implements Comparable {
    public int price, num, id;
    
    public Offer (int _price, int _num, int _id) { price=_price; num=_num; id=_id; }
    
    public int compareTo (Object _x) {
      Offer x = (Offer) _x;
      if (price < x.price) return 1;
      if (price > x.price) return -1;
      if (id < x.id) return -1;
      if (id > x.id) return 1;
      return 0;      
    }    
  }

  public void solve () {

    int N = in.nextInt();
    int[] get = new int[N];
    int[] price = new int[N];
    int[] sell = new int[N];
    
    for (int i=0; i<N; i++) {
      get[i] = in.nextInt();
      price[i] = in.nextInt();
      sell[i] = in.nextInt();
    }
    
    int res=0;
    TreeSet<Offer> pq = new TreeSet<Offer>();
    
    for (int i=N-1; i>=0; i--) {
      pq.add(new Offer(price[i],sell[i],i));
      
      while (!pq.isEmpty() && get[i]>0) {
	int p = pq.first().price;
	int n = pq.first().num;
	int id = pq.first().id;
	pq.remove(pq.first());
	
	int s = Math.min(get[i], n);
	res+=s*p;
	get[i]-=s;
	n-=s;
	
	if (n>0) pq.add(new Offer(p,n,id));
      }
    }
    
    System.out.println(res);
  }
  
  static Scanner in = new Scanner(System.in);

  public static void main (String[] args) {
    
    int runs = in.nextInt();
    while (runs-->0) { new Jan().solve(); }
  }
};

