output_description: For each data set, the output should contain one line with resulting set
  elements sorted in ascending order separated by blanks. If the result contains
  no set elements then the line should contain the text NULL.
input_description: Input consists of several pairs of lines difining sets and one pair of lines
  defining an expression. Each pair of lines for set definition includes the
  following.
  
  _Line 1_ : Set name (A, B, C, D, E), number of elements in a set.
  
  _Line 2_ : Set elements separated by blanks.
  
  Pair of lines for expression definition:
  
  _Line 1_ : R 0
  
  _Line 2_ : Expression consisting of set names, operators and parenthesis (no
  blanks).
  
  Number of sets can vary from 1 to 5. Set names can be specified in any order.
  Each set consists of 1-100 elements. Pair of lines for expression definition
  signals the end of data set. Input file includes several data sets. The number
  of datasets is less than 20.
attempt: import sys
  
  
  def rpn(str):
      r = []
      stack = []
      for i in range(0, len(str)):
          c = str[i]
          if c in "idsu":
              while len(stack) > 0:
                  if stack[-1] in "idsuc":
                      a = stack.pop()
                      r.extend(a)
                  else:
                      break
              stack.extend(c)
          elif c == "c":
              stack.extend(c)
          elif c == "(":
              stack.extend(c)
          elif c == ")":
              while len(stack) > 0:
                  a = stack.pop()
                  if a == "(":
                      break
                  r.extend(a)
          else:
              r.extend(c)
      while len(stack) > 0:
          a = stack.pop()
          r.extend(a)
      return r
  
  
  def intersect(a, b):
      r = []
      for e in a:
          if e in b:
              r.extend([e])
      return r
  
  
  def union(a, b):
      r = list(set(a + b))
      return r
  
  
  def diff(a, b):
      r = []
      for e in a:
          if e not in b:
              r.extend([e])
      return r
  
  
  def universal(sets):
      r = []
      for v in sets.values():
          r.extend(v)
      r = list(set(r))
      return r
  
  
  def calc(rpn, sets):
      stack = []
      U = universal(sets)
      for c in rpn:
          if c in "iuds":
              op2 = stack.pop()
              op1 = stack.pop()
              if c == "i":
                  x = intersect(op1, op2)
                  stack.append(x)
              elif c == "u":
                  x = union(op1, op2)
                  stack.append(x)
              elif c == "d":
                  x = diff(op1, op2)
                  stack.append(x)
              elif c == "s":
                  x = diff(op1, op2)
                  y = diff(op2, op1)
                  z = union(x, y)
                  stack.append(z)
          elif c == "c":
              op1 = stack.pop()
              x = diff(U, op1)
              stack.append(x)
          else:
              stack.append(sets[c])
      return stack.pop()
  
  
  lno = 0
  sets = {}
  name = ""
  for line in sys.stdin:
      lno += 1
      if lno % 2 == 1:
          name = line.strip().split()[0]
      elif name != "R":
          elem = list(map(int, line.strip().split()))
          sets[name] = elem
      else:
          e = rpn(line.strip())
          result = calc(e, sets)
          result.sort()
          for n in result:
              print(n, end=" ")
          print()
  
problem_description: D: Operations with Finite Sets
  
  Let _A, B, C, D, E_ be sets of integers and let _U_ is a universal set that
  includes all sets under consideration. All elements in any set are different
  (no repetitions).
  
  u - **union** of two sets, _AuB_ = {_x ∈ U_ : _x ∈ A_ or _x ∈ B_} is the set
  of all elements which belong to _A_ or _B_.
  
  i - **intersection** of two sets, _AiB_ = {_x ∈ U_ : _x ∈ A_ and _x ∈ B_} is
  the set of all elements which belong to both _A_ and _B_.
  
  d - **difference** of two sets, _AdB_ = {_x ∈ U_ : _x ∈ A_, _x ∉ B_} is the
  set of those elements of _A_ which do not belong to _B_.
  
  s - **symmetric difference** of two sets, _AsB_ = (_AdB_)_u_(_BdA_) consists
  of those elements which belong to _A_ or _B_ but not to both.
  
  c - **complement** of a set, _cA_ = {_x ∈ U_ : _x ∉ A_}, is set of elements
  which belong to _U_ but do not belong to _A_. Unary operator _c_ has higest
  precedence.
  
  The universal set _U_ is defined as a union of all sets specified in data.
  
  Your task is to determine the result of an expression, which includes sets,
  set operations and parenthesis (any number of parenthesis and any correct
  enclosure of parenthesis may take place).
samples: [{"input": "A 3\n    1 3 -1\n    B 4\n    3 1 5 7\n    D 1\n    5\n    R 0\n    cAiBdD\n    C 3\n    1 2 3\n    A 4\n    2 10 8 3\n    B 3\n    2 4 8\n    R 0\n    (As(AiB))uC", "output": "1 2 3 10"}]