#include <stdio.h>
#include <string.h>
char str[200010];
int dp[200010][3];
int num[200010];
int max(int a, int b)
{
return a> b? a: b;
}
int main()
{
int t, len, i;
scanf("%d", &t);
for(int cas = 1;cas <= t;cas++)
{
scanf("%s", str+1);
len = strlen(str+1);
str[0] = '0';
for(i = 1;i <= len;i++)
scanf("%d", &num[i]);
dp[0][0] = 0;
dp[0][1] = -9999;
for(i = 1;i <= len;i++)
{
if(str[i]=='1')
{
dp[i][1] = max(dp[i-1][0] + num[i],dp[i-1][1]);
dp[i][0] = -9999;
}
else if(str[i]=='0')
{
dp[i][0] = max(dp[i-1][1] + num[i],dp[i-1][0]);
dp[i][1] = -9999;
}
else
{
dp[i][1] = max(dp[i-1][0] + num[i],dp[i-1][1]);
dp[i][0] = max(dp[i-1][1] + num[i],dp[i-1][0]);
}
}
printf("Case #%d: %d\n",cas,max(dp[len][0],dp[len][1]));
}
}
08-14