#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 81

char equation[NUM],buf[5];
int value[4];

int getNUM(char ch){
	return ch - 'a';
}

int calc_Hash(int left,int right){

	if(equation[left] != '['){ //letter
		return value[getNUM(equation[left])];
	}

	//[カッコ]で囲まれている場合

	char op = equation[left+1];
	int left_value,right_value,ret;

	if(equation[left+2] == '['){

		int depth = 0;
		int close_pos;

		for(int i = left+2; i <= right; i++){

			if(equation[i] == '['){

				depth++;

			}else if(equation[i] == ']'){

				depth--;

				if(depth == 0){

					close_pos = i;
					break;
				}
			}
		}

		left_value = calc_Hash(left+2,close_pos);
		right_value = calc_Hash(close_pos+1,right-1);

	}else{ //equation[left+2] == letter

		left_value = calc_Hash(left+2,left+2);
		right_value = calc_Hash(left+3,right-1);
	}

	switch(op){
	case '+':
		ret = left_value|right_value;
		break;
	case '*':
		ret = left_value&right_value;
		break;
	case '^':
		ret = left_value^right_value;
		break;
	}

	return ret;
}

void func(){

	scanf("%s",buf);

	for(int i = 0; i < 4; i++){

		value[i] = buf[i]-'0';
	}

	int length;

	for(length = 0; equation[length] != '\0'; length++);

	int first_value = calc_Hash(0,length-1);
	int count = 0;

	for(int a = 0; a <= 9; a++){
		for(int b = 0; b <= 9; b++){
			for(int c = 0; c <= 9; c++){
				for(int d = 0; d <= 9; d++){

					value[0] = a;
					value[1] = b;
					value[2] = c;
					value[3] = d;

					if(calc_Hash(0,length-1) == first_value){
						count++;
					}
				}
			}
		}
	}

	printf("%d %d\n",first_value,count);
}

int main(){

	while(true){
		scanf("%s",equation);
		if(equation[0] == '.')break;

		func();
	}

	return 0;
}

