There is a field with plants — a grid with $N$ rows (numbered $1$ through $N$) and $M$ columns (numbered $1$ through $M$); out of its $NM$ cells, $K$ cells contain plants, while the rest contain weeds. Two cells are adjacent if they have a common side.
You want to build fences in the field in such a way that the following conditions hold for each cell that contains a plant:
- it is possible to move from this cell to each adjacent cell containing a plant without crossing any fences
- it is impossible to move from this cell to any cell containing weeds or to leave the grid without crossing any fences
The fences can only be built between cells or on the boundary of the grid, i.e. on the sides of cells. The total length of the built fences is the number of pairs of side-adjacent cells such that there is a fence built on their common side plus the number of sides of cells on the boundary of the grid which have fences built on them. Find the minimum required total length of fences that need to be built.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains three space-separated integers $N$, $M$ and $K$.
- $K$ lines follow. Each of these lines contains two space-separated integers $r$ and $c$ denoting that the cell in row $r$ and column $c$ contains a plant.

-----Output-----
For each test case, print a single line containing one integer — the minimum required length of fences.

-----Constraints-----
- $1 \le T \le 10$
- $1 \le N, M \le 10^9$
- $1 \le K \le 10^5$
- $1 \le r \le N$
- $1 \le c \le M$
- the cells containing plants are pairwise distinct

-----Subtasks-----
Subtask #1 (30 points): $1 \le N, M \le 1,000$
Subtask #2 (70 points): original constraints

-----Example Input-----
2
4 4 9
1 4
2 1 
2 2
2 3
3 1
3 3
4 1
4 2
4 3
4 4 1
1 1

-----Example Output-----
20
4

-----Explanation-----
Example case 1: The field looks like this ('x' denotes a cell containing a plant, '.' denotes a cell containing weeds):
...x
xxx.
x.x.
xxx.

An optimal solution is to build fences around the topmost plant (with length $4$), around the remaining eight plants (with length $12$) and around the hole between them (with length $4$). The total length is $4+12+4 = 20$.

Difficulty: interview

Starter code:

Solutions:
t=int(input())
while(t):
 t-=1
 d={}
 n,m,k=[int(x) for x in list(input().split())]
 sum=0
 while(k):
  k-=1
  x,y=[int(x) for x in list(input().split())]
  a=[-1,1,0,0]
  b=[0,0,-1,1]
  for i in range(4):
   if((x+a[i],y+b[i]) in d):
    sum-=1
   else:
    sum+=1
  d[(x,y)]=1
 print(sum)

# cook your dish here
import numpy as np
t=int(input())
while(t):
 t-=1
 n,m,k=[int(x) for x in list(input().split())]
 l=np.array([["."]*m]*n)
 sum=0
 while(k):
  k-=1
  x,y=[int(x) for x in list(input().split())]
  i=x-1
  j=y-1
  l[i][j]="X"
  if(i==0 or l[i-1][j]=="."):
   sum+=1
  else:
   sum-=1
  if(i==n-1 or l[i+1][j]=="."):
   sum+=1
  else:
   sum-=1
  if(j==0 or l[i][j-1]=="."):
   sum+=1
  else:
   sum-=1
  if(j==m-1 or l[i][j+1]=="."):
   sum+=1
  else:
   sum-=1
 print(sum)

# cook your dish here
import numpy as np
t=int(input())
while(t):
 t-=1
 n,m,k=[int(x) for x in list(input().split())]
 l=np.array([["."]*m]*n)
 while(k):
  k-=1
  x,y=[int(x) for x in list(input().split())]
  l[x-1][y-1]="X"
 sum=0
 for i in range(n):
  for j in range(m):
   if(l[i][j]=="X"):
    if(i==0 or l[i-1][j]=="."):
     sum+=1
    if(i==n-1 or l[i+1][j]=="."):
     sum+=1
    if(j==0 or l[i][j-1]=="."):
     sum+=1
    if(j==m-1 or l[i][j+1]=="."):
     sum+=1
 print(sum)

from sys import stdin
def sin():
 return stdin.readline()

for _ in range(int(sin())):
 n,m,k = list(map(int, sin().split()))
 d={}
 ans=4*k
 for i in range(k):
  r,c = list(map(int, sin().split()))
  ans-=(2*d.get((r-1,c),0)+
   2*(d.get((r,c-1),0))+
   2*(d.get((r+1,c),0))+
   2*(d.get((r,c+1),0)))
  d[r,c]=1
 print(ans)


from sys import stdin
import numpy as np


def sin():
 return stdin.readline()

for _ in range(int(sin())):
 n,m,k = list(map(int, sin().split()))
 grid= np.zeros([n,m])
 # print(grid)
 ans=4*k
 for i in range(k):
  r,c = list(map(int, sin().split()))
  r-=1
  c-=1
  if r>0 and grid[r-1][c]==1:
   ans-=2
  if c>0 and grid[r][c-1]==1:
   ans-=2
  if c<m-1 and grid[r][c+1]==1:
   ans-=2
  if r<n-1 and grid[r+1][c]==1:
   ans-=2
  grid[r][c]=1
 print(ans)
 # print(grid)


from sys import stdin
def sin():
 return stdin.readline()

for _ in range(int(sin())):
 n,m,k = list(map(int, sin().split()))
 grid=[]
 ans=4*k
 for i in range(n):
  grid.append([0]*m)
 for i in range(k):
  r,c = list(map(int, sin().split()))
  r-=1
  c-=1
  if r>0 and grid[r-1][c]==1:
   ans-=2
  if c>0 and grid[r][c-1]==1:
   ans-=2
  if c<m-1 and grid[r][c+1]==1:
   ans-=2
  if r<n-1 and grid[r+1][c]==1:
   ans-=2
  grid[r][c]=1
 print(ans)


for _ in range(int(input())):
 n,m,k=map(int,input().split())
 d={}
 s=0
 for i in range(k):
  #print(d)
  x,y=map(int,input().split())
  d[x,y]=1
  s-=d.get((x-1,y),-1)
  s-=d.get((x,y-1),-1)
  s-=d.get((x,y+1),-1)
  s-=d.get((x+1,y),-1)
  #print(s)
 print(s)

from sys import stdin as si

def __starting_point():
 for _ in range(int(si.readline().strip())):
  _, _, k = list(map(int, si.readline().strip().split()))
  lookup = {}
  for i in range(k):
   x, y = list(map(int, si.readline().strip().split()))
   def check(d):
    if d in lookup:
     del lookup[d]
    else:
     lookup[d] = True
   check((x-1, y-1, x-1, y))
   check((x-1, y-1, x, y-1))
   check((x-1, y, x, y))
   check((x, y-1, x, y))
  print(len(lookup))

__starting_point()

# cook your dish here
for _ in range(int(input())):
 n,m,k=list(map(int,input().split()))
 d={}
 s=0
 for i in range(k):
  #print(d)
  x,y=list(map(int,input().split()))
  d[x,y]=1
  s-=d.get((x-1,y),-1)
  s-=d.get((x,y-1),-1)
  s-=d.get((x,y+1),-1)
  s-=d.get((x+1,y),-1)
  #print(s)
 print(s)


dirs = [(0, -1), (0, 1), (1, 0), (-1, 0)]
def fence(n, m, k, coords):
 f = 0
 for coord in coords:
  for dx, dy in dirs:
   x, y = coord[0]+dx, coord[1]+dy
   if x >= 0 and x < n and y >= 0 and y < m and (x, y) in coords:
    pass
   else:
    f += 1
 return f


for t in range(int(input())):
 n, m, k = map(int, input().split())
 coords = []
 for _ in range(k):
  a, b = map(int, input().split())
  coords.append((a-1, b-1))
 coords = set(coords)
 print(fence(n, m, k, coords))

# cook your dish here
def fence(n, m, k, coords):
 # def bfs(n, m, s, visited):
 #   from collections import deque
 #   q = deque([s])
 #   visited.add(s)
 #   dirs = [(0, -1), (0, 1), (1, 0), (-1, 0)]
 #   f = 0
 #   while q:
 #       sr, sc = q.popleft()
 #       for dr, dc in dirs:
 #           new_sr, new_sc = sr+dr, sc+dc
 #           new_s = (new_sr, new_sc)
 #           if new_sr >= 0 and new_sr < n and \
 #           new_sc >= 0 and new_sc < m and new_s not in visited: 
 #               q.append(new_s); visited.add(new_s)
 #           else: f += 1
 #   return f
 # r, visited = 0, set([])
 # for s in coords:
 #   if s not in visited:
 #       r += bfs(n, m, s, visited)
 # return r
 dirs = [(0, -1), (0, 1), (1, 0), (-1, 0)]
 f = 0
 for coord in coords:
  for dx, dy in dirs:
   x, y = coord[0]+dx, coord[1]+dy
   if x >= 0 and x < n and y >= 0 and y < m and (x, y) in coords:
    pass
   else:
    f += 1
 return f
 
t = int(input())
for _ in range(t):
 n, m, k = map(int, input().split())
 L = []
 for _ in range(k):
  a, b = map(int, input().split())
  L.append((a-1, b-1))
 coords = set(L)
 print(fence(n, m, k, coords))

for _ in range(int(input())):
 row,col,k = list(map(int, input().split()))
 fen=0
 garden = [[0 for i in range(col)] for j in range(row)]
 for i in range(k):
  r, c=list(map(int, input().split()))
  garden[r-1][c-1] = 1
 #print(garden)
 for c in range(col):
  for r in range(row):
   if r==0 or r==row-1:
    if garden[r][c]==1:
     fen+=1
     #print(c,r)
   if r!=0 :
    if garden[r][c]!=garden[r-1][c]:
     fen+=1
     #print(c,r)
   if c==0 or c==col-1:
    if garden[r][c]==1:
     fen+=1
     #print(c,r)
   if c!=0 :
    if garden[r][c]!=garden[r][c-1]:
     fen+=1
     #print(c,r)
 '''for r in range(row):
     for c in range(col):
      if c==0 or c==col-1:
       if garden[r][c]==1:
        fen+=1
        #print(c,r)
      if c!=0 :
       if garden[r][c]!=garden[r][c-1]:
        fen+=1
        #print(c,r)'''
 print(fen)# cook your dish here


for _ in range(int(input())):
 row,col,k = list(map(int, input().split()))
 fen=0
 garden = [[0 for i in range(col)] for j in range(row)]
 for i in range(k):
  r, c=list(map(int, input().split()))
  garden[r-1][c-1] = 1
 #print(garden)
 for c in range(col):
  for r in range(row):
   if r==0 or r==row-1:
    if garden[r][c]==1:
     fen+=1
     #print(c,r)
   if r!=0 :
    if garden[r][c]!=garden[r-1][c]:
     fen+=1
     #print(c,r)
 for r in range(row):
  for c in range(col):
   if c==0 or c==col-1:
    if garden[r][c]==1:
     fen+=1
     #print(c,r)
   if c!=0 :
    if garden[r][c]!=garden[r][c-1]:
     fen+=1
     #print(c,r)
 print(fen)# cook your dish here


for _ in range(int(input())):
 n,m,q = list(map(int,input().split()))
 p = []
 dic = {}
 for i in range(q):
  x,y = list(map(int,input().split()))
  p.append([x,y])
  dic[(x,y)] = 1

 cnt = 0
 #print(dic)
 for u in p:
  r1 = u[0]
  r2 = u[1]

  one = (r1-1,r2)
  two = (r1+1,r2)
  thr = (r1,r2-1)
  fo = (r1,r2+1)

  s = 0
  if one in dic:
   s = s+1

  if two in dic:
   s = s+1

  if thr in dic:
   s = s+1

  if fo in dic:
   s = s+1

  #print(s)

  re = 4-s
  cnt = cnt + re

 print(cnt)



from math import *
t=int(input())
while t>0:
 t-=1
 n,m,k=list(map(int,input().split()))
 a=[]
 b=[]
 ll=k*4
 for i in range(k):
  d=[int(x) for x in input().split()]
  a.append(d)
  d=d[::-1]
  b.append(d)
 a.sort()
 b.sort()
 
 for i in range(1,len(a)):
  #print(ll,a[i])
  if a[i][0]==a[i-1][0] and abs(a[i][1]-a[i-1][1])==1:
   ll-=2
 for i in range(1,len(b)):
  if b[i][0]==b[i-1][0] and abs(b[i][1]-b[i-1][1])==1:
   ll-=2 
 print(ll) 


# cook your dish here
# cook your dish here
def binarySearch(ar,l,r,x): 
 if r>=l: 
  mid=(l+r)//2
  if ar[mid] == x: 
   return mid 
  elif ar[mid] > x: 
   return binarySearch(ar,l,mid-1,x) 
  else: 
   return binarySearch(ar,mid+1,r,x) 
 else: 
  return -1
t=int(input())
for _ in range (t):
 r,c,q=list(map(int,input().split()))
 er=[]
 ar=[]
 coun=0
 for i in range (q):
  er.append(input().split())
  ar.append((int(er[i][0]))*(c+1)+int(er[i][1]))
 ar=sorted(ar)
 for i in range (q):
  l=0
  r=q-1
  x=((ar[i]//(c+1)+1)*(c+1)+(ar[i]%(c+1)))
  if binarySearch(ar,l,r,x)==-1:
   coun+=1
  x=((ar[i]//(c+1)-1)*(c+1)+(ar[i]%(c+1)))
  if binarySearch(ar,l,r,x)==-1:
   coun+=1
  x=ar[i]+1
  if i<q-1:
   if ar[i+1]!=x:
    coun+=1
  else:
   coun+=1
  x=ar[i]-1
  if i>0:
   if ar[i-1]!=x:
    coun+=1
  else:
   coun+=1
 print(coun)
  
  
  
  


for i in range(int(input())):
 n,m,k= map(int,input().split())
 a=[]
 item = {}
 count =0
 for j in range (k):
  x,y= map(int,input().split())
  ele=(x,y)
  item[ele]=1
  a.append([x,y])
  count+=4
  if (item.get((x-1,y))==1):
   count-=2
  if ((item.get((x,y+1)))==1):
   count-=2
  if ((item.get((x,y-1)))==1):
   count-=2
  if ((item.get((x+1,y)))==1):
   count-=2 
 print(count)

t = int(input())
for _ in range(t):
 n,m,k = map(int,input().strip().split())
 plants = []
 stat = {}
 count = 0
 for i in range(k):
  x,y = map(int,input().strip().split())
  ele = (x,y)
  stat [ele] = 1
  plants.append([x,y])
  count += 4 
  if(stat.get((x-1,y)) == 1):
   count -= 2
  if(stat.get((x,y+1)) == 1):
   count -= 2
  if(stat.get((x+1,y)) == 1):
   count -= 2
  if(stat.get((x,y-1)) == 1):
   count -= 2
 print(count)

def check(x,y,mat):
 tc=0
 if(mat[(x-1)][y]==0):
  tc+=1
 if(mat[(x)][y-1]==0):
  tc+=1
 if(mat[(x)][y+1]==0):
  tc+=1
 if(mat[(x+1)][y]==0):
  tc+=1
 #print(x,y,mat[x],tc)
 return tc



T=int(input())
for t in range(T):
 ls=list(map(int,input().split()))
 N=ls[0]
 M=ls[1]
 K=ls[2]
 mat=[[0 for i in range(M+2)] for i in range(N+2)]
 rows=[]
 cols=[] 
 for k in range(K):
  es=list(map(int,input().split()))
  mat[es[0]][es[1]]=1
  rows.append(es[0])
  cols.append(es[1])
 #for row in mat:
 #   print(row)
 count=0
 for i in range(len(rows)):
  count=count+check(rows[i],cols[i],mat)
 print(count)


# https://www.codechef.com/APRIL19B/problems/FENCE
'''
    Author - Subhajit Das
    University of Engineering and Management, Kolkata
    04/07/2019
'''


def main():
 for _ in range(int(input())):
  n, m, k = list(map(int, input().split()))
  plants = dict()
  ans = 0
  while(k != 0):
   r, c = list(map(int, input().split()))
   plants[(r,c)] = True
   ans += 4
   if(plants.get((r+1,c),False)):
    ans -= 2
   if(plants.get((r-1,c),False)):
    ans -= 2
   if(plants.get((r,c+1),False)):
    ans -= 2
   if(plants.get((r,c-1),False)):
    ans -= 2
   k -= 1
  print(ans)


def __starting_point():
 main()

__starting_point()

# https://www.codechef.com/APRIL19B/problems/FENCE
'''
    Author - Subhajit Das
    University of Engineering and Management, Kolkata
    04/07/2019
'''


def main():
 for _ in range(int(input())):
  n, m, k = list(map(int, input().split()))
  field = [[0]*m for _ in range(n)]
  moves = [(1, 0), (-1, 0), (0, 1), (0, -1)]
  ans = 0
  while(k != 0):
   r, c = list(map(int, input().split()))
   r -= 1
   c -= 1
   field[r][c] = 1
   for dx, dy in moves:
    nr, nc = r+dx, c+dy
    #print(f'r: {nr}\tc: {nc}')
    if(nr < 0 or nr >= n or nc < 0 or nc >= m):
     ans += 1
     continue
    if(field[nr][nc] == 1):
     ans -= 1
    else:
     ans += 1
   k -= 1
  print(ans)


def __starting_point():
 main()

__starting_point()