import scala.annotation.tailrec
//https://onlinejudge.u-aizu.ac.jp/challenges/sources/JAG/Prelim/2883
object Main extends App {
  import scala.io.StdIn._
  solve
  @tailrec def solve: Unit = {
    val expresssion = readLine.trim.toList
    if (expresssion == '.'::Nil) return
    val password = new Password(readLine.trim.toInt)
    expresssion match{
      case hash Hash Nil ⇒ {
        val checkSum = hash.calHash(password)
        println(
          s"$checkSum ${(0 until 10000).count{p ⇒ hash.calHash(new Password(p)) == checkSum}}"
        )
      }
      case _ ⇒ throw new Exception
    }
    solve
  }

  case class Password(a: Int, b: Int, c: Int, d:Int) {
    def this(password: Int){
      this(password / 1000, (password / 100) % 10, (password / 10) % 10, password % 10)
    }
  }
  sealed trait HashFunc{
    def calHash(password: Password): Int
  }
  object Hash {
    def unapply(arg: List[Char]): Option[(HashFunc, List[Char])] = arg match {
      case '['::Operator(op, hashA Hash (hashB Hash ']'::t)) ⇒ Some(op.operation(hashA, hashB), t)
      case l Letter t ⇒ Some(l, t)
      case _ ⇒ None
    }
  }
  class Hash(private val func:(Password)⇒ Int) extends HashFunc {
    override def calHash(password: Password): Int = func(password)
  }
  sealed trait Letter extends HashFunc
  object Letter {
    def unapply(arg: List[Char]): Option[(Letter, List[Char])] = arg match {
      case c::t if ('a' to 'd').contains(c) ⇒ Some(new ReadLetter(c), t)
      case _ ⇒ None
    }
  }
  class ReadLetter(private val char: Char) extends Letter {
    override def calHash(password: Password): Int = char match {
      case 'a' ⇒ password.a
      case 'b' ⇒ password.b
      case 'c' ⇒ password.c
      case 'd' ⇒ password.d
      case _ ⇒ throw new Exception
    }
  }
  sealed trait Operator {
    def operation(hashA: HashFunc, hashB: HashFunc): HashFunc
  }
  object Operator {
    def unapply(arg: List[Char]): Option[(Operator, List[Char])] = arg match {
      case '+'::t ⇒ Some(OrOp, t)
      case '*'::t ⇒ Some(AndOp, t)
      case '^'::t ⇒ Some(XorOp, t)
      case _ ⇒ None
    }
  }
  object AndOp extends Operator {
    override def operation(hashA: HashFunc, hashB: HashFunc): HashFunc = new Hash({p ⇒ hashA.calHash(p) & hashB.calHash(p)})
  }
  object OrOp extends Operator {
    override def operation(hashA: HashFunc, hashB: HashFunc): HashFunc = new Hash({p ⇒ hashA.calHash(p) | hashB.calHash(p)})
  }
  object XorOp extends Operator {
    override def operation(hashA: HashFunc, hashB: HashFunc): HashFunc = new Hash({p ⇒ hashA.calHash(p) ^ hashB.calHash(p)})
  }
}

