#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
}
//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 (orc1 + orc2 + orc3 >= c[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+IGFuc3dlcikgewoJCXJldHVybjsvL3VzZWxlc3MgYnJhbmNoCgl9CglpZiAocG9zaXRpb24gPT0gbikgewoJCWFuc3dlciA9IG1pbihhbnN3ZXIsIGN1cnJlbnQpOwoJCXJldHVybjsvL2ZpbmFsIHN0YXRlCgl9CgkvL3BheQoJZnVuKHBvc2l0aW9uICsgMSwgb3JjMSwgb3JjMiwgb3JjMywgY3VycmVudCArIGNbcG9zaXRpb25dKTsKCS8vaGlyZQoJZnVuKHBvc2l0aW9uICsgMSwgb3JjMSArIGtb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