import sysdefsolution(n): time =0 y, x =0,0 q = [(0,0)] dy = [0,1,0,-1] dx = [1,0,-1,0] d =0whileTrue:# ๋ฐฉํฅ ์ค์ for plan in dir_plan:if time == plan[0]:if plan[1]=='D': d = (d +1) %4else: d = (d -1) %4# ๋ค์ ๋จธ๋ฆฌ ์์น ์ค์ ny = y + dy[d] nx = x + dx[d]# ๋ค์ ๋จธ๋ฆฌ๊ฐ ๋ชธ์ด๋ ๋ฒฝ์ด๋ผ๋ฉด time + 1 ์ถ๋ ฅif (ny,nx) in q or ny <0or ny >= n or nx <0or nx >= n:returnprint(time +1)# ๋ค์ ๋จธ๋ฆฌ๊ฐ ์ฌ๊ณผ๋ฉด ์์ทจ์ ์ถ๊ฐ๋ง ์งํelif arr[ny][nx] =='apple': arr[ny][nx] =0 q.append((ny,nx))# ๋ค์ ๋จธ๋ฆฌ์ ์๋ฌด๊ฒ๋ ์์ผ๋ฉด ์์ทจ์ ๋ชธ ์ถ๊ฐ ํ๊ณ ๊ผฌ๋ฆฌ๋ ๋นผ์else: q.append((ny,nx)) q.pop(0)# ์๊ฐ ์์น ์ ๋ฐ์ดํธ time +=1 y = ny x = nxn =int(input())arr = [[0] * n for _ inrange(n)]k =int(input())apples = [list(map(int,sys.stdin.readline().split()))for _ inrange(k)]for apple in apples: arr[apple[0]-1][apple[1]-1] ='apple'l =int(input())dir_plan = [list(sys.stdin.readline().split())for _ inrange(l)]for v in dir_plan: v[0]=int(v[0])solution(n)