#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define pb push_back
const int N = 110000;
int pos;
char s[N];
map<char,int> mp;
int number(){
    return mp[s[pos++]];
}
int factor(){
    if(s[pos] == '['){
        pos++;
        int res = factor();
        pos++;
        return res;
    }
    else if(s[pos]=='+'){
        pos++;
        return factor()|factor();
    }
    else if(s[pos]=='*'){
        pos++;
        return factor()&factor();
    }
    else if(s[pos]=='^'){
        pos++;
        return factor()^factor();
    }
    else return number();
}
        
int main(){
    while(1){
        scanf("%s",s);
        if(s[0]=='.')exit(0);
        map<int,int> cnt;
        int k,ans;cin>>k;
        rep(i,10000){
            int now = i;
            rep(j,4){
                mp['d'-j]=now%10;
                now/=10;
            }
            pos = 0;
            int t = factor();
            if(k==i){
                ans = t;
            }
            cnt[t]++;
        }
        cout << ans << " " << cnt[ans] << endl;
    }
}
