https://www.acmicpc.net/problem/7562
import sys
from collections import deque
input = sys.stdin.readline
dx = [2, 2, -2, -2, -1, -1, 1, 1]
dy = [-1, 1, -1, 1, 2, -2, 2, -2]
def bfs(a, b, m, n):
que = deque()
visited[a][b] = 0
que.append((a, b))
while que:
x, y = que.popleft()
if x == m and y == n:
print(visited[x][y])
break
for i in range(8):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < num and 0 <= ny < num and visited[nx][ny] == -1:
visited[nx][ny] = visited[x][y] + 1
que.append((nx, ny))
testCase = int(input())
for _ in range(testCase):
num = int(input())
cnt = 0
a, b = map(int, input().split())
m, n = map(int, input().split())
visited = [[-1 for _ in range(num)]for _ in range(num)]
bfs(a, b, m, n)