t=["0"]*999 s=raw_input().replace(" ","") p=[] while 1: try:p=p+raw_input().replace(": ","").split(" ")[1:] except:break x=0 i=a=b=499 while 1: v=p[x+int(t[i])*len(s)] t[i]=v[0] if v[2]!="#":x=s.index(v[2]) else:print "".join(t[a:b+1]);break if(v[1]=="<"):i-=1;a=min(i,a) else:i+=1;b=max(i,b)