import sys from itertools import cycle L = [l for l in sys.stdin.read().splitlines()] I = L[0] # instructions for l in L[2:]: print(f'%{l}%') Q = [l.split(' = ') for l in L[2:] ] P = {k:v[1:-1].split(", ") for [k,v] in Q } print(P) C = 'AAA' N = 0 for i in cycle(I): if C == 'ZZZ': break C = P[C][i == 'R'] N += 1 print(C) print(N)