#include <bits/stdc++.h>
using namespace std;
int const nMax = 20;
int n, k[nMax], c[nMax];
int answer;
void fun (int position, int orc1, int orc2, int orc3, int current) {
if (current > answer) {
return;//useless branch
}
if (position == n) {
answer = min(answer, current);
return;//final state
}
bool fighted = false;
if (orc1 + orc2 + orc3 >= k[position]) {
int enemy = k[position];
int toFight = min(enemy, orc3);
orc3 -= toFight;
enemy -= toFight;
toFight = min(enemy, orc2);
orc2 -= toFight;
enemy -= toFight;
toFight = min(enemy, orc1);
orc1 -= toFight;
enemy -= toFight;
fun(position + 1, 0, orc1, orc2, current);
fighted = true;
}
//pay
fun(position + 1, orc1, orc2, orc3, current + c[position]);
//hire
fun(position + 1, orc1 + k[position], orc2, orc3, current + c[position] + c[position]);
//fight
if (!fighted && orc1 + orc2 + orc3 >= k[position]) {
int enemy = k[position];
int toFight = min(enemy, orc3);
orc3 -= toFight;
enemy -= toFight;
toFight = min(enemy, orc2);
orc2 -= toFight;
enemy -= toFight;
toFight = min(enemy, orc1);
orc1 -= toFight;
enemy -= toFight;
fun(position + 1, 0, orc1, orc2, current);
}
}
int main () {
int tests;
cin >> tests;
for (int t = 1; t <= tests; t++) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> k[i] >> c[i];
}
answer = 100000000;
fun(0, 0, 0, 0, 0);
cout << "#" << t << " " << answer << endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IGNvbnN0IG5NYXggPSAyMDsKCmludCBuLCBrW25NYXhdLCBjW25NYXhdOwoKaW50IGFuc3dlcjsKCnZvaWQgZnVuIChpbnQgcG9zaXRpb24sIGludCBvcmMxLCBpbnQgb3JjMiwgaW50IG9yYzMsIGludCBjdXJyZW50KSB7CglpZiAoY3VycmVudCA+IGFuc3dlcikgewoJCXJldHVybjsvL3VzZWxlc3MgYnJhbmNoCgl9CglpZiAocG9zaXRpb24gPT0gbikgewoJCWFuc3dlciA9IG1pb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