#include<bits/stdc++.h>
using namespace std;
typedef string::const_iterator State;
class ParseError{};

int letter(State& begin,const int& a,const int& b,const int& c,const int& d){
    char let = *begin;
    begin++;
    switch(let){
        case 'a':
            return a;
        case 'b':
            return b;
        case 'c':
            return c;
        case 'd':
            return d;
        default:
            throw ParseError();
    }
}

int Hash(State& begin,const int& a,const int& b,const int& c,const int& d){
    if(*begin == '['){
        begin++;
        char op = *begin;
        begin++;
        int hash1 = Hash(begin,a,b,c,d);
        int hash2 = Hash(begin,a,b,c,d);
        begin++;
        switch(op){
            case '+':
                return hash1 | hash2;
            case '*':
                return hash1 & hash2;
            case '^':
                return hash1 ^ hash2;
            default:
                throw ParseError();
        }
    }
    return letter(begin,a,b,c,d);
}

void solve(string S,string P){
    State begin = S.begin();
    int hash = Hash(begin,P[0]-'0',P[1]-'0',P[2]-'0',P[3]-'0');
    int res = 0;
    vector<int> v;
    for(int i = 0;i < 10;i++)v.push_back(i);
    for(auto a:v)for(auto b:v)for(auto c:v)for(auto d:v){
        begin = S.begin();
        if(hash == Hash(begin,a,b,c,d))res++;
    }
    cout << hash << ' ' << res << endl;
}

int main(){
    string S,P;
    while(true){
        cin >> S;
        if(S == ".")return 0;
        cin >> P;
        solve(S,P);
    }
}
