#include <iostream>
#include <map>
#include <vector>
using namespace std;

void run()
{
    int N;
    cin >> N;
    vector<int> obtain(N), price(N), sell(N);
    for(int n = 0; n < N; ++n)
        cin >> obtain[n] >> price[n] >> sell[n];

    int value = 0;
    map<int, int, greater<int> > unsold;
    for(int n = N - 1; n >= 0; --n)
    {
        unsold[price[n]] += sell[n];
        while(!unsold.empty()) {
            map<int, int>::iterator i = unsold.begin();
            if(i->second < obtain[n])
            {
                obtain[n] -= i->second;
                value += i->second * i->first;
                unsold.erase(i);
            }
            else
            {
                value += obtain[n] * i->first;
                i->second -= obtain[n];
                break;
            }
        }
    }
    cout << value << endl;
}

int main()
{
    int runs;
    cin >> runs;
    while(runs--)
        run();
}
