defcheck(x,y):dx,dy=0,0whilex+dx<Nandarr[x+dx][y]:dx+=1whiley+dy<Nandarr[x][y+dy]:dy+=1result.append([dx*dy,dx,dy])foriinrange(x,x+dx):forjinrange(y,y+dy):arr[i][j]=0forTinrange(int(input())):N=int(input())arr=[list(map(int,input().split()))for_inrange(N)]result=[]foriinrange(N):forjinrange(N):ifarr[i][j]!=0:check(i,j)aws=sorted(result,key=lambdax:[x[0],x[1]])print(f'#{T+1}{len(aws)}',*[str(i[1])+' '+str(i[2])foriinaws])# DFS로 구현해보려 했는데.# 내가 원하는 위치값을 반환하고 바로 종료시키는 것이# 불가능해서. 최소한의 값인 가로 세로 이동값만을 뽑아# 정리하였다.# 마지막에 정렬이 더 복잡했다..
fromcollectionsimportdequedx=[0,1,0,-1]dy=[1,0,-1,0]forTinrange(int(input())):N=int(input())arr=[list(map(int,input()))for_inrange(N)]visited=[[float('inf')for_inrange(N)]for_inrange(N)]visited[0][0]=arr[0][0]q=deque([[0,0]])whileq:x,y=q.popleft()foriinrange(4):cx=x+dx[i]cy=y+dy[i]if0<=cx<Nand0<=cy<N:Sum=visited[x][y]+arr[cx][cy]ifvisited[cx][cy]>Sum:visited[cx][cy]=Sumq.append([cx,cy])print(f'#{T+1}{visited[N-1][N-1]}')# DFS로 구현해보려 했지만, 특정 위치에 왔을 때 종료를 # 구현하는데 어려움이 있어, BFS로 바꾸어 구현.
anycall={'2':"abc",'3':"def",'4':"ghi",'5':"jkl",'6':"mno",'7':"pqrs",'8':"tuv",'9':"wxyz"}forTinrange(int(input())):S,N=map(int,input().split())words=list(input().split())result=0forxinrange(len(words)):count=0fori,jinenumerate(str(S)):a=words[x][i]b=anycall[j]ifwords[x][i]inanycall[j]:count+=1else:breakifcount==len(str(S)):result+=1else:count=0print(f'#{T+1}{result}')# 비교적 쉬운 문제.