def calc(h)
    stack = []
    h.each_char do |char|
        stack << char
        while stack.size >= 3 && stack[-3] =~ /\W/ && stack[-2] =~ /\w/ && stack[-1] =~ /\w/
            op, x, y = stack[-3..-1]
            3.times { stack.pop }
            case op
            when ?+ then stack << (x.to_i | y.to_i).to_s
            when ?* then stack << (x.to_i & y.to_i).to_s
            when ?^ then stack << (x.to_i ^ y.to_i).to_s
            end
        end
    end
    stack[0]
end

while 1
    h = gets.chomp.gsub(/[\[\]]/, '')
    break if h =~ /\./
    x = calc(h.tr('abcd', gets.chomp))
    n = (0..9999).count{|i| calc(h.tr('abcd', sprintf("%04d", i))) == x}
    puts "#{x} #{n}"
end

