
import scala.annotation.tailrec
object Main extends App {

  import scala.io.StdIn._

  solve
  @tailrec
  def solve: Unit = {
    val first = readLine
    if (first == null) return
    val (sets, suspects) = parseSet(first.split(' ').toList)
    implicit val set = sets
    val expression = readLine.trim match {
      case Expression(e) ⇒ println(suspects.filter(i ⇒ e(i)).let{
        case Nil ⇒ "NULL"
        case l ⇒ l.mkString(" ")
      })
      case _ ⇒ ???
    }
    solve
  }
  @tailrec
  def parseSet(line: List[String], sets: Map[Char, Int ⇒ Boolean] = Map.empty.withDefault(_ ⇒ _ ⇒ false), factors: List[Int] = Nil): (Char ⇒ Int ⇒ Boolean, List[Int]) = {
    line match {
      case "R"::_ => (sets, factors.distinct.sorted)
      case c::_ ⇒ val elements = readLine.trim.split(' ').map(_.toInt).sorted
        parseSet(readLine.trim.split(' ').toList, sets + (c.head, i ⇒ sortedContains(elements, i)), elements ++: factors)
      case _ ⇒ ???
    }
  }

  object Expression{
    def unapply(arg: String)(implicit set: Char => Int => Boolean): Option[Int ⇒ Boolean] = {
      arg.toList match {
        case s Second Nil ⇒ Some(s)
        case _ ⇒ ???
      }
    }
  }
  object First {
    def unapply(arg: List[Char])(implicit set: Char ⇒ Int ⇒ Boolean): Option[(Int ⇒ Boolean, List[Char])] = {
      arg match {
        case '('::(in Second ')'::t) ⇒ Some(in, t)
        case 'c'::(f First t) ⇒ Some(!f(_), t)
        case c::t if c.isUpper⇒ Some(set(c), t)
        case _ ⇒ None
      }
    }
  }
  object Second{
    def unapply(arg: List[Char])(implicit set: Char => Int => Boolean): Option[(Int ⇒ Boolean, List[Char])] = {
      arg match {
        case f First t ⇒ Some(parseAll(t, f))
        case _ ⇒ None
      }
    }
    @tailrec
    private def parseAll(arg: List[Char], left: Int ⇒ Boolean)(implicit set: Char ⇒ Int ⇒ Boolean): (Int ⇒ Boolean, List[Char]) = {
      arg match {
        case 'u'::(s First t) ⇒ parseAll(t, i ⇒ left(i) || s(i))
        case 'i'::(s First t) ⇒ parseAll(t, i ⇒ left(i) && s(i))
        case 'd'::(s First t) ⇒ parseAll(t, i ⇒ left(i) && !s(i))
        case 's'::(s First t) ⇒ parseAll(t, i ⇒ left(i) ^ s(i))
        case _ ⇒ (left, arg)
      }
    }
  }
  def sortedContains(array: Array[Int], target: Int): Boolean = {
    val i = lowerBound(array, target)
    if (array.indices.contains(i)) array(i) == target
    else false
  }
  def lowerBound(array: Array[Int], target: Int): Int = {
    var left = 0
    var right = array.length
    while(left < right){
      val mid = (left + right) / 2
      if (array(mid) < target){
        left = mid + 1
      }else {
        right = mid
      }
    }
    right
  }

  implicit class Extension[T](value :T){
    @inline
    def let[A](@inline func: T ⇒ A): A = func(value)
    @inline
    def also(@inline func: T ⇒ Unit): T = {func(value); value}
  }
}
