#pragma GCC optimize ("O3")
#pragma GCC target ("tune=native")
#pragma GCC target ("avx")

#include <bits/stdc++.h>

// 汎用マクロ
#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i,n) for (long long i=0, i##_len=(n); i<i##_len; i++)
#define RANGE(i,is,ie) for (long long i=(is), i##_end=(ie); i<=i##_end; i++)
#define DSRNG(i,is,ie) for (long long i=(is), i##_end=(ie); i>=i##_end; i--)
#define UNIQUE(v) { sort((v).begin(), (v).end()); (v).erase(unique((v).begin(), (v).end()), (v).end()); }
template<class T> bool chmax(T &a, const T &b) {if (a < b) {a = b; return true;} return false; }
template<class T> bool chmin(T &a, const T &b) {if (a > b) {a = b; return true;} return false; }
#define INF 0x7FFFFFFF
#define LINF 0x7FFFFFFFFFFFFFFFLL
#define Yes(q) (q ? "Yes" : "No")
#define YES(q) (q ? "YES" : "NO")
#define DUMP(q) cerr << "[DEBUG] " #q ": " << (q) << " at " __FILE__ ":" << __LINE__ << endl
#define DUMPALL(q) { cerr << "[DEBUG] " #q ": ["; REP(dumpall_i, (q).size()) { cerr << q[dumpall_i] << (dumpall_i == (q).size() - 1 ? "" : ", "); } cerr << "] at " __FILE__ ":" << __LINE__ << endl; }
template<class T> T gcd(const T &a, const T &b) { return a < b ? gcd(b, a) : b ? gcd(b, a % b) : a; }
template<class T> T lcm(const T &a, const T &b) { return a / gcd(a, b) * b; }

// gcc拡張マクロ
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll

// エイリアス
using  ll = long long;
using ull = unsigned long long;
using  ld = long double;
using namespace std;

// モジュール
ll sp_hash(const string &s, const string &p) {
    stack<ll> lstk;
    stack<char> lop;
    for (const char &c : s) {

        switch(c) {
            case '[': continue;
            case ']':
                {
                    ll c1 = lstk.top(); lstk.pop();
                    ll c2 = lstk.top(); lstk.pop();
                    ll res;
                    char op = lop .top(); lop .pop();
                    switch (op) {
                        case '+': res = c1 | c2; break;
                        case '*': res = c1 & c2; break;
                        case '^': res = c1 ^ c2; break;
                    }
                    lstk.push(res);
                }
                break;
            case 'a':
            case 'b':
            case 'c':
            case 'd':
                lstk.push(p[c - 'a'] - '0');
                break;
            case '+':
            case '*':
            case '^':
                lop.push(c);
                break;
        }

    }

    return lstk.top();

}

// 処理内容
int main() {
    
    // <Hash>   ::= <Letter> | '['<Op><Hash><Hash>']'
    // <Op>     ::= '+' | '*' | '^'
    // <Letter> ::= 'a' | 'b' | 'c' | 'd'

    string s, p;
    string i(4, '0');
    vector<ll> hashes;
    vector<ll> anss;
    while (cin >> s, s != ".") {
        cin >> p;

        ll phash = sp_hash(s, p);
        ll ans = 0;
        REP(_a, 10) {
            i[0] = '0' + _a;
            REP(_b, 10) {
                i[1] = '0' + _b;
                REP(_c, 10) {
                    i[2] = '0' + _c;
                    REP(_d, 10) {
                        i[3] = '0' + _d;
                        if (phash == sp_hash(s, i)) ans++;
                    }
                }
            }
        }

        hashes.push_back(phash);
        anss.push_back(ans);

    }

    REP(i, anss.size()) cout << hashes[i] << " " << anss[i] << endl;
    
}


Measured memory_bytes: 3164