Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game.

Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bob computes the values of n variables. Each variable is assigned either a constant m-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values.

Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose.


-----Input-----

The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≤ n ≤ 5000; 1 ≤ m ≤ 1000). 

The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of:  Binary number of exactly m bits.  The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter.  

Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different.


-----Output-----

In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary numbers.


-----Examples-----
Input
3 3
a := 101
b := 011
c := ? XOR b

Output
011
100

Input
5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb

Output
0
0



-----Note-----

In the first sample if Peter chooses a number 011_2, then a = 101_2, b = 011_2, c = 000_2, the sum of their values is 8. If he chooses the number 100_2, then a = 101_2, b = 011_2, c = 111_2, the sum of their values is 15.

For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0.

Difficulty: competition

Starter code:

Solutions:
import sys
def calc(b0, b1, q):
    if q == 0:
        return b0 ^ b1
    if q == 1:
        return b0 | b1
    if q == 2:
        return b0 & b1
n, m = list(map(int,sys.stdin.readline().split()))
arr1 = {}
opt = ['XOR', 'OR', 'AND']
arr2 = []
for j in range(n):
    a, b = list(map(str,sys.stdin.readline().split(" := ")))
    b = b.split()
    if len(b) == 1:
        s = b[0]
        arr1[a] = s
    else:
        c = b[0]
        d = b[2]
        q = opt.index(b[1])
        arr2.append((a, c, d, q))
 
mins = ''
maxs = ''
d0 = {'?':0}
d1 = {'?':1}
for i in range(m):
    for a, b in list(arr1.items()):
        d0[a] = int(b[i])
        d1[a] = int(b[i])
    s0 = 0
    s1 = 0
    for a, c, d, q in arr2:
        b00 = d0[c]
        b01 = d0[d]
        b10 = d1[c]
        b11 = d1[d]
        c0 = calc(b00, b01, q)
        c1 = calc(b10, b11, q)
        s0 += (1 if c0 else 0)
        s1 += (1 if c1 else 0)
        d0[a] = c0
        d1[a] = c1
    if s1 < s0:
        mins += "1"
    else:
        mins += "0"
    if s1 > s0:
        maxs += "1"
    else:
        maxs += "0"
sys.stdout.write("{0}\n{1}".format(mins,maxs))



    



def sumBin(a, b, varMap):
  lhs = varMap[a]
  rhs = varMap[b]
  return bin(int(lhs, 2) + int(rhs, 2))[2:]

def andBin(a, b, varMap):
  lhs = varMap[a]
  rhs = varMap[b]
  return bin(int(lhs, 2) & int(rhs, 2))[2:]

def orBin(a, b, varMap):
  lhs = varMap[a]
  rhs = varMap[b]
  return bin(int(lhs, 2) | int(rhs, 2))[2:]

def xorBin(a, b, varMap):
  lhs = varMap[a]
  rhs = varMap[b]
  return bin(int(lhs, 2) ^ int(rhs, 2))[2:]

mapOper = {'AND': andBin, 'OR': orBin, 'XOR' : xorBin}


n, m = list(map(int, input().split()))
minMap = {"?": "0", "": "0"}
maxMap = {"?": "1"*m, "": "0"}

minSum = "0"
maxSum = "0"
for _ in range(n):
  name, _, expr = input().split(' ', 2)

  if len(expr.split(' ')) == 1:
    minMap[name] = expr
    maxMap[name] = expr
  else:
    lhs, oper, rhs = expr.split()
    minMap[name] = mapOper[oper](lhs, rhs, minMap).zfill(m)
    maxMap[name] = mapOper[oper](lhs, rhs, maxMap).zfill(m)

  minSum = sumBin("", name, minMap)
  maxSum = sumBin("", name, maxMap)


def countOnes(i, varMap):
  ones = 0
  for name, num in list(varMap.items()):
    if name != "?" and name != "":
      ones += num[i] == "1"
  return ones

minRes = ""
maxRes = ""

for i in range(m):
  zeroOnes = countOnes(i, minMap)
  oneOnes = countOnes(i, maxMap)

  if zeroOnes > oneOnes:
    maxRes += "0"
    minRes += "1"
  elif zeroOnes < oneOnes:
    maxRes += "1"
    minRes += "0"
  else:
    maxRes += "0"
    minRes += "0"

print (minRes)
print (maxRes)