#!/usr/bin/python3 import sys import re paths = {} accpaths = [] argfile = "0 1 a\n0 0 b\n0 0 c\n1 2 b\n1 1 a\n1 1 c\n2 3 c\n2 2 b\n2 2 a\n3 3 a\n3 3 b\n3 3 c\n3\n" lines = argfile.splitlines(); for line in lines: answer = re.match('^([0-9]+) ([0-9]+) ([a-z]*)$', line); if answer: try: try: paths[answer.group(1)][answer.group(3)].add(answer.group(2)) except KeyError: paths[answer.group(1)][answer.group(3)] = {answer.group(2)} except KeyError: paths.update({answer.group(1):{}}) paths[answer.group(1)][answer.group(3)] = {answer.group(2)} else: accpaths.append(line) for input in sys.stdin.readlines(): y = {str(0)} for char in input[:-1]: x = set() for i in y: try: x.update(paths[i][char]) except KeyError: pass y = x if len(y.intersection(accpaths))>0: print("YES") else: print("NO")