An area named Renus, is divided into $(N \times M)$ cells. According to archaeological survey the area contains huge amount of treasure. Some cells out of $(N \times M)$ cells contain treasure. But problem is, you can't go to every cell as some of the cells are blocked.  
For every $a_{ij}$ cell($1 \leq i \leq N$,$1 \leq j \leq M$), your task is to find the distance of the nearest cell having treasure. 
Note:
- You can only traverse up, down, left and right from a given cell.
- Diagonal movements are not allowed.
- Cells having treasure can't be blocked, only empty cells ( cells without treasure) can be blocked.  

-----Input Format:------
- First line contains $T$, the number of test cases.
- Second line contains two space-separated integers $N\ and\ M$.
- Third line contains a single integer $X$ denoting number of cells having treasures, followed by $X$ lines containing two space-separated integers $x_i$ and $y_i$ denoting  position of row and column of $i^{th}$ treasure, for every $1\leq i \leq X$
- The next line contains a single integer $Y$ denoting the number of cells that are blocked, and it is followed by subsequent $Y$ lines containing  two space-separated integers $u_i$ and $v_i$ denoting position of row and column of blocked cells , for every $1\leq i \leq Y$

-----Constraints:------
- $1\le T \le 100$
- $1 \le N, M \le 200$
- $1 \le X < N*M$
- $0 \le Y <(N*M) - X$
- $1 \le x_i,u_j \le N, for\ every\ 1 \le i \le X\ and\ 1 \le j \le Y$
- $1 \le y_i,v_j \le M, for\ every\ 1 \le i \le X\ and\ 1 \le j \le Y$

-----Output Format:------
For each test case print a $N \times M$ matrix where each cell consists of distance of nearest treasure. Cells that are blocked will show "$X$" (without quotes). Also cells that doesn't have access to any treasure will show "$-1$" (without quotes).
Note: Co-ordinate of top left cell is $(1,1)$.

-----Sample Input-----
1
3 3
2
1 1
1 3
2
2 1
2 2

-----Sample Output-----
0 1 0 
X X 1
4 3 2

-----Explanation:-----
- Coordinates  (1,1) and (1,3)  shows "0" because they contain treasure and nearest distance is 0.
- Coordinates (2,1) and (2,2) shows "X" as they are blocked.
- Rest shows distance of nearest cell having treasure.

Starter code:

Solutions:
t=int(input())
for _ in range(t):
 n,m=[int(x) for x in input().split()]
 mat=[]
 ans=[]
 for i in range(n+2):
  l=[]
  p=[]
  for j in range(m+2):
   l.append(0)
   p.append(1000000000)
  mat.append(l)
  ans.append(p)
 y=int(input())
 for i in range(y):
  a,b=[int(x) for x in input().split()]
  mat[a][b]=1
  ans[a][b]=0
 y=int(input())
 for i in range(y):
  a,b=[int(x) for x in input().split()]
  mat[a][b]=1000000000
  ans[a][b]=1000000000
 for i in range(1,n+1):
  for j in range(1,m+1):
   if mat[i][j]==1 or mat[i][j]==1000000000:
    continue
   else:
    ans[i][j]=min(ans[i][j],ans[i][j-1]+1,ans[i-1][j]+1)
 for i in range(n,0,-1):
  for j in range(m,0,-1):
   if mat[i][j] == 1 or mat[i][j] == 1000000000:
    continue
   else:
    ans[i][j]=min(ans[i][j],ans[i+1][j]+1,ans[i][j+1]+1)
 for i in range(1,n+1):
  for j in range(m, 0, -1):
   if mat[i][j] == 1 or mat[i][j] == 1000000000:
    continue
   else:
    ans[i][j] = min(ans[i][j], ans[i - 1][j] + 1, ans[i][j + 1] + 1)
 for i in range(n, 0, -1):
  for j in range(1,m+1):
   if mat[i][j] == 1 or mat[i][j] == 1000000000:
    continue
   else:
    ans[i][j] = min(ans[i][j], ans[i + 1][j] + 1, ans[i][j - 1] + 1)
 for i in range(1,n+1):
  for j in range(1,m+1):
   if mat[i][j]==1 or mat[i][j]==1000000000:
    continue
   else:
    ans[i][j]=min(ans[i][j],ans[i][j-1]+1,ans[i-1][j]+1)
 for i in range(n,0,-1):
  for j in range(m,0,-1):
   if mat[i][j] == 1 or mat[i][j] == 1000000000:
    continue
   else:
    ans[i][j]=min(ans[i][j],ans[i+1][j]+1,ans[i][j+1]+1)
 for i in range(1,n+1):
  for j in range(m, 0, -1):
   if mat[i][j] == 1 or mat[i][j] == 1000000000:
    continue
   else:
    ans[i][j] = min(ans[i][j], ans[i - 1][j] + 1, ans[i][j + 1] + 1)
 for i in range(n, 0, -1):
  for j in range(1,m+1):
   if mat[i][j] == 1 or mat[i][j] == 1000000000:
    continue
   else:
    ans[i][j] = min(ans[i][j], ans[i + 1][j] + 1, ans[i][j - 1] + 1)
 for i in range(1,n+1):
  for j in range(1,m+1):
   if mat[i][j]==1000000000:
    print('X',end=" ")
   elif ans[i][j]>=1000000000:
    print('-1',end=" ")
   else:
    print(ans[i][j],end=" ")
  print()

import sys
import numpy as np
from collections import deque

readline = lambda : list(map(int, sys.stdin.readline().replace("\n", "").split(" ")))

(T,) = readline()

def run():
 N, M = readline()
 grid = np.full((N, M), -1)
 queue = deque()
 
 (X,) = readline()
 for _ in range(X):
  (x, y) = readline()
  (x, y) = (x-1, y-1)
  queue.append((x, y))
  grid[x, y] = 0
 
 (Y,) = readline()
 for _ in range(Y):
  (x, y) = readline()
  (x, y) = (x-1, y-1)
  grid[x, y] = -2
 
 while(len(queue) > 0):
  (x,y) = queue.popleft()
  #print('Propagate', (x+1,y+1), '; d =', grid[x,y])
  for (u, v) in [(x-1, y), (x+1, y), (x,y-1), (x,y+1)]:
   if u < 0 or v < 0 or u >= N or v >= M:
    continue
   if grid[u, v] == -1:
    grid[u, v] = grid[x, y] + 1
    queue.append((u, v))
 
 val = lambda x : str(x) if x != -2 else 'X'
 
 for i in range(N):
  print(' '.join(val(grid[i, j]) for j in range(M)))
  
for _ in range(T):
 run()