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++: // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

string pat = "abcd";
int parse(string &s, int l, int r, string &p) {
    int paren = 0, pre = l+2, block = 0;
    if(s[l] == '[') {
        char op = s[l+1];
        int values[2] = {};
        for(int i=l+2; i<r; i++) {
            if(s[i] == '[') paren++;
            if(s[i] == ']') paren--;

            if(paren == 0) {
                values[block++] = parse(s, pre, i+1, p);
                pre = i+1;
            }
        }

        if(op == '+') {
            return values[0] | values[1];
        }
        if(op == '*') {
            return values[0] & values[1];
        }
        if(op == '^') {
            return values[0] ^ values[1];
        }
        assert(false);
    }
    else {
        assert(r - l == 1);
        return p[pat.find(s[l])] - '0';
    }
}
 
signed main() {
    string s;
    while(cin >> s, s != ".") {
        string pass; cin >> pass;
        int res = parse(s, 0, s.length(), pass);
        int cnt = 0;
        
        for(int i=0; i<10000; i++) {
            string p = "";
            int n = i;
            for(int k=0; k<4; k++) {
                p += (char)('0' + (n % 10));
                n /= 10;
            }

            int r = parse(s, 0, s.length(), p);
            if(res == r) cnt++;
        }

        printf("%lld %lld\n", res, cnt);
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#define for0(i, n) for(int i = 0; i < (n); i++)
#define for1(i, n) for(int i = 1; i <= (n);i++)
using namespace std;
int a, b, c, d;
int ar[10000];
int f(string s) {
	if (s.size() == 1) {
		if (s[0] == 'a')return a;
		if (s[0] == 'b')return b;
		if (s[0] == 'c')return c;
		if (s[0] == 'd')return d;
	}
	char ch = s[1]; int p, q;
	int e = 2;
	string s1;
	if (s[e] == '[') {
		int cnt = 1;
		s1 = "[";
		while (cnt) {
			e++;
			s1 += s[e];
			if (s[e] == '[')cnt++;
			if (s[e] == ']')cnt--;
		}
	}
	else s1 += s[e];
	p = f(s1);
	e++;
	//
	s1 = "";
	if (s[e] == '[') {
		int cnt = 1;
		s1 = "[";
		while (cnt) {
			e++;
			s1 += s[e];
			if (s[e] == '[')cnt++;
			if (s[e] == ']')cnt--;
		}
	}
	else s1 += s[e];
	q = f(s1);
	
	if (ch == '+')return p | q;
	if (ch == '*')return p & q;
	if (ch == '^')return p ^ q;
}
signed main() {
	string s;
	while (cin >> s, s != ".") {
		for (a = 0; a <= 9; a++)
			for (b = 0; b <= 9; b++)
				for (c = 0; c <= 9; c++)
					for (d = 0; d <= 9; d++)
						ar[1000 * a + 100 * b + 10 * c + d] = f(s);
		int p; cin >> p; cout << ar[p] << ' ';
		int cnt = 0; for0(i, 10000)if (ar[i] == ar[p])cnt++; cout << cnt << endl;
	}
}