Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		S := io.NextLine()
		if S == "." {
			break
		}
		P := io.NextLine()
		i := 0
		val := hash(S, P, &i)
		cnt := 0
		for j := 0; j < 10000; j++ {
			i = 0
			if hash(S, fmt.Sprintf("%04d", j), &i) == val {
				cnt++
			}
		}
		io.Println(val, cnt)
	}
}

func hash(s, n string, i *int) int {
	if s[*i] == '[' {
		*i++
		switch s[*i] {
		case '+':
			*i++
			l := hash(s, n, i)
			r := hash(s, n, i)
			*i++
			return l | r
		case '*':
			*i++
			l := hash(s, n, i)
			r := hash(s, n, i)
			*i++
			return l & r
		case '^':
			*i++
			l := hash(s, n, i)
			r := hash(s, n, i)
			*i++
			return l ^ r
		}
	}
	x := int(n[s[*i]-'a'] - '0')
	*i++
	return x
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = (1e+9) + 7;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-4;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
int calc(string s, int k) {
	int a[4];
	a[3] = k % 10; k /= 10;
	a[2] = k % 10; k /= 10;
	a[1] = k % 10; k /= 10;
	a[0] = k;
	int n = s.length();
	stack<char> ord;
	stack<int> num;
	rep(i, n) {
		if (s[i] == ']') {
			int u = num.top(); num.pop();
			int v = num.top(); num.pop();
			char od = ord.top(); ord.pop();
			if (od == '+') {
				num.push(u|v);
			}
			else if (od == '*') {
				num.push(u&v);
			}
			else {
				num.push(u^v);
			}
		}
		else if (s[i] != '[') {
			if ('a' <= s[i] && s[i] <= 'd') {
				num.push(a[s[i] - 'a']);
			}
			else {
				ord.push(s[i]);
			}
		}
	}
	return num.top();
}
int main() {
	string s;
	while (cin >> s) {
		if (s == ".")break;
		int h; cin >> h;
		int val = calc(s, h);
		cout << val << " ";
		int c = 0;
		rep(i, 10000) {
			int v = calc(s, i);
			if (v == val)c++;
		}
		cout << c << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define INF (1LL<<30)
#define DEKAI 1000000007
#define lp(i,n) for(int i=0;i<n;i++)
#define int long long

int hah(string s){
	while(1){
		int check=0;
		if(s.size()<4)break;
		lp(i,s.size()-4){
			if(s[i]=='['&&s[i+4]==']'){
				char op=s[i+1];
				int x=s[i+2]-'0';
				int y=s[i+3]-'0';
				int ref;
				if(op=='+'){
					ref=(x|y);
				}
				if(op=='*'){
					ref=(x&y);
				}
				if(op=='^'){
					ref=(x^y);
				}
				check=1;
				string sbef=s.substr(0,i);
				char c=ref+'0';
				string scen{c};
				string saf=s.substr(i+5,s.size()-i-4);
				s=sbef+scen+saf;
				/*
				cout<<sbef<<" "<<saf<<" "<<scen<<endl;
				cout<<op<<" "<<x<<" "<<y<<" "<<ref<<endl;
				cout<<s<<endl;
				int hoge;
				cin>>hoge;
	*/
				break;
			}
		}
		if(check==0)break;
	}
	return s[0]-'0';
}

signed main(){
	while(1){
		string s;
		cin>>s;
		if(s==".")break;
		int n;
		cin>>n;
		int tx[100000];
		lp(i,100000)tx[i]=0;
		int memo;
		lp(z,10000){
			string x=s;
			lp(i,x.size()){
				if(x[i]=='a'){
					x[i]=(z/1000)+'0';
				}
				if(x[i]=='b'){
					x[i]=(z%1000/100)+'0';
					
				}
				if(x[i]=='c'){
					x[i]=(z%100/10)+'0';
					
				}
				if(x[i]=='d'){
					x[i]=(z%10)+'0';
				}
			}
			int zx=hah(x);
			tx[zx]++;
			if(n==z)memo=zx;
		}
		cout<<memo<<" "<<tx[memo]<<endl;
	}
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using P   = pair<ll, ll>;
using mat = vector<vector<ll>>;
typedef string::const_iterator State;
class ParseError {};

void IOS() { ios::sync_with_stdio(false), cin.tie(0); }

const ll INF = 1e16;
const ll MOD = 1e9 + 7;
const ld EPS = 1e-12;

template <typename T>
void dump(T x) { cout << x << endl; }
void dumpf(ld x, int t) { cout << setprecision(t) << fixed << x << endl; }
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
	fill((T *)array, (T *)(array + N), val);
}
inline ll mod(ll a, ll b) {
	if (a > 0) return a % b;
	if (a % b == 0) return 0;
	ll x = -a / b + 1;
	a += x * b;
	return a % b;
}
ll powm(ll a, ll b, ll c) {
	if (b == 0)
		return 1;
	else if (b % 2 == 0) {
		ll d = powm(a, b / 2, c);
		return (d * d) % c;
	} else
		return (a * powm(a, b - 1, c)) % c;
}
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

//ll dx[4] = {1, 0, -1, 0};
//ll dy[4] = {0, 1, 0, -1};
int key[4];
int number(State &);
int factor(State &);
int expr(State &);

int number(State &begin) {
	int ret = key[*begin - 'a'];
	begin++;
	return ret;
}

int expr(State &begin) {
	if (*begin == '[') {
		begin++;
		if (*begin == '+') {
			begin++;
			ll left  = expr(begin);
			ll right = expr(begin);
			begin++;
			return left | right;
		} else if (*begin == '*') {
			begin++;
			ll left  = expr(begin);
			ll right = expr(begin);
			begin++;
			return left & right;
		} else if (*begin == '^') {
			begin++;
			ll left  = expr(begin);
			ll right = expr(begin);
			begin++;
			return left ^ right;
		}
	} else {
		return number(begin);
	}
}

int main() {
	string s, t;
	while (1) {
		cin >> s;
		if (s == ".") break;
		cin >> t;
		for (int i = 0; i < 4; i++) {
			key[i] = t[i] - '0';
		}
		State begin = s.begin();
		ll tmp		= expr(begin);
		ll ans		= 0;
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				for (int k = 0; k < 10; k++) {
					for (int l = 0; l < 10; l++) {
						key[0] = i, key[1] = j, key[2] = k, key[3] = l;
						begin   = s.begin();
						ll unko = expr(begin);
						if (tmp == unko) ans++;
					}
				}
			}
		}
		cout << tmp << " " << ans << endl;
	}
	return 0;
}