#include <iostream>
#include <sstream>
using namespace std;
int main()
{
int n;
int red, white, yellow, black, poison, weight;
bool isPoisoned, isDead;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> red >> white >> yellow >> black >> poison >> weight;
string str;
cin.ignore();
getline(cin, str);
if (str == "") // no input
{
cout << weight << "\n";
continue;
}
for (char carrot : str)
{
if (isDead)
break;
if (carrot == ' ')
continue;
if (isPoisoned)
weight -= poison;
if (weight <= 0)
isDead = true;
switch (carrot)
{
case '1':
weight += red;
break;
case '2':
weight += white;
break;
case '3':
weight -= yellow;
break;
case '4':
weight -= black;
if (isPoisoned) // 毒性疊加
poison += poison;
else
isPoisoned = true;
break;
}
if (weight <= 0)
isDead = true;
}
if (isDead)
cout << "bye~Rabbit" << "\n";
else
cout << weight << "g" << "\n";
}
return 0;
};