#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <complex>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>
#include <cassert>
using namespace std;
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = " << x << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
const int M = 100111;
int x[M], p[M], m[M];
int main() {
ios :: sync_with_stdio(false); cin.tie(NULL);
cout << (fixed) << setprecision(6);
int test; cin >> test;
while (test--) {
int n; cin >> n;
FOR(i,1,n) cin >> x[i] >> p[i] >> m[i];
multiset< pair<int,int> > all;
long long res = 0;
FORD(i,n,1) {
all.insert(make_pair(-p[i], m[i]));
while (x[i] && !all.empty()) {
int price = -all.begin()->first;
int amount = all.begin()->second;
all.erase(all.begin());
int can = min(amount, x[i]);
res += can * (long long) price;
x[i] -= can;
amount -= can;
if (amount > 0) all.insert(make_pair(-price, amount));
}
DEBUG(res);
}
cout << res << endl;
}
return 0;
}
I2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPGxpc3Q+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8c3RhY2s+CiNpbmNsdWRlIDxjc3RkaW8+CiNpbmNsdWRlIDxzdHJpbmc+CiNpbmNsdWRlIDx2ZWN0b3I+CiNpbmNsdWRlIDxjc3RkbGliPgojaW5jbHVkZSA8Y3N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oTlVMTCk7CiAgICBjb3V0IDw8IChmaXhlZCkgPDwgc2V0cHJlY2lzaW9uKDYpOwogICAgaW50IHRlc3Q7IGNpbiA+PiB0ZXN0OwogICAgd2hpbGUgKHRlc3QtLSkgewogICAgICAgIGludCBuOyBjaW4gPj4gbjsKICAgICAgICBGT1IoaSwxLG4pIGNpbiA+PiB4W2ldID4+IHBbaV0gPj4gbVtpXTsKCiAgICAgICAgbXVsdGlzZXQ8IHBhaXI8aW50LGludD4gPiBhbGw7CiAgICAgICAgbG9uZyBsb25nIHJlcyA9IDA7CiAgICAgICAgRk9SRChpLG4sMSkgewogICAgICAgICAgICBhbGwuaW5zZXJ0KG1ha2VfcGFpcigtcFtpXSwgbVtpXSkpOwoKICAgICAgICAgICAgd2hpbGUgKHhbaV0gJiYgIWFsbC5lbXB0eSgpKSB7CiAgICAgICAgICAgICAgICBpbnQgcHJpY2UgPSAtYWxsLmJlZ2luKCktPmZpcnN0OwogICAgICAgICAgICAgICAgaW50IGFtb3VudCA9IGFsbC5iZWdpbigpLT5zZWNvbmQ7CiAgICAgICAgICAgICAgICBhbGwuZXJhc2UoYWxsLmJlZ2luKCkpOwoKICAgICAgICAgICAgICAgIGludCBjYW4gPSBtaW4oYW1vdW50LCB4W2ldKTsKCiAgICAgICAgICAgICAgICByZXMgKz0gY2FuICogKGxvbmcgbG9uZykgcHJpY2U7CiAgICAgICAgICAgICAgICB4W2ldIC09IGNhbjsKICAgICAgICAgICAgICAgIGFtb3VudCAtPSBjYW47CiAgICAgICAgICAgICAgICBpZiAoYW1vdW50ID4gMCkgYWxsLmluc2VydChtYWtlX3BhaXIoLXByaWNlLCBhbW91bnQpKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBERUJVRyhyZXMpOwogICAgICAgIH0KICAgICAgICBjb3V0IDw8IHJlcyA8PCBlbmRsOwogICAgfQogICAgcmV0dXJuIDA7Cn0KCg==