On a strip of land of length $n$ there are $k$ air conditioners: the $i$-th air conditioner is placed in cell $a_i$ ($1 \le a_i \le n$). Two or more air conditioners cannot be placed in the same cell (i.e. all $a_i$ are distinct).

Each air conditioner is characterized by one parameter: temperature. The $i$-th air conditioner is set to the temperature $t_i$.

Example of strip of length $n=6$, where $k=2$, $a=[2,5]$ and $t=[14,16]$.

For each cell $i$ ($1 \le i \le n$) find it's temperature, that can be calculated by the formula $$\min_{1 \le j \le k}(t_j + |a_j - i|),$$

where $|a_j - i|$ denotes absolute value of the difference $a_j - i$.

In other words, the temperature in cell $i$ is equal to the minimum among the temperatures of air conditioners, increased by the distance from it to the cell $i$.

Let's look at an example. Consider that $n=6, k=2$, the first air conditioner is placed in cell $a_1=2$ and is set to the temperature $t_1=14$ and the second air conditioner is placed in cell $a_2=5$ and is set to the temperature $t_2=16$. In that case temperatures in cells are:

temperature in cell $1$ is: $\min(14 + |2 - 1|, 16 + |5 - 1|)=\min(14 + 1, 16 + 4)=\min(15, 20)=15$;

temperature in cell $2$ is: $\min(14 + |2 - 2|, 16 + |5 - 2|)=\min(14 + 0, 16 + 3)=\min(14, 19)=14$;

temperature in cell $3$ is: $\min(14 + |2 - 3|, 16 + |5 - 3|)=\min(14 + 1, 16 + 2)=\min(15, 18)=15$;

temperature in cell $4$ is: $\min(14 + |2 - 4|, 16 + |5 - 4|)=\min(14 + 2, 16 + 1)=\min(16, 17)=16$;

temperature in cell $5$ is: $\min(14 + |2 - 5|, 16 + |5 - 5|)=\min(14 + 3, 16 + 0)=\min(17, 16)=16$;

temperature in cell $6$ is: $\min(14 + |2 - 6|, 16 + |5 - 6|)=\min(14 + 4, 16 + 1)=\min(18, 17)=17$.

For each cell from $1$ to $n$ find the temperature in it.


-----Input-----

The first line contains one integer $q$ ($1 \le q \le 10^4$) — the number of test cases in the input. Then test cases follow. Before each test case, there is an empty line.

Each test case contains three lines. The first line contains two integers $n$ ($1 \le n \le 3 \cdot 10^5$) and $k$ ($1 \le k \le n$) — the length of the strip of land and the number of air conditioners respectively.

The second line contains $k$ integers $a_1, a_2, \ldots, a_k$ ($1 \le a_i \le n$) — positions of air conditioners on the strip of land.

The third line contains $k$ integers $t_1, t_2, \ldots, t_k$ ($1 \le t_i \le 10^9$) — temperatures of air conditioners.

It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.


-----Output-----

For each test case output $n$ integers separated by space: temperatures of air in cells.


-----Examples-----

Input
5

6 2
2 5
14 16

10 1
7
30

5 5
3 1 4 2 5
3 1 4 2 5

7 1
1
1000000000

6 3
6 1 3
5 5 5
Output
15 14 15 16 16 17 
36 35 34 33 32 31 30 31 32 33 
1 2 3 4 5 
1000000000 1000000001 1000000002 1000000003 1000000004 1000000005 1000000006 
5 6 5 6 6 5


-----Note-----

None

Difficulty: MEDIUM
Tags: Shortest paths

Sorting

Amortized analysis

Implementation

Dynamic programming

Data structures
Skill types: Dynamic programming

Data structures

Sorting

Amortized analysis

Starter code:

Solutions:
import sys
input = sys.stdin.readline
import math
for t in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	(*pos,) = map(int, input().split())
	a = [0] * n
	idx = 0
	for i in map(int, input().split()):
		a[pos[idx] - 1] = i
		idx += 1
	forward_dis = [0] * n
	reverse_dis = [0] * n
	fp = -math.inf
	rp = math.inf
	for i in range(n):
		if a[i]:
			if fp == -math.inf or a[i] < a[fp] + (i - fp):
				fp = i
		if a[n - 1 - i]:
			if rp == math.inf or a[n - 1 - i] < a[rp] + (rp - (n - 1 - i)):
				rp = n - 1 - i
		forward_dis[i] = a[fp] + (i - fp) if fp > -math.inf else math.inf
		reverse_dis[n - 1 - i] = a[rp] + (rp - (n - 1 - i)) if rp < math.inf else math.inf
	print(*[min(forward_dis[i], reverse_dis[i]) for i in range(n)])


t = int(input())
for _ in range(t):
	_ = input()
	(n, k) = map(int, input().split())
	pos = list(map(int, input().split()))
	temp = list(map(int, input().split()))
	ar = [0] * (n + 1)
	dp = [2 * 10 ** 9] * (n + 1)
	for i in range(k):
		ar[pos[i]] = temp[i]
	last = None
	for i in range(1, n + 1):
		if ar[i]:
			if last == None:
				last = ar[i]
			elif ar[i] < last:
				last = ar[i]
		if last:
			dp[i] = min(dp[i], last)
			last += 1
	last = None
	for i in range(n, 0, -1):
		if ar[i]:
			if last == None:
				last = ar[i]
			elif ar[i] < last:
				last = ar[i]
		if last:
			dp[i] = min(dp[i], last)
			last += 1
	print(' '.join((str(dp[i]) for i in range(1, n + 1))))


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time
inf = 10 ** 20
mod = 10 ** 9 + 7

def LI():
	return list(map(int, input().split()))

def II():
	return int(input())

def LS():
	return list(input().split())

def S():
	return input()

def solve():
	(n, k) = LI()
	pos = LI()
	tmp = LI()
	ans = [inf for _ in range(n)]
	cooler = [inf for _ in range(n)]
	for i in range(k):
		t = tmp[i]
		p = pos[i] - 1
		cooler[p] = t
	nowtmp = inf
	for i in range(n):
		nowtmp = min(nowtmp + 1, cooler[i])
		ans[i] = min(ans[i], nowtmp)
	nowtmp = inf
	for i in range(n)[::-1]:
		nowtmp = min(nowtmp + 1, cooler[i])
		ans[i] = min(ans[i], nowtmp)
	return ans

def main():
	t = II()
	res = list()
	for _ in range(t):
		_ = S()
		res.append(solve())
	for ans in res:
		print(*ans)
	return 0
main()


from collections import deque, defaultdict
from math import sqrt, ceil, factorial, floor, inf, log2, sqrt, gcd
import bisect
import copy
from itertools import combinations
import sys

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
for _ in range(int(input())):
	s = input()
	(n, k) = get_ints()
	a = get_array()
	t = get_array()
	c = []
	for i in range(k):
		c.append([a[i] - 1, t[i]])
	c.sort()
	ans1 = [inf] * n
	ans2 = [inf] * n
	ans = inf
	ind = len(c) - 1
	for i in range(n - 1, -1, -1):
		if ind >= 0 and i == c[ind][0]:
			ans1[i] = min(c[ind][1], ans + 1)
			ans = ans1[i]
			ind -= 1
		else:
			ans1[i] = ans + 1
			ans += 1
	ind = 0
	ans = inf
	for i in range(n):
		if ind < len(c) and i == c[ind][0]:
			ans2[i] = min(c[ind][1], ans + 1)
			ans = ans2[i]
			ind += 1
		else:
			ans2[i] = ans + 1
			ans += 1
	fin = []
	for i in range(n):
		fin.append(min(ans1[i], ans2[i]))
	print(*fin)


import sys
import heapq
input = sys.stdin.readline

def solve():
	_ = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	for i in range(k):
		a[i] -= 1
	pq = []
	ans = [1 << 32] * n
	for j in range(k):
		ans[a[j]] = min(ans[a[j]], t[j])
		heapq.heappush(pq, (ans[a[j]], a[j]))
	while pq:
		(temp, i) = heapq.heappop(pq)
		if ans[i] < temp:
			continue
		if i + 1 < n and ans[i + 1] > ans[i] + 1:
			ans[i + 1] = ans[i] + 1
			heapq.heappush(pq, (ans[i + 1], i + 1))
		if i >= 1 and ans[i - 1] > ans[i] + 1:
			ans[i - 1] = ans[i] + 1
			heapq.heappush(pq, (ans[i - 1], i - 1))
	print(*ans)
for nt in range(int(input())):
	solve()


t = int(input())
for i in range(t):
	blank = input()
	total = list(map(int, input().split()))
	pos = list(map(int, input().split()))
	tmp = list(map(int, input().split()))
	cells = total[0]
	cons = total[1]
	for i in range(len(pos)):
		pos[i] = pos[i] - 1
	di = {}
	for i in range(cons):
		di[pos[i]] = tmp[i]
	temp = []
	pos.sort()
	for i in pos:
		temp.append(di[i])
	if cons == 1:
		lasttemp = []
		for i in range(pos[0]):
			lasttemp.append(temp[0] + pos[0] - i)
		for i in range(pos[0], cells, 1):
			lasttemp.append(temp[0] + i - pos[0])
		print(*lasttemp)
	if cons > 1:
		diR = {}
		diL = {}
		for i in range(pos[0]):
			diR[i] = di[pos[0]] + pos[0] - i
		diR[pos[0]] = di[pos[0]]
		for i in range(pos[0] + 1, cells, 1):
			if i not in di.keys():
				diR[i] = diR[i - 1] + 1
			else:
				diR[i] = min(di[i], diR[i - 1] + 1)
		for i in range(pos[len(pos) - 1] + 1, cells, 1):
			diL[i] = di[pos[len(pos) - 1]] - pos[len(pos) - 1] + i
		diL[pos[len(pos) - 1]] = di[pos[len(pos) - 1]]
		for i in range(pos[len(pos) - 1] - 1, -1, -1):
			if i not in di.keys():
				diL[i] = diL[i + 1] + 1
			else:
				diL[i] = min(di[i], diL[i + 1] + 1)
		lasttemp = []
		for i in range(cells):
			lasttemp.append(min(diR[i], diL[i]))
		print(*lasttemp)


from sys import stdin, stdout
input = stdin.readline

def main():
	t = int(input())
	for z in range(t):
		input()
		(n, k) = map(int, input().split())
		ai = list(map(int, input().split()))
		ti = list(map(int, input().split()))
		num = 10 ** 10
		ans = [num] * n
		ans2 = [num] * n
		ans3 = [num] * n
		for i in range(k):
			ans[ai[i] - 1] = ti[i]
			ans2[ai[i] - 1] = ti[i]
		for i in range(1, n):
			ans[i] = min(ans[i - 1] + 1, ans[i])
		for i in range(n - 2, -1, -1):
			ans2[i] = min(ans2[i + 1] + 1, ans2[i])
		for i in range(n):
			ans3[i] = min(ans[i], ans2[i])
		print(*ans3)
main()


import sys
q = int(input())
for _ in range(q):
	_ = input()
	(n, k) = map(int, input().split())
	A = list(map(int, input().split()))
	temp = list(map(int, input().split()))
	L = [sys.maxsize] * n
	R = [sys.maxsize] * n
	for i in range(k):
		L[A[i] - 1] = temp[i]
		R[A[i] - 1] = temp[i]
	for i in range(1, n):
		L[i] = min(L[i - 1] + 1, L[i])
	for i in range(n - 2, -1, -1):
		R[i] = min(R[i + 1] + 1, R[i])
	Ans = [0] * n
	for i in range(n):
		Ans[i] = min(L[i], R[i])
	for a in Ans:
		print(a, end=' ')
	print()


for _ in range(int(input())):
	x = input()
	(n, k) = map(int, input().split())
	l = [1 << 31] * n
	for (idx, t) in zip(map(int, input().split()), map(int, input().split())):
		l[idx - 1] = t
	for i in range(1, n):
		l[i] = min(l[i], l[i - 1] + 1)
	for i in range(n - 2, -1, -1):
		l[i] = min(l[i], l[i + 1] + 1)
	print(*l)


for q in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	ans = [10 ** 14] * n
	for i in range(len(a)):
		ans[a[i] - 1] = t[i]
	for i in range(n - 1):
		if ans[i] != 10 ** 14:
			ans[i + 1] = min(ans[i] + 1, ans[i + 1])
	for i in range(n - 2, -1, -1):
		ans[i] = min(ans[i + 1] + 1, ans[i])
	print(*ans)


t = int(input())
INF = 9 * 10 ** 9
for _ in range(t):
	input()
	(n, k) = [int(x) for x in input().split()]
	a = [int(x) - 1 for x in input().split()]
	t = [int(x) for x in input().split()]
	cond = [(a[i], t[i]) for i in range(k)]
	cond.sort()
	res = [INF for _ in range(n)]
	for (mult, idx, ai) in [(1, 0, 0), (-1, n - 1, k - 1)]:
		pred = INF
		for _ in range(n):
			if n > idx >= 0 and 0 <= ai < k and (idx == cond[ai][0]):
				res[idx] = min(pred + 1, cond[ai][1], res[idx])
				ai += mult
				pred = res[idx]
			elif n > idx >= 0:
				res[idx] = min(pred + 1, res[idx])
				pred = res[idx]
			idx += mult
	for i in res:
		print(i, end=' ')
	print()


def solve(t_id):
	s = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	ans = [10 ** 10 for i in range(n)]
	pp = []
	for i in range(k):
		pp.append([a[i] - 1, t[i]])
	pp.sort()
	cur_ind = 0
	cur_temp = 10 ** 10
	for i in range(n):
		if cur_ind < k and pp[cur_ind][0] == i:
			cur_temp = min(cur_temp, pp[cur_ind][1])
			cur_ind += 1
		ans[i] = cur_temp
		cur_temp += 1
	cur_ind = k - 1
	cur_temp = 10 ** 10
	for i in range(n - 1, -1, -1):
		if cur_ind >= 0 and pp[cur_ind][0] == i:
			cur_temp = min(cur_temp, pp[cur_ind][1])
			cur_ind -= 1
		ans[i] = min(ans[i], cur_temp)
		cur_temp += 1
	print(*ans)
tests = 1
tests = int(input())
for i in range(tests):
	solve(i + 1)


import math
from collections import Counter, defaultdict
import sys
import heapq
mod = 10 ** 9 + 7
input = sys.stdin.readline
readInt = lambda : int(input().strip())
readfloat = lambda : float(input().strip())
readStr = lambda : input().strip()
intList = lambda : list(map(int, input().strip().split()))
intMap = lambda : map(int, input().strip().split())
floatList = lambda : list(map(float, input().strip().split()))
floatMap = lambda : map(float, input().strip().split())
strList = lambda : list(input().strip().split())

def print(*args, end='\n', sep=' '):
	for i in args:
		sys.stdout.write(str(i))
		sys.stdout.write(sep)
	sys.stdout.write(end)

def solve():
	res = [10 ** 12 for i in range(n)]
	d = dict()
	for i in range(k):
		d[arr[i]] = trr[i]
	if 1 in d:
		res[0] = d[1]
	for i in range(1, n):
		if i + 1 not in d:
			res[i] = res[i - 1] + 1
		else:
			res[i] = min(res[i - 1] + 1, d[i + 1])
	if n in d:
		res[n - 1] = min(res[n - 1], d[n])
	for i in range(n - 2, -1, -1):
		if i + 1 not in d:
			res[i] = min(res[i], res[i + 1] + 1)
		else:
			res[i] = min([res[i], res[i + 1] + 1, d[i + 1]])
	return res
for _ in range(readInt()):
	input()
	(n, k) = intMap()
	arr = intList()
	trr = intList()
	print(*solve())


from audioop import reverse
for _ in range(int(input())):
	input()
	parameters = list(map(int, input().strip().split()))
	input_conditioners = dict(zip(list(map(lambda x: x - 1, map(int, input().strip().split()))), list(map(int, input().strip().split()))))
	conditioners = {location: temperature for (location, temperature) in sorted(input_conditioners.items(), key=lambda item: item[0])}
	lower_minima = list()
	upper_minima = list()
	temp = float('inf')
	for index in range(parameters[0]):
		temp = min(temp + 1, conditioners.get(index, float('inf')))
		lower_minima.append(temp)
	temp = float('inf')
	for index in reversed(range(parameters[0])):
		temp = min(temp + 1, conditioners.get(index, float('inf')))
		upper_minima.append(temp)
	upper_minima.reverse()
	for index in range(parameters[0]):
		print(min(lower_minima[index], upper_minima[index]), end=' ')
	print()


t = int(input())
for _ in range(t):
	empytLine = input()
	(n, k) = list(map(int, input().split()))
	conditionersPos = list(map(int, input().split()))
	conditionersTemp = list(map(int, input().split()))
	sol = [float('inf')] * n
	for i in range(k):
		sol[conditionersPos[i] - 1] = conditionersTemp[i]
	for i in range(1, n):
		sol[i] = min(sol[i], sol[i - 1] + 1)
	for i in range(n - 2, -1, -1):
		sol[i] = min(sol[i], sol[i + 1] + 1)
	print(*sol)


def solve_5():
	input()
	(n, k) = map(int, input().split())
	a_arr = [a - 1 for a in map(int, input().split())]
	t_arr = list(map(int, input().split()))
	res = [float('inf') for _ in range(n)]
	for i in range(k):
		res[a_arr[i]] = t_arr[i]
	for i in range(1, n):
		res[i] = min(res[i - 1] + 1, res[i])
	for i in range(n - 2, -1, -1):
		res[i] = min(res[i + 1] + 1, res[i])
	print(*res)
n = int(input())
for _ in range(n):
	solve_5()


for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	pos = list(map(int, input().split()))
	temp = list(map(int, input().split()))
	ans = [float('inf')] * n
	for i in range(k):
		ans[pos[i] - 1] = temp[i]
	for i in range(1, n):
		ans[i] = min(ans[i], ans[i - 1] + 1)
	for i in range(n - 2, -1, -1):
		ans[i] = min(ans[i], ans[i + 1] + 1)
	for i in range(n):
		print(ans[i], end=' ')
	print()


inf = 1e+21
for t in range(int(input())):
	space = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	L = [inf for i in range(n)]
	R = [inf for i in range(n)]
	c = [inf for i in range(n)]
	for i in range(len(a)):
		c[a[i] - 1] = t[i]
	L[0] = c[0]
	R[n - 1] = c[n - 1]
	for i in range(1, n):
		L[i] = min(L[i - 1] + 1, c[i])
	for i in range(n - 2, -1, -1):
		R[i] = min(R[i + 1] + 1, c[i])
	r = [min(R[i], L[i]) for i in range(n)]
	print(*r)


import sys, io, os, math, bisect, heapq
from collections import Counter, deque, defaultdict
try:
	inp = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
except:
	inp = lambda : sys.stdin.readline().encode()
L = lambda : list(map(int, inp().split()))
I = lambda : int(inp())
S = lambda : inp().strip().decode()
C = lambda : inp().split()

def pr(a):
	print(' '.join(list(map(str, a))))

def solve():
	q = S()
	(n, k) = L()
	a = L()
	b = L()
	d = [10 ** 10] * n
	for i in range(k):
		d[a[i] - 1] = b[i]
	c = [0] * n
	e = [0] * n
	c[0] = d[0]
	e[-1] = d[n - 1]
	for i in range(1, n):
		c[i] = min(c[i - 1] + 1, d[i])
		e[-i - 1] = min(e[-i] + 1, d[n - i - 1])
	for i in range(n):
		print(min(c[i], e[i]), end=' ')
	print()
for _ in range(I()):
	solve()


q = int(input())
out = []
for i in range(q):
	_ = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	d = {}
	for i in range(k):
		d[a[i]] = t[i]
	d = sorted(d.items())
	t_min = [0 for i in range(n)]
	j = 0
	for i in range(n):
		if j > 0:
			t_min[i] = t_min[i - 1] + 1
		if j < k and i == d[j][0] - 1:
			if j == 0:
				t_min[i] = d[j][1]
			else:
				t_min[i] = min(t_min[i], d[j][1])
			j += 1
	j = k - 1
	for i in range(n - 1, -1, -1):
		if j < k - 1:
			t_min[i] = min(t_min[i + 1] + 1, t_min[i])
		if j == -1:
			t_min[i] = t_min[i + 1] + 1
		if j >= 0 and i == d[j][0] - 1:
			t_min[i] = min(d[j][1], t_min[i])
			j -= 1
	out.append(t_min)
for i in range(q):
	print(*out[i])


import math
INF = 1e+20

def lerp(val, lb, ub, lv, uv):
	return lv + (val - lb) * (uv - lv) / (ub - lb)

def get_ints():
	return list(map(int, input().split()))

def get_floats():
	return list(map(float, input().split()))
for _ in range(int(input())):
	input()
	(N, K) = get_ints()
	a = get_ints()
	t = get_ints()
	c = [INF] * N
	for i in range(K):
		c[a[i] - 1] = t[i]
	p = INF
	L = [INF] * N
	for i in range(N):
		p = min(p + 1, c[i])
		L[i] = p
	p = INF
	R = [INF] * N
	for i in range(N - 1, -1, -1):
		p = min(p + 1, c[i])
		R[i] = p
	print(*[min(L[i], R[i]) for i in range(N)])


def task():
	input()
	(n, k) = [int(x) for x in input().split()]
	a = [int(x) - 1 for x in input().split()]
	t = [int(x) for x in input().split()]
	cond = []
	inf = float('inf')
	ans = [inf] * n
	for (i, temp) in zip(a, t):
		ans[i] = temp
	for i in range(1, n):
		ans[i] = min(ans[i - 1] + 1, ans[i])
	for i in range(n - 2, -1, -1):
		ans[i] = min(ans[i + 1] + 1, ans[i])
	return ' '.join([str(x) for x in ans])
T = int(input())
for _ in range(T):
	print(task())


import sys
import math
import bisect
import heapq
from bisect import bisect_right
from sys import stdin, stdout

def get_ints_in_variables():
	return map(int, sys.stdin.readline().strip().split())

def get_int():
	return int(sys.stdin.readline())

def get_ints_in_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_list_of_list(n):
	return [list(map(int, sys.stdin.readline().strip().split())) for _ in range(n)]

def get_string():
	return sys.stdin.readline().strip()

def myceil(x, y):
	return (x + y - 1) // y

def Solution(a, b, n, k):
	hm = {}
	for i in range(k):
		hm[a[i] - 1] = b[i]
	pref = [float('inf')]
	suffix = [float('inf') for _ in range(n + 1)]
	for i in range(n):
		if i in hm:
			pref.append(min(pref[-1] + 1, hm[i]))
		else:
			pref.append(pref[-1] + 1)
	pref.pop(0)
	for i in range(n - 1, -1, -1):
		if i in hm:
			suffix[i] = min(suffix[i + 1] + 1, hm[i])
		else:
			suffix[i] = suffix[i + 1] + 1
	suffix.pop()
	for i in range(n):
		print(min(pref[i], suffix[i]), end=' ')
	print()

def main():
	for _ in range(get_int()):
		input()
		(n, k) = get_ints_in_variables()
		a = get_ints_in_list()
		b = get_ints_in_list()
		Solution(a, b, n, k)
main()


q = int(input())
for i in range(q):
	xx = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	tot = []
	for j in range(n):
		tot.append(0)
	for j in range(k):
		tot[a[j] - 1] = t[j]
	ps = 2000000000
	(left, right) = ([], [])
	for j in range(n):
		ps = ps + 1
		if tot[j] != 0:
			ps = min(ps, tot[j])
		left.append(ps)
	ps = 2000000000
	for j in range(n - 1, -1, -1):
		ps = ps + 1
		if tot[j] != 0:
			ps = min(ps, tot[j])
		right.append(ps)
	right.reverse()
	for j in range(n):
		print(min(left[j], right[j]), end=' ')
	print()


def solve():
	input()
	(n, k) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	t = [int(i) for i in input().split()]
	dp = [10 ** 10 for i in range(n + 1)]
	for i in range(k):
		dp[a[i]] = t[i]
	for i in range(1, len(dp)):
		dp[i] = min(dp[i - 1] + 1, dp[i])
	for i in range(len(dp) - 2, 0, -1):
		dp[i] = min(dp[i + 1] + 1, dp[i])
	print(*dp[1:])
for _ in range(int(input())):
	solve()


for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	dist = list(map(int, input().split()))
	temp = list(map(int, input().split()))
	store = [None for _ in range(n)]
	for i in range(k):
		store[dist[i] - 1] = temp[i]
	left = [float('inf') for _ in range(n)]
	right = [float('inf') for _ in range(n)]
	for i in range(n):
		cur_temp = store[i] if store[i] else float('inf')
		if i > 0:
			left[i] = min(cur_temp, left[i - 1] + 1)
		elif i == 0:
			left[i] = cur_temp
	for i in range(n - 1, -1, -1):
		cur_temp = store[i] if store[i] else float('inf')
		if i == n - 1:
			right[i] = cur_temp
		elif i < n - 1:
			right[i] = min(right[i + 1] + 1, cur_temp)
	res = []
	for i in range(n):
		res.append(min(left[i], right[i]))
	print(*res)


import sys, heapq
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	input()
	(n, k) = [int(x) for x in input().split()]
	v = [int(x) for x in input().split()]
	d = {}
	temp = [int(x) for x in input().split()]
	for i in range(k):
		d[v[i]] = temp[i]
	h2 = []
	h1 = []
	for i in d:
		heapq.heappush(h2, (d[i] + i, i))
	res = []
	for i in range(1, n + 1):
		ans = 1000000000000000
		if i in d:
			heapq.heappush(h1, d[i] - i)
		while h2 and h2[0][1] < i:
			heapq.heappop(h2)
		if h1:
			ans = min(ans, h1[0] + i)
		if h2:
			ans = min(ans, h2[0][0] - i)
		res.append(ans)
	print(*res)


for _ in range(int(input())):
	e = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	left = [float('inf')] * (n + 2)
	right = [float('inf')] * (n + 2)
	for i in range(k):
		left[a[i]] = t[i]
		right[a[i]] = t[i]
	for i in range(1, n + 1):
		left[i] = min(left[i - 1] + 1, left[i])
	for i in range(n, 0, -1):
		right[i] = min(right[i + 1] + 1, right[i])
	out = []
	for i in range(1, n + 1):
		out.append(min(left[i], right[i]))
	print(*out)


import sys
import math
import collections
dy = [1, 0, -1, 0]
dx = [0, 1, 0, -1]
r = sys.stdin.readline
for _ in range(int(r())):
	r()
	dic = {}
	(N, K) = map(int, r().split())
	AL = list(map(int, r().split()))
	TL = list(map(int, r().split()))
	for i in range(K):
		dic[AL[i]] = TL[i]
	ans = [0] * (N + 1)
	nowtemp = -124421421442142241
	for i in range(1, N + 1):
		if i in dic:
			if dic[i] < nowtemp or nowtemp < 0:
				nowtemp = dic[i]
		if nowtemp > 0:
			if ans[i] == 0:
				ans[i] = nowtemp
			if ans[i] > nowtemp:
				ans[i] = nowtemp
		nowtemp += 1
	nowtemp = -1225125521512521251
	for i in range(N, 0, -1):
		if i in dic:
			if dic[i] < nowtemp or nowtemp < 0:
				nowtemp = dic[i]
		if nowtemp > 0:
			if ans[i] == 0:
				ans[i] = nowtemp
			if ans[i] > nowtemp:
				ans[i] = nowtemp
		nowtemp += 1
	print(*ans[1:])


q = int(input())
for _ in range(q):
	inp = input()
	inp = list(map(int, input().split()))
	n = inp[0]
	k = inp[1]
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	at = zip(a, t)
	at = sorted(at, key=lambda x: x[0])
	a = [x[0] for x in at]
	t = [x[1] for x in at]
	fltr = [0 for x in range(n)]
	frtl = [0 for x in range(n)]
	for i in range(k):
		a[i] -= 1
	(mint, mini) = (t[0], a[0])
	for i in range(k - 1):
		if mint - mini + a[i] > t[i]:
			(mint, mini) = (t[i], a[i])
		for j in range(a[i], a[i + 1]):
			fltr[j] = mint + j - mini
	if mint - mini + a[-1] > t[-1]:
		(mint, mini) = (t[-1], a[-1])
	for j in range(a[-1], n):
		fltr[j] = mint + j - mini
	(mint, mini) = (t[-1], a[-1])
	for i in range(k - 2, -1, -1):
		if mint + (mini - a[i + 1]) > t[i + 1]:
			(mint, mini) = (t[i + 1], a[i + 1])
		for j in range(a[i] + 1, a[i + 1] + 1):
			frtl[j] = mint + (mini - j)
	if mint + mini - a[0] > t[0]:
		(mint, mini) = (t[0], a[0])
	for j in range(a[0] + 1):
		frtl[j] = mint + (mini - j)
	for j in range(a[0] + 1):
		print(frtl[j], end=' ')
	for j in range(a[0] + 1, a[-1] + 1):
		print(min(fltr[j], frtl[j]), end=' ')
	for j in range(a[-1] + 1, n):
		print(fltr[j], end=' ')
	print()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	s = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	sol = [300000000000005] * n
	for (x, y) in zip(a, b):
		sol[x - 1] = y
	j = 0
	for i in range(1, n):
		sol[i] = min(sol[i - 1] + 1, sol[i])
	for i in range(n - 2, -1, -1):
		sol[i] = min(sol[i + 1] + 1, sol[i])
	print(*sol)


t = int(input())
for j in range(t):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	final = [10 ** 10 for i in range(n)]
	for i in range(k):
		final[a[i] - 1] = t[i]
	for i in range(1, n):
		final[i] = min(final[i - 1] + 1, final[i])
	for i in range(n - 2, -1, -1):
		final[i] = min(final[i], final[i + 1] + 1)
	print(*final)


for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	res = [float('inf')] * n
	for i in range(k):
		res[a[i] - 1] = t[i]
	for i in range(1, n):
		res[i] = min(res[i], res[i - 1] + 1)
	for i in range(n - 1, 0, -1):
		res[i - 1] = min(res[i - 1], 1 + res[i])
	print(*res)
num_inp = lambda : int(input())
arr_inp = lambda : list(map(int, input().split()))
sp_inp = lambda : map(int, input().split())
str_inp = lambda : input()


for _ in range(int(input())):
	tem = input()
	maxx = 10 ** 12
	(n, k) = map(int, input().split())
	ind = [int(x) - 1 for x in input().split()]
	temp = [int(x) for x in input().split()]
	a = [maxx] * n
	for i in range(k):
		a[ind[i]] = temp[i]
	for i in range(1, n):
		a[i] = min(a[i - 1] + 1, a[i])
	for i in range(n - 2, -1, -1):
		a[i] = min(a[i + 1] + 1, a[i])
	print(*a)


for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	temp = list(map(int, input().split()))
	inf = 10 ** 18
	c = [inf] * n
	for i in range(k):
		c[arr[i] - 1] = temp[i]
	p = inf
	L = [inf] * n
	for i in range(n):
		p = min(p + 1, c[i])
		L[i] = p
	R = [inf] * n
	p = inf
	for i in range(n - 1, -1, -1):
		p = min(p + 1, c[i])
		R[i] = p
	for i in range(n):
		print(min(L[i], R[i]), end=' ')
	print()


from sys import stdin, stdout
from math import gcd, sqrt, factorial, pi, inf
from collections import deque, defaultdict
from bisect import bisect, bisect_left
from time import time
from itertools import permutations as per
from heapq import heapify, heappush, heappop, heappushpop
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\r\n')
L = lambda : list(R())
P = lambda x: stdout.write(str(x) + '\n')
lcm = lambda x, y: x * y // gcd(x, y)
nCr = lambda x, y: f[x] * inv(f[y] * f[x - y] % N) % N
inv = lambda x: pow(x, N - 2, N)
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for i in range(I()):
	S()
	(n, k) = R()
	a = L()
	t = L()
	v = [0] * n
	for i in range(k):
		v[a[i] - 1] = t[i]
	m = []
	mn = inf
	ind = inf
	for i in range(n):
		if v[i] and v[i] < mn + abs(ind - i):
			mn = v[i]
			ind = i
		m += ((mn, ind),)
	mn = inf
	ind = inf
	ans = [0] * n
	for i in range(n - 1, -1, -1):
		if v[i] and v[i] < mn + abs(ind - i):
			mn = v[i]
			ind = i
		ans[i] = min(mn + abs(ind - i), m[i][0] + abs(m[i][1] - i))
	print(*ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict
from math import ceil, floor, sqrt, log2, gcd
from heapq import heappush, heappop
from bisect import bisect_left, bisect
import sys
abc = 'abcdefghijklmnopqrstuvwxyz'
for _ in range(int(input())):
	blah = input()
	(n, k) = map(int, input().split())
	pos = list(map(int, input().split()))
	temp = list(map(int, input().split()))
	p = [-1] * n
	for i in range(k):
		p[pos[i] - 1] = temp[i]
	ans = [float('inf')] * n
	curr = float('inf')
	for i in range(n):
		if p[i] != -1:
			curr = min(p[i], curr)
		ans[i] = min(ans[i], curr)
		curr += 1
	curr = float('inf')
	for i in range(n - 1, -1, -1):
		if p[i] != -1:
			curr = min(p[i], curr)
		ans[i] = min(ans[i], curr)
		curr += 1
	print(*ans)


from math import gcd, floor, inf, log2, sqrt, log10
from bisect import bisect_right, bisect_left
from statistics import mode
cases = int(input())
for _ in range(cases):
	input()
	(n, k) = map(int, input().split())
	air_con = list(map(int, input().split()))
	temper = list(map(int, input().split()))
	st = {cell: temp for (cell, temp) in zip(air_con, temper)}
	ans1 = []
	best = []
	for cell in range(1, n + 1):
		if cell in st:
			if not best:
				best = [cell, st[cell]]
			elif cell - best[0] + best[1] > st[cell]:
				best = [cell, st[cell]]
		if not best:
			ans1.append(inf)
		else:
			ans1.append(cell - best[0] + best[1])
	ans2 = []
	best = []
	for cell in range(n, 0, -1):
		if cell in st:
			if not best:
				best = [cell, st[cell]]
			elif best[0] - cell + best[1] > st[cell]:
				best = [cell, st[cell]]
		if not best:
			ans2.append(inf)
		else:
			ans2.append(best[0] - cell + best[1])
	ans2 = ans2[::-1]
	final_ans = []
	for (i, j) in zip(ans1, ans2):
		final_ans.append(min(i, j))
	print(*final_ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import Counter, defaultdict
import math
for _ in range(int(input())):
	s = input()
	(n, k) = map(int, input().split())
	p = list(map(int, input().split()))
	t = list(map(int, input().split()))
	a = [10 ** 12] * n
	vis = [-1] * n
	for i in range(k):
		vis[p[i] - 1] = t[i]
	pos = -n - 100
	temp = 10 ** 12
	for i in range(n):
		if vis[i] != -1 and temp + (i - pos) > vis[i]:
			pos = i
			temp = vis[i]
		if temp != 10 ** 12:
			a[i] = min(a[i], temp + (i - pos))
	pos = 10 ** 6
	temp = 10 ** 12
	for i in range(n - 1, -1, -1):
		if vis[i] != -1 and temp + (pos - i) > vis[i]:
			pos = i
			temp = vis[i]
		if temp != 10 ** 12:
			a[i] = min(a[i], temp + (pos - i))
	print(*a)


cases = int(input())
for c in range(cases):
	input()
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	data = []
	for i in range(k):
		data.append([a[i], t[i]])
	data.sort(key=lambda pair: pair[0])
	left = [0] * n
	li = 0
	right = [0] * n
	ri = k - 1
	lt = 1100000000
	rt = 1100000000
	for i in range(n):
		lt += 1
		if li < k and data[li][0] - 1 == i:
			lt = min(lt, data[li][1])
			li += 1
		left[i] = lt
		rt += 1
		if ri >= 0 and data[ri][0] - 1 == n - 1 - i:
			rt = min(rt, data[ri][1])
			ri -= 1
		right[n - 1 - i] = rt
	for i in range(n):
		print(min(left[i], right[i]), end=' ')
	print()


import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = [(a[i], b[i]) for i in range(k)]
	c.sort()
	a = [c[i][0] for i in range(k)]
	b = [c[i][1] for i in range(k)]
	data = []
	data2 = []
	h = {}
	for i in range(k):
		h[a[i]] = 1
		data.append(a[i] + b[i])
		data2.append(-a[i] + b[i])
	for i in range(k - 2, -1, -1):
		data[i] = min(data[i + 1], data[i])
	for i in range(1, k):
		data2[i] = min(data2[i - 1], data2[i])
	answer = [-1] * n
	cnt = 0
	for i in range(n):
		if cnt == 0:
			temp = -(i + 1) + data[cnt]
		elif cnt == k:
			temp = i + 1 + data2[cnt - 1]
		else:
			temp = min(-(i + 1) + data[cnt], i + 1 + data2[cnt - 1])
		answer[i] = temp
		if i + 1 in h:
			cnt += 1
	print(' '.join(map(str, answer)))


import sys
t = int(input())
for tt in range(t):
	sys.stdin.readline()
	(n, k) = map(int, sys.stdin.readline().split())
	dpl = [0 for _ in range(n + 2)]
	dpr = [0 for _ in range(n + 2)]
	w = [0 for _ in range(n + 1)]
	q = list(map(int, sys.stdin.readline().split()))
	f = list(map(int, sys.stdin.readline().split()))
	for i in range(len(q)):
		w[q[i]] = f[i]
	for i in range(1, n + 1):
		if dpl[i - 1]:
			dpl[i] = dpl[i - 1] + 1
			if w[i]:
				dpl[i] = min(dpl[i], w[i])
		elif w[i]:
			dpl[i] = w[i]
	for i in range(n, 0, -1):
		if dpr[i + 1]:
			dpr[i] = dpr[i + 1] + 1
			if w[i]:
				dpr[i] = min(dpr[i], w[i])
		elif w[i]:
			dpr[i] = w[i]
	dp = []
	for i in range(1, n + 1):
		an = 0
		if dpl[i]:
			an = dpl[i]
			if dpr[i]:
				an = min(an, dpr[i])
		else:
			an = dpr[i]
		dp.append(an)
	print(' '.join(map(str, dp)))


def main():
	for _ in range(int(input())):
		input()
		parameters = list(map(int, input().strip().split()))
		conditioners = dict(zip(list(map(lambda x: x - 1, map(int, input().strip().split()))), list(map(int, input().strip().split()))))
		lower_minima = [0] * parameters[0]
		upper_minima = [0] * parameters[0]
		temp = float('inf')
		for index in range(parameters[0]):
			temp = min(temp + 1, conditioners.get(index, float('inf')))
			lower_minima[index] = temp
		temp = float('inf')
		for index in reversed(range(parameters[0])):
			temp = min(temp + 1, conditioners.get(index, float('inf')))
			upper_minima[index] = temp
		for index in range(parameters[0]):
			print(min(lower_minima[index], upper_minima[index]), end=' ')
		print()
main()


for _ in range(int(input())):
	st = input()
	(n, m) = list(map(int, input().split()))
	arr1 = list(map(int, input().split()))
	arr2 = list(map(int, input().split()))
	(arr1, arr2) = zip(*sorted(zip(arr1, arr2)))
	x = 0
	temp1 = []
	temp2 = []
	temp3 = []
	neer = float('inf')
	for i in range(n):
		if x < m and i + 1 == arr1[x]:
			neer = min(neer, arr2[x])
			x += 1
		temp1.append(neer)
		neer += 1
	neer = float('inf')
	x = m - 1
	for i in range(n - 1, -1, -1):
		if x >= 0 and i + 1 == arr1[x]:
			ind = arr2[x]
			neer = min(neer, arr2[x])
			x -= 1
		temp2.append(neer)
		neer += 1
	temp2.reverse()
	for j in range(n):
		temp3.append(min(temp1[j], temp2[j]))
	print(*temp3)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for t in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	l3 = []
	l4 = []
	l5 = [0] * n
	a = 10 ** 10
	b = a
	for i in range(k):
		l5[l1[i] - 1] = l2[i]
	for i in range(n):
		if l5[i]:
			a = min(a, l5[i] + n - i)
		l3.append(a)
	for i in range(n - 1, -1, -1):
		if l5[i]:
			b = min(b, l5[i] + i)
		l4.append(b)
	l4.reverse()
	ans = [0] * n
	for i in range(n):
		a = l3[i] - (n - i)
		b = l4[i] - i
		ans[i] = min(a, b)
	print(*ans)


from bisect import bisect_left
from collections import defaultdict as D
MAX = 99999999999
for _ in range(int(input())):
	s = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	d = D(int)
	for i in range(k):
		a[i] -= 1
		d[a[i]] = t[i]
	ans = [MAX for i in range(n)]
	r = MAX
	for i in range(n):
		if d[i] == 0:
			d[i] = MAX
		r = min(r, d[i])
		ans[i] = min(ans[i], r)
		r += 1
	for i in range(n - 1, -1, -1):
		if d[i] == 0:
			d[i] = MAX
		r = min(r, d[i])
		ans[i] = min(ans[i], r)
		r += 1
	print(*ans)


for _ in range(int(input())):
	line = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	c = [2000000000.0] * (n + 1)
	for i in range(k):
		c[a[i] - 1] = t[i]
	p = 2000000000.0
	L = [0] * (n + 1)
	for i in range(n):
		p = min(p + 1, c[i])
		L[i] = p
	p = 2000000000.0
	R = [0] * (n + 1)
	for i in range(n - 1, -1, -1):
		p = min(p + 1, c[i])
		R[i] = p
	for i in range(n):
		print(min(L[i], R[i]), end=' ')
	print()


from collections import deque
import heapq
import sys
input = sys.stdin.readline
q = int(input())
inf = 1145141919810
for _ in range(q):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	h = []
	for i in range(k):
		heapq.heappush(h, (t[i], a[i] - 1))
	ans = [inf] * n
	q = deque()
	(t0, a0) = heapq.heappop(h)
	ans[a0] = t0
	q.append((t0, a0))
	while q:
		(t0, a0) = q.popleft()
		for i in [-1, 1]:
			a1 = a0 + i
			if 0 <= a1 < n:
				if ans[a1] == inf:
					ans[a1] = t0 + 1
					while h and h[0][0] == t0:
						(t2, a2) = heapq.heappop(h)
						if t2 < ans[a2]:
							ans[a2] = t2
							q.append((t2, a2))
					q.append((t0 + 1, a1))
	print(*ans)


import math
import sys
inp = int(input())
for _ in range(inp):
	input()
	temp = float('inf')
	(n, k) = map(int, input().split(' '))
	a = list(map(int, input().split(' ')))
	t = list(map(int, input().split(' ')))
	c = [temp] * n
	for i in range(k):
		c[a[i] - 1] = t[i]
	L = [temp] * n
	R = [temp] * n
	p = temp * n
	for i in range(n):
		p = min(p + 1, c[i])
		L[i] = p
	p = temp
	res = [0] * n
	for i in range(n - 1, -1, -1):
		p = min(p + 1, c[i])
		res[i] = min(L[i], p)
	print(*res)


for i in range(int(input())):
	x = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	s = set()
	for i in a:
		s.add(i - 1)
	d = {}
	for i in range(k):
		d[a[i]] = t[i]
	ans = [0] * n
	l = [0] * n
	r = [0] * n
	p = 10 ** 10
	for i in range(n):
		if i in s:
			p = min(d[i + 1], p + 1)
		else:
			p += 1
		l[i] = p
	p = 10 ** 10
	for i in range(n - 1, -1, -1):
		if i in s:
			p = min(d[i + 1], p + 1)
		else:
			p += 1
		r[i] = p
	for i in range(n):
		ans[i] = min(l[i], r[i])
	print(*ans)


from math import *
from collections import *
from functools import *
from bisect import *
from itertools import *
from heapq import *
from statistics import *
inf = float('inf')
ninf = -float('inf')
ip = input
alphal = 'abcdefghijklmnopqrstuvwxyz'
alphau = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def ipl():
	return list(map(int, ip().split()))

def ipn():
	return int(ip())

def ipf():
	return float(ip())

def solve():
	(n, k) = ipl()
	(a, t) = (ipl(), ipl())
	(c, l, r) = ([inf] * n, [inf] * n, [inf] * n)
	for i in range(k):
		c[a[i] - 1] = t[i]
	p = inf
	for i in range(n):
		p = min(p + 1, c[i])
		l[i] = p
	p = inf
	for i in range(n - 1, -1, -1):
		p = min(p + 1, c[i])
		r[i] = p
	for i in range(n):
		print(min(l[i], r[i]), end=' ')
t = ipn()
for _ in range(t):
	ip()
	solve()


import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def main(t):
	s = input()
	(n, k) = map(int, input().split())
	left = [-1] * n
	right = [-1] * n
	ans = [-1] * n
	airloc = list(map(int, input().split()))
	airtemp = list(map(int, input().split()))
	for i in range(k):
		left[airloc[i] - 1] = airtemp[i]
		right[airloc[i] - 1] = airtemp[i]
	curr = 2147483647
	for x in range(n):
		if left[x] >= 0:
			curr = min(curr + 1, left[x])
		else:
			curr += 1
		left[x] = curr
	for x in range(n - 1, -1, -1):
		if right[x] >= 0:
			curr = min(curr + 1, right[x])
		else:
			curr += 1
		right[x] = curr
	for x in range(n):
		ans[x] = min(left[x], right[x])
	print(' '.join(map(str, ans)))
T = int(input())
t = 1
while t <= T:
	main(t)
	t += 1


t = int(input())
for i in range(t):
	input()
	(n, ac) = map(int, input().split())
	acp = [int(j) for j in input().split()]
	acv = [int(j) for j in input().split()]
	L = [10 ** 10 for j in range(n)]
	P = [10 ** 10 for j in range(n)]
	for j in range(ac):
		L[acp[j] - 1] = acv[j]
		P[acp[j] - 1] = acv[j]
	p = 10 ** 10
	for j in range(n):
		p = min(L[j], p + 1)
		L[j] = p
	p = 10 ** 10
	for j in range(n - 1, -1, -1):
		p = min(P[j], p + 1)
		P[j] = p
	res = [0 for j in range(n)]
	for j in range(n):
		res[j] = min(L[j], P[j])
	print(*res)


from __future__ import division, print_function
import math
import sys
import os
from io import BytesIO, IOBase
from collections import deque, Counter, OrderedDict, defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input().strip()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
for _ in range(inp()):
	inlt()
	(n, k) = invr()
	a = inlt()
	t = inlt()
	z = [[a[i] - 1, t[i]] for i in range(k)]
	z.sort()
	v = [0 for i in range(n)]
	for i in range(1, len(t)):
		z[i][1] = min(z[i][1], z[i - 1][1] + abs(z[i - 1][0] - z[i][0]))
	for i in range(len(t) - 2, -1, -1):
		z[i][1] = min(z[i][1], z[i + 1][1] + abs(z[i + 1][0] - z[i][0]))
	j = 0
	fst = 0
	sec = 1
	while j < n:
		v1 = z[fst][1] + abs(z[fst][0] - j)
		if sec < len(t):
			v2 = z[sec][1] + abs(z[sec][0] - j)
		else:
			v2 = sys.maxsize
		if v1 < v2:
			v[j] = v1
		else:
			v[j] = v2
			fst = sec
			sec += 1
		j += 1
	print(*v)


def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
for a0 in range(int(input())):
	_ = input()
	(n, k) = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	t = [int(x) for x in input().split()]
	tmp = dict()
	for i in range(k):
		tmp[a[i]] = t[i]
	a.sort()
	t = [tmp[a[-1]] + n for i in range(n + 1)]
	mi = a[0]
	cu = -1
	for i in range(1, n + 1):
		if cu < k - 1 and i == a[cu + 1]:
			if tmp[mi] + abs(mi - i) > tmp[i]:
				mi = a[cu + 1]
			cu += 1
		t[i] = min(t[i], abs(i - mi) + tmp[mi])
	mi = a[-1]
	cu = k
	for i in range(n, 0, -1):
		if cu > 0 and i == a[cu - 1]:
			if tmp[mi] + abs(mi - i) > tmp[i]:
				mi = a[cu - 1]
			cu -= 1
		t[i] = min(t[i], abs(i - mi) + tmp[mi])
	for i in range(1, n + 1):
		print(t[i], end=' ')
	print()


import sys
import math
from typing import DefaultDict
mod = 10 ** 9 + 7
LI = lambda : [int(k) for k in input().split()]
input = lambda : sys.stdin.readline().rstrip()
IN = lambda : int(input())
S = lambda : input()
r = range
for tt in r(IN()):
	ll = S()
	(n, k) = LI()
	a = LI()
	t = LI()
	d = DefaultDict(int)
	for i in range(k):
		d[a[i] - 1] = t[i]
	a.sort()
	ans = [10 ** 10] * n
	temp = 10 ** 10
	for i in range(a[0] - 1, n):
		if d[i] > 0:
			temp = min(temp, d[i])
		ans[i] = temp
		temp += 1
	temp = 10 ** 10
	for i in range(a[-1] - 1, -1, -1):
		if d[i] > 0:
			temp = min(temp, d[i])
		ans[i] = min(ans[i], temp)
		temp += 1
	print(*ans)


INT_MAX = 2147483647
for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	b = [2147483647] * n
	p = 2147483647
	l = [INT_MAX] * n
	r = [INT_MAX] * n
	for i in range(k):
		b[a[i] - 1] = t[i]
	for i in range(n):
		p = min(p + 1, b[i])
		l[i] = p
	p = 2147483647
	for i in reversed(range(n)):
		p = min(p + 1, b[i])
		r[i] = p
	for i in range(n):
		b[i] = min(l[i], r[i])
	print(*b)


for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	ind = list(map(int, input().split()))
	value = list(map(int, input().split()))
	right = [10 ** 10] * n
	left = [10 ** 10] * n
	for i in range(k):
		right[ind[i] - 1] = value[i]
		left[ind[i] - 1] = value[i]
	l = 10 ** 10
	for i in range(n):
		p = min(l, right[i])
		right[i] = p
		l = p + 1
	l = 10 ** 10
	for i in range(n - 1, -1, -1):
		p = min(l, left[i])
		left[i] = p
		l = p + 1
	res = []
	for i in range(n):
		res.append(min(right[i], left[i]))
	print(*res)


import sys
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		S = input().strip()
		(N, K) = [int(x) for x in input().split()]
		A = [int(x) - 1 for x in input().split()]
		T = [int(x) for x in input().split()]
		dic = {}
		a = set(A)
		for i in range(K):
			dic[A[i]] = i
		ans = [10 ** 10] * N
		current = 10 ** 10
		for i in range(N):
			current += 1
			if i in a:
				current = min(current, T[dic[i]])
			ans[i] = current
		current = 10 ** 10
		for i in range(N - 1, -1, -1):
			current += 1
			if i in a:
				current = min(current, T[dic[i]])
			ans[i] = min(ans[i], current)
		print(*ans)
main()


import math
for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	ac = list(map(int, input().split()))
	temp = list(map(int, input().split()))
	ac = {ac[i]: temp[i] for i in range(k)}
	prefix = [math.inf for i in range(n)]
	suffix = [math.inf for i in range(n)]
	if 1 in ac:
		prefix[0] = ac[1]
	if n in ac:
		suffix[-1] = ac[n]
	for i in range(1, n):
		prefix[i] = prefix[i - 1] + 1
		if i + 1 in ac:
			prefix[i] = min(prefix[i], ac[i + 1])
	for i in range(n - 2, -1, -1):
		suffix[i] = suffix[i + 1] + 1
		if i + 1 in ac:
			suffix[i] = min(suffix[i], ac[i + 1])
	ans = []
	for i in range(n):
		ans.append(min(prefix[i], suffix[i]))
	print(*ans)


for _ in range(int(input())):
	input()
	(n, k) = [int(i) for i in input().split()]
	arr = [int(i) for i in input().split()]
	temp = [int(i) for i in input().split()]
	map = zip(arr, temp)
	ans1 = [float('inf') for i in range(n)]
	ans2 = [float('inf') for i in range(n)]
	for i in range(k):
		ans1[arr[i] - 1] = temp[i]
		ans2[arr[i] - 1] = temp[i]
	start = -1
	for i in range(n):
		if ans1[i] == float('inf'):
			if start != -1:
				ans1[i] = ans1[start] + (i - start)
		elif start != -1:
			if ans1[start] + abs(i - start) > ans1[i]:
				start = i
			else:
				ans1[i] = ans1[start] + abs(start - i)
		else:
			start = i
	start = -1
	for i in range(n - 1, -1, -1):
		if ans2[i] == float('inf'):
			if start != -1:
				ans2[i] = ans2[start] + (start - i)
		elif start != -1:
			if ans2[start] + abs(i - start) > ans2[i]:
				start = i
			else:
				ans2[i] = ans2[start] + abs(start - i)
		else:
			start = i
	ans = [min(ans1[i], ans2[i]) for i in range(n)]
	print(*ans)


INF = 1 << 42

def solve():
	A = [INF] * (N + 1)
	for (pos, t) in zip(P, B):
		A[pos] = t
	temp = [INF] * (N + 1)
	for i in range(1, N + 1):
		temp[i] = min(temp[i - 1] + 1, A[i])
	for i in range(N - 1, 0, -1):
		temp[i] = min(temp[i], temp[i + 1] + 1, A[i])
	return temp[1:]
test_cases = int(input())
for test_case in range(test_cases):
	input()
	(N, K) = map(int, input().split())
	P = list(map(int, input().split()))
	B = list(map(int, input().split()))
	print(*solve())


def ans(n, k, a, t):
	d = {}
	for i in range(len(a)):
		d[a[i] - 1] = t[i]
	z = 10 ** 10
	L = [z for i in range(n)]
	R = [z for i in range(n)]
	x = min(a) - 1
	y = max(a) - 1
	for i in range(x, n):
		if i in d:
			z = min(d[i], z + 1)
		else:
			z = z + 1
		L[i] = min(L[i], z)
	for i in range(y, -1, -1):
		if i in d:
			z = min(d[i], z + 1)
		else:
			z = z + 1
		R[i] = min(R[i], z)
	for i in range(n):
		x = min(L[i], R[i])
		print(x, end=' ')
	print()
	return
n = int(input())
for i in range(n):
	x = input()
	d = input().split()
	m = int(d[0])
	k = int(d[1])
	b = input().split()
	a = []
	for i in b:
		a.append(int(i))
	b = input().split()
	t = []
	for i in b:
		t.append(int(i))
	ans(m, k, a, t)


def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().split())

def msi():
	return map(str, input().split())

def li():
	return list(mi())
t = ii()
for _ in range(t):
	s = si()
	(n, k) = mi()
	a = li()
	temp = li()
	ac = []
	for i in range(k):
		ac.append([a[i], temp[i]])
	ac.sort()
	btemp = temp.copy()
	(ldp, rdp) = ([0] * n, [0] * n)
	dp = [0] * n
	b = ac.copy()
	ind = 0
	bind = k - 1
	mini = 10 ** 11
	maxi = 10 ** 11
	for i in range(n):
		if ind < k:
			if i + 1 == ac[ind][0]:
				if ac[ind][1] - ac[ind][0] < mini:
					mini = ac[ind][1] - ac[ind][0]
				ind += 1
				ldp[i] = mini + i + 1
			else:
				ldp[i] = mini + i + 1
		else:
			ldp[i] = mini + i + 1
	for i in range(n - 1, -1, -1):
		if bind > -1:
			if i + 1 == b[bind][0]:
				if b[bind][1] + b[bind][0] < maxi:
					maxi = b[bind][1] + b[bind][0]
				bind -= 1
				rdp[i] = maxi - i - 1
			else:
				rdp[i] = maxi - i - 1
		else:
			rdp[i] = maxi - i - 1
	for i in range(n):
		dp[i] = min(ldp[i], rdp[i])
	print(*dp)


inf = float('inf')
for i in range(int(input())):
	e = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	d = list(map(int, input().split()))
	t = {}
	for j in range(k):
		t[a[j] - 1] = d[j]
	a = set(a)
	(left, right) = ([inf for j in range(n)], [inf for j in range(n)])
	if 1 in a:
		left[0] = t[0]
	for j in range(1, n):
		ct = inf
		if j + 1 in a:
			ct = t[j]
		left[j] = min(left[j - 1] + 1, ct)
	if n in a:
		right[-1] = t[n - 1]
	for j in range(n - 2, -1, -1):
		ct = inf
		if j + 1 in a:
			ct = t[j]
		right[j] = min(right[j + 1] + 1, ct)
	for j in range(n):
		print(min(left[j], right[j]), end=' ')
	print()


import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
threading.stack_size(10 ** 8)
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
sys.setrecursionlimit(300000)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default=2 ** 51, func=lambda a, b: a & b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=2 * 10 ** 9, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater

class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.isEndOfWord = False

class Trie:

	def __init__(self):
		self.root = self.getNode()

	def getNode(self):
		return TrieNode()

	def _charToIndex(self, ch):
		return ord(ch) - ord('a')

	def insert(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				pCrawl.children[index] = self.getNode()
			pCrawl = pCrawl.children[index]
		pCrawl.isEndOfWord = True

	def search(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				return False
			pCrawl = pCrawl.children[index]
		return pCrawl != None and pCrawl.isEndOfWord

class Node:

	def __init__(self, data):
		self.data = data
		self.count = 0
		self.left = None
		self.right = None

class BinaryTrie:

	def __init__(self):
		self.root = Node(0)

	def insert(self, pre_xor):
		self.temp = self.root
		for i in range(31, -1, -1):
			val = pre_xor & 1 << i
			if val:
				if not self.temp.right:
					self.temp.right = Node(0)
				self.temp = self.temp.right
				self.temp.count += 1
			if not val:
				if not self.temp.left:
					self.temp.left = Node(0)
				self.temp = self.temp.left
				self.temp.count += 1
		self.temp.data = pre_xor

	def query(self, xor):
		self.temp = self.root
		for i in range(31, -1, -1):
			val = xor & 1 << i
			if not val:
				if self.temp.left and self.temp.left.count > 0:
					self.temp = self.temp.left
				elif self.temp.right:
					self.temp = self.temp.right
			elif self.temp.right and self.temp.right.count > 0:
				self.temp = self.temp.right
			elif self.temp.left:
				self.temp = self.temp.left
			self.temp.count -= 1
		return xor ^ self.temp.data
for ik in range(int(input())):
	line = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	a = [(a[i], t[i]) for i in range(k)]
	a.sort()
	diff = [0] * k
	add = [0] * k
	for i in range(k):
		diff[i] = a[i][1] - a[i][0]
		add[i] = a[i][1] + a[i][0]
	s = SegmentTree(diff)
	s1 = SegmentTree(add)
	ans = [0] * n
	j = 0
	for i in range(n):
		while j < k:
			if a[j][0] < i + 1:
				j += 1
			else:
				break
		ans[i] = min(s.query(0, j - 1) + i + 1, s1.query(j, k - 1) - i - 1)
	print(*ans)


from functools import lru_cache
ncases = int(input())
q = []
while ncases:
	_ = input()
	(n, k) = list(map(int, input().split()))
	pos = list(map(int, input().split()))
	temp = list(map(int, input().split()))
	res = [float('inf')] * n
	for i in range(k):
		res[pos[i] - 1] = temp[i]
	for i in range(1, n):
		res[i] = min(res[i], res[i - 1] + 1)
	for i in range(n - 1, 0, -1):
		res[i - 1] = min(res[i - 1], 1 + res[i])
	print(*res)
	ncases -= 1


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	sp = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = [float('inf')] * n
	for i in range(k):
		ans[a[i] - 1] = b[i]
	for i in range(n - 1):
		ans[i + 1] = min(ans[i] + 1, ans[i + 1])
	for i in range(n - 1, 0, -1):
		ans[i - 1] = min(ans[i - 1], ans[i] + 1)
	print(*ans)


import os
import sys
import math
from io import BytesIO, IOBase
import io
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
from collections import Counter
import threading
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: max(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree1:

	def __init__(self, data, default=10 ** 6, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
MOD = 10 ** 9 + 7

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
mod = 10 ** 9 + 7
omod = 998244353
prime = [True for i in range(10001)]
prime[0] = prime[1] = False

def SieveOfEratosthenes(n=10000):
	p = 2
	c = 0
	while p <= n:
		if prime[p] == True:
			c += 1
			for i in range(p, n + 1, p):
				prime[i] = False
		p += 1

class DSU:

	def __init__(self, R, C):
		self.par = range(R * C + 1)
		self.rnk = [0] * (R * C + 1)
		self.sz = [1] * (R * C + 1)

	def find(self, x):
		if self.par[x] != x:
			self.par[x] = self.find(self.par[x])
		return self.par[x]

	def union(self, x, y):
		(xr, yr) = (self.find(x), self.find(y))
		if xr == yr:
			return
		if self.rnk[xr] < self.rnk[yr]:
			(xr, yr) = (yr, xr)
		if self.rnk[xr] == self.rnk[yr]:
			self.rnk[xr] += 1
		self.par[yr] = xr
		self.sz[xr] += self.sz[yr]

	def size(self, x):
		return self.sz[self.find(x)]

	def top(self):
		return self.size(len(self.sz) - 1) - 1

class LazySegTree:

	def __init__(self, _op, _e, _mapping, _composition, _id, v):

		def set(p, x):
			assert 0 <= p < _n
			p += _size
			for i in range(_log, 0, -1):
				_push(p >> i)
			_d[p] = x
			for i in range(1, _log + 1):
				_update(p >> i)

		def get(p):
			assert 0 <= p < _n
			p += _size
			for i in range(_log, 0, -1):
				_push(p >> i)
			return _d[p]

		def prod(l, r):
			assert 0 <= l <= r <= _n
			if l == r:
				return _e
			l += _size
			r += _size
			for i in range(_log, 0, -1):
				if l >> i << i != l:
					_push(l >> i)
				if r >> i << i != r:
					_push(r >> i)
			sml = _e
			smr = _e
			while l < r:
				if l & 1:
					sml = _op(sml, _d[l])
					l += 1
				if r & 1:
					r -= 1
					smr = _op(_d[r], smr)
				l >>= 1
				r >>= 1
			return _op(sml, smr)

		def apply(l, r, f):
			assert 0 <= l <= r <= _n
			if l == r:
				return
			l += _size
			r += _size
			for i in range(_log, 0, -1):
				if l >> i << i != l:
					_push(l >> i)
				if r >> i << i != r:
					_push(r - 1 >> i)
			l2 = l
			r2 = r
			while l < r:
				if l & 1:
					_all_apply(l, f)
					l += 1
				if r & 1:
					r -= 1
					_all_apply(r, f)
				l >>= 1
				r >>= 1
			l = l2
			r = r2
			for i in range(1, _log + 1):
				if l >> i << i != l:
					_update(l >> i)
				if r >> i << i != r:
					_update(r - 1 >> i)

		def _update(k):
			_d[k] = _op(_d[2 * k], _d[2 * k + 1])

		def _all_apply(k, f):
			_d[k] = _mapping(f, _d[k])
			if k < _size:
				_lz[k] = _composition(f, _lz[k])

		def _push(k):
			_all_apply(2 * k, _lz[k])
			_all_apply(2 * k + 1, _lz[k])
			_lz[k] = _id
		_n = len(v)
		_log = _n.bit_length()
		_size = 1 << _log
		_d = [_e] * (2 * _size)
		_lz = [_id] * _size
		for i in range(_n):
			_d[_size + i] = v[i]
		for i in range(_size - 1, 0, -1):
			_update(i)
		self.set = set
		self.get = get
		self.prod = prod
		self.apply = apply
MIL = 1 << 20

def makeNode(total, count):
	return total * MIL + count

def getTotal(node):
	return math.floor(node / MIL)

def getCount(node):
	return node - getTotal(node) * MIL
nodeIdentity = makeNode(0.0, 0.0)

def nodeOp(node1, node2):
	return node1 + node2
	return makeNode(getTotal(node1) + getTotal(node2), getCount(node1) + getCount(node2))
identityMapping = -1

def mapping(tag, node):
	if tag == identityMapping:
		return node
	count = getCount(node)
	return makeNode(tag * count, count)

def composition(mapping1, mapping2):
	return mapping1 if mapping1 != identityMapping else mapping2

def memodict(f):

	class memodict(dict):

		def __missing__(self, key):
			ret = self[key] = f(key)
			return ret
	return memodict().__getitem__

def pollard_rho(n):
	if n & 1 == 0:
		return 2
	if n % 3 == 0:
		return 3
	s = (n - 1 & 1 - n).bit_length() - 1
	d = n >> s
	for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022]:
		p = pow(a, d, n)
		if p == 1 or p == n - 1 or a % n == 0:
			continue
		for _ in range(s):
			prev = p
			p = p * p % n
			if p == 1:
				return math.gcd(prev - 1, n)
			if p == n - 1:
				break
		else:
			for i in range(2, n):
				(x, y) = (i, (i * i + 1) % n)
				f = math.gcd(abs(x - y), n)
				while f == 1:
					(x, y) = ((x * x + 1) % n, (y * y + 1) % n)
					y = (y * y + 1) % n
					f = math.gcd(abs(x - y), n)
				if f != n:
					return f
	return n

@memodict
def prime_factors(n):
	if n <= 1:
		return Counter()
	f = pollard_rho(n)
	return Counter([n]) if f == n else prime_factors(f) + prime_factors(n // f)

def distinct_factors(n):
	factors = [1]
	for (p, exp) in prime_factors(n).items():
		factors += [p ** i * factor for factor in factors for i in range(1, exp + 1)]
	return factors

def all_factors(n):
	(small, large) = ([], [])
	for i in range(1, int(n ** 0.5) + 1, 2 if n & 1 else 1):
		if not n % i:
			small.append(i)
			large.append(n // i)
	if small[-1] == large[-1]:
		large.pop()
	large.reverse()
	small.extend(large)
	return small

def binarySearch(arr, n, key):
	left = 0
	right = n - 1
	mid = 0
	res = arr[n - 1]
	while left <= right:
		mid = (right + left) // 2
		if arr[mid] >= key:
			res = arr[mid]
			right = mid - 1
		else:
			left = mid + 1
	return res

def binarySearch1(arr, n, key):
	left = 0
	right = n - 1
	mid = 0
	res = -1
	while left <= right:
		mid = (right + left) // 2
		if arr[mid][0] >= key:
			right = mid - 1
		else:
			res = mid
			left = mid + 1
	return res
from heapq import heapify, heappop, heappush
t = 1
t = int(input())
for _ in range(t):
	x = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	tp = list(map(int, input().split()))
	b = [[a[i], tp[i]] for i in range(k)]
	b.sort()
	h = []
	heapify(h)
	res = [2 * 10 ** 9] * n
	j = 0
	for i in range(n):
		if j < k and b[j][0] == i + 1:
			heappush(h, b[j][1] + n - i)
			j += 1
		if not h:
			continue
		res[i] = min(res[i], h[0] - n + i)
	h = []
	heapify(h)
	j = k - 1
	for i in range(n - 1, -1, -1):
		if j >= 0 and b[j][0] == i + 1:
			heappush(h, b[j][1] + i)
			j -= 1
		if not h:
			continue
		res[i] = min(res[i], h[0] - i)
	print(*res)


import sys
from math import cos, pi, ceil, pi, tan, floor, gcd, sqrt, log2, factorial, log10
from collections import Counter
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase
from statistics import mean, median, mode
alpha = ascii_lowercase
beta = ascii_uppercase

def nextPowerOf2(n):
	p = 1
	if n and (not n & n - 1):
		return n
	while p < n:
		p <<= 1
	return p

def get_Divisors(n):
	arr = []
	i = 1
	while i <= sqrt(n):
		if n % i == 0:
			if n / i == i:
				arr.append(i)
			else:
				arr.append(i)
				arr.append(n // i)
		i = i + 1
	return arr

def isprime(n):
	if n == 1:
		return 0
	i = 2
	while i * i <= n:
		if n % i == 0:
			return 0
		i += 1
	return 1

def Sieve(n):
	prime = [True for i in range(n + 1)]
	prime[0] = False
	prime[1] = False
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def main():
	for ttt in range(int(input())):
		s = input()
		(n, k) = map(int, input().split())
		air = list(map(int, input().split()))
		temp = list(map(int, input().split()))
		l = [10 ** 18] * n
		r = [10 ** 18] * n
		val = 10 ** 18
		arr = [10 ** 18] * n
		for i in range(k):
			arr[air[i] - 1] = temp[i]
		p = 10 ** 18
		for i in range(n):
			p = min(p + 1, arr[i])
			l[i] = p
		q = 10 ** 18
		for i in range(n - 1, -1, -1):
			q = min(q + 1, arr[i])
			r[i] = q
		for i in range(n):
			print(min(l[i], r[i]), end=' ')
		print('')
main()


def air_conditioner(L, n):
	left = [10000000000.0 for _ in range(n)]
	right = [10000000000.0 for _ in range(n)]
	if L[0] != 0:
		left[0] = L[0]
	if L[n - 1] != 0:
		right[n - 1] = L[n - 1]
	for i in range(1, n):
		if L[i] != 0:
			left[i] = min(L[i], left[i - 1] + 1)
		else:
			left[i] = left[i - 1] + 1
	for i in range(n - 2, -1, -1):
		if L[i] != 0:
			right[i] = min(L[i], right[i + 1] + 1)
		else:
			right[i] = right[i + 1] + 1
	output = []
	for i in range(n):
		output.append(int(min(left[i], right[i])))
	print(*output)
T = int(input())
for _ in range(T):
	input()
	(l, n) = tuple(map(int, input().split()))
	L = [0 for _ in range(l)]
	index = list(map(int, input().split()))
	values = list(map(int, input().split()))
	for i in range(len(index)):
		L[index[i] - 1] = values[i]
	air_conditioner(L, l)


import sys
t = int(input())
for tt in range(t):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	t_expanded = [sys.maxsize] * n
	for i in range(k):
		t_expanded[a[i] - 1] = t[i]
	left = [0] * n
	te = sys.maxsize
	for i in range(n):
		te = min(te + 1, t_expanded[i])
		left[i] = te
	right = [0] * n
	te = sys.maxsize
	for i in range(n - 1, -1, -1):
		te = min(te + 1, t_expanded[i])
		right[i] = te
	final = [0] * n
	for i in range(n):
		final[i] = min(left[i], right[i])
		print(final[i], end=' ')


from collections import deque

def solve(n, k, a, t):
	h = []
	for i in range(k):
		x = t[i] + abs(a[i] - 1)
		h.append((x, a[i], t[i]))
	h = deque(sorted(h))
	z = []
	y = 10 ** 10
	for i in range(1, n + 1):
		if i == 1:
			(x, ai, ti) = h.popleft()
			z.append(x)
			continue
		y += 1
		while ai < i:
			y = min(y, ti + abs(ai - i))
			if len(h) > 0:
				(x, ai, ti) = h.popleft()
			else:
				x = 10 ** 10
				break
		z.append(min(y, ti + abs(ai - i)))
	return ' '.join(map(str, z))
t = int(input())
for i in range(t):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	print(solve(n, k, a, t))


import sys, os
from io import BytesIO, IOBase
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(stdin, stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def ceil(a, b):
	return (a + b - 1) // b
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
for _ in range(int(inp())):
	s = inp()
	(n, k) = mp()
	dp = l1d(n + 1, inf)
	arr = lmp()
	temps = lmp()
	arr = [(arr[i], i) for i in range(k)]
	arr.sort()
	x = 0
	for i in range(1, n + 1):
		if x < k and arr[x][0] == i:
			dp[i] = min(dp[i - 1] + 1, temps[arr[x][1]])
			x += 1
		else:
			dp[i] = dp[i - 1] + 1
	x = 0
	for i in range(n - 1, -1, -1):
		if x < k and arr[x][0] == i:
			dp[i] = min(dp[i], dp[i + 1] + 1, temps[arr[x][1]])
			x += 1
		else:
			dp[i] = min(dp[i], dp[i + 1] + 1)
	print(*dp[1:])


from math import inf

def cf_731E(n: int, acLocations: list, acTemperatures: list):
	answer = [inf] * n
	temperatures = {}
	for (index, temp) in zip(acLocations, acTemperatures):
		answer[index - 1] = temp
		temperatures[index] = temp
	acLocations.sort()
	start = acLocations[0]
	temp = answer[start - 1]
	for i in range(start + 1, n + 1):
		if i in temperatures:
			if i - start + temp > answer[i - 1]:
				start = i
				temp = answer[i - 1]
		answer[i - 1] = min(temp + i - start, answer[i - 1])
	start = acLocations[-1]
	temp = answer[start - 1]
	for i in range(start, 0, -1):
		if i in temperatures:
			if start - i + temp > answer[i - 1]:
				start = i
				temp = answer[i - 1]
		answer[i - 1] = min(start - i + temp, answer[i - 1])
	return answer
for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	acLocations = list(map(int, input().split()))
	acTemperatures = list(map(int, input().split()))
	print(*cf_731E(n, acLocations, acTemperatures))


def main():
	global n, k, a, t
	for _ in range(int(input())):
		input()
		(n, k) = [int(x) for x in input().split()]
		a = [int(x) for x in input().split()]
		t = [int(x) for x in input().split()]
		(a, t) = zip(*sorted(zip(a, t)))
		f()

def f():
	global n, k, a, t
	_max = 10 ** 10
	temp = [_max] * (n + 2)
	cond = 0
	for i in range(1, n + 1):
		if cond < k and i == a[cond]:
			if t[cond] < temp[i - 1] + 1:
				temp[i] = t[cond]
			else:
				temp[i] = temp[i - 1] + 1
			cond += 1
		else:
			temp[i] = temp[i - 1] + 1
	cond = k - 1
	for i in range(n, 0, -1):
		if cond > 0 and i == a[cond]:
			if t[cond] < temp[i + 1] + 1:
				_new = t[cond]
			else:
				_new = temp[i + 1] + 1
			cond -= 1
		else:
			_new = temp[i + 1] + 1
		if _new < temp[i]:
			temp[i] = _new
	s = ' '.join([str(x) for x in temp[1:n + 1]])
	print(s)
main()


q = int(input())
for i in range(q):
	stupid_space = input()
	(n, k) = map(int, input().split())
	spaces = [2 ** 31] * n
	for (ac, temp) in zip(map(int, input().split()), map(int, input().split())):
		spaces[ac - 1] = temp
	for k in range(1, n):
		spaces[k] = min(spaces[k], spaces[k - 1] + 1)
	for l in range(n - 2, -1, -1):
		spaces[l] = min(spaces[l], spaces[l + 1] + 1)
	print(' '.join((str(s) for s in spaces)))


import math
import heapq
import sys
input = sys.stdin.readline
S = lambda : input()
I = lambda : int(S())
M = lambda : map(int, S().split())
L = lambda : list(M())
H = 1000000000 + 7
for _ in range(I()):
	s = input()
	(n, k) = M()
	po = L()
	t = L()
	h = math.inf
	a = [h] * n
	(l, r) = ([0] * n, [0] * n)
	for i in range(k):
		a[po[i] - 1] = t[i]
	p = h
	for i in range(n):
		p = min(p + 1, a[i])
		l[i] = p
	p = h
	for i in range(n - 1, -1, -1):
		p = min(p + 1, a[i])
		r[i] = p
	for i in range(n):
		l[i] = min(l[i], r[i])
	print(*l)


q = int(input())
outputs = []
for _ in range(q):
	empty = input()
	(n, k) = [int(i) for i in input().split()]
	airs_pos = [int(i) for i in input().split()]
	airs_temp = [int(i) for i in input().split()]
	temps_l = [None for _ in range(n)]
	temps_r = [None for _ in range(n)]
	optimal_temp = [None for _ in range(n)]
	for (pos, temp) in zip(airs_pos, airs_temp):
		optimal_temp[pos - 1] = temp
	p = 10 ** 10
	for (idx, i) in enumerate(temps_l):
		p = min(p + 1, optimal_temp[idx] if optimal_temp[idx] != None else p + 1)
		temps_l[idx] = p
	p = 10 ** 10
	for (idx, i) in enumerate(temps_r[::-1]):
		idx = n - idx - 1
		p = min(p + 1, optimal_temp[idx] if optimal_temp[idx] != None else p + 1)
		temps_r[idx] = p
	for (idx, i) in enumerate(optimal_temp):
		optimal_temp[idx] = min(temps_r[idx], temps_l[idx])
		print(optimal_temp[idx], end=' ')
	print('\n')


result = []

def code():
	(n, k) = map(int, input().split())
	pos_k = list(map(int, input().split()))
	temp = list(map(int, input().split()))
	big = 10 ** 10 + 1
	L = [big] * n
	for x in range(k):
		L[pos_k[x] - 1] = temp[x]
	R = L[:]
	current = big + 1
	for x in range(n):
		current = min(current + 1, L[x])
		L[x] = current
	current2 = big + 1
	for x in range(n - 1, -1, -1):
		current2 = min(current2 + 1, R[x])
		R[x] = current2
	result.append(list((min(R[x], L[x]) for x in range(n))))
q = int(input())
for test in range(q):
	nothing = input()
	code()
for x in result:
	print(' '.join((str(y) for y in x)))


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
from array import array
import heapq
import math
sys.setrecursionlimit(100000)
mod = 1000000007
MAX = 99999999999
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def test_case():
	(n, k) = map(int, input().split())
	ac = [int(x) - 1 for x in input().split()]
	temp = [int(x) for x in input().split()]
	arr = [MAX for i in range(n)]
	for i in range(k):
		arr[ac[i]] = temp[i]
	leftMax = [MAX for i in range(n)]
	rightMax = [MAX for i in range(n)]
	leftMax[0] = arr[0]
	for i in range(1, n):
		leftMax[i] = min(arr[i], leftMax[i - 1] + 1)
	rightMax[-1] = arr[-1]
	for i in range(-2, -n - 1, -1):
		rightMax[i] = min(arr[i], rightMax[i + 1] + 1)
	final = []
	for i in range(n):
		final.append(min(leftMax[i], rightMax[i]))
	print(*final)

def main():
	t = int(input())
	for _ in range(t):
		input()
		test_case()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()

def dummy_function():
	x = 10
	x *= 100
	print('This function is written by Harsh.')

def dummy_function1():
	x = 10
	x *= 100
	print('This function is written by Harsh.')

def dummy_function3():
	x = 10
	x *= 100
	print('This function is written by Harsh.')

def dummy_function4():
	x = 10
	x *= 100
	print('This function is written by Harsh.')


def solve(k, n, air_pos, temperature):
	final_temp = [10 ** 12] * n
	air_pos_set = set(air_pos)
	air_temp = {air_pos[i]: temperature[i] for i in range(k)}
	air_list = [(temperature[i], air_pos[i]) for i in range(k)]
	air_list.sort(key=lambda x: x[0])
	a = min(air_list)
	min_temp = final_temp[0]
	for (t, a) in air_list:
		if t > min_temp + n:
			break
		for look_left in range(a):
			my_temp = t + look_left
			left_temp = 0
			left_index = a - look_left - 1
			left_temp = final_temp[left_index]
			new_temp = min(left_temp, my_temp)
			min_temp = min(min_temp, new_temp)
			final_temp[left_index] = new_temp
			if look_left > 0:
				if left_index + 1 in air_pos_set:
					if my_temp >= air_temp[left_index + 1]:
						break
				if left_temp <= my_temp:
					break
		for look_right in range(n - a + 1):
			my_temp = t + look_right
			right_temp = 0
			right_index = a + look_right - 1
			right_temp = final_temp[right_index]
			new_temp = min(right_temp, my_temp)
			min_temp = min(min_temp, new_temp)
			final_temp[right_index] = new_temp
			if look_right > 0:
				if right_index + 1 in air_pos_set:
					if my_temp >= air_temp[right_index + 1]:
						break
				if right_temp <= my_temp:
					break
	return ' '.join([str(x) for x in final_temp])
import os
import io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input().decode().strip())
for t in range(T):
	input()
	(n, k) = [int(x) for x in input().decode().strip().split(' ')]
	air_pos = [int(x) for x in input().decode().strip().split(' ')]
	temperature = [int(x) for x in input().decode().strip().split(' ')]
	res = solve(k, n, air_pos, temperature)
	print(res)


import math
for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	c = [math.inf] * n
	for i in range(k):
		c[a[i] - 1] = t[i]
	ans = [0] * n
	L = [math.inf] * n
	p = math.inf
	for i in range(n):
		p = min(p + 1, c[i])
		L[i] = p
	R = [math.inf] * n
	p = math.inf
	for i in range(n - 1, -1, -1):
		p = min(p + 1, c[i])
		R[i] = p
	for i in range(n):
		ans[i] = min(L[i], R[i])
	print(*ans)


for q in range(int(input())):
	a = input()
	(n, k) = map(int, input().split())
	msa = [int(i) for i in input().split()]
	sl = [int(i) for i in input().split()]
	mst = {}
	ans = []
	for i in range(k):
		mst[msa[i]] = sl[i]
	buf1 = []
	buf2 = []
	mn = 10 ** 18 + 1
	msa = set(msa)
	for i in range(1, n + 1):
		mn += 1
		if i not in msa:
			buf1.append(mn)
		elif mst[i] < mn:
			mn = mst[i]
			buf1.append(mn)
		else:
			buf1.append(mn)
	mn = 10 ** 18 + 1
	for i in range(n, 0, -1):
		mn += 1
		if i not in msa:
			buf2.append(mn)
		elif mst[i] < mn:
			mn = mst[i]
			buf2.append(mn)
		else:
			buf2.append(mn)
	buf2.reverse()
	for i in range(n):
		ans.append(min(buf1[i], buf2[i]))
	print(*ans)


import sys
from bisect import bisect
from math import sqrt, ceil, floor

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def rinput():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))
mod = int(1000000000.0) + 7
for _ in range(iinput()):
	input()
	(n, k) = rinput()
	a = get_list()
	t = get_list()
	pref = [10 ** 20] * n
	suff = [10 ** 20] * n
	for i in range(k):
		pref[a[i] - 1] = t[i]
		suff[a[i] - 1] = t[i]
	for i in range(1, n):
		pref[i] = min(pref[i - 1] + 1, pref[i])
	for i in range(n - 2, -1, -1):
		suff[i] = min(suff[i + 1] + 1, suff[i])
	print(*[min(pref[i], suff[i]) for i in range(n)])


import math

def merge(lst, left_start, left_end, right_start, right_end):
	temp_right = lst[right_start] + right_start
	for i in range(left_end, left_start - 1, -1):
		if temp_right - i < lst[i]:
			lst[i] = temp_right - i
		else:
			break
	temp_left = lst[left_end] - left_end
	for i in range(right_start, right_end + 1):
		if temp_left + i < lst[i]:
			lst[i] = temp_left + i
		else:
			break

def calculate_temperature(lst, start, end):
	if end - start == 0:
		return
	middle = (end + start) // 2
	calculate_temperature(lst, start, middle)
	calculate_temperature(lst, middle + 1, end)
	merge(lst, start, middle, middle + 1, end)
t = int(input())
results = []
for i in range(t):
	input()
	(n, k) = map(int, input().split())
	positions = [int(num) - 1 for num in input().split()]
	temperatures = [int(num) for num in input().split()]
	lst = [math.inf] * n
	for i in range(len(positions)):
		lst[positions[i]] = temperatures[i]
	calculate_temperature(lst, 0, n - 1)
	results.append(lst)
for i in range(t):
	print(' '.join([str(num) for num in results[i]]))


INF = 10 ** 10
for q in range(int(input())):
	blank = input()
	(n, k) = map(int, input().split())
	pos = list(map(int, input().split()))
	val = list(map(int, input().split()))
	pos = [x - 1 for x in pos]
	dp = [INF for _ in range(n)]
	arr = [(val[i] + pos[i], pos[i]) for i in range(k)]
	arr.sort(key=lambda x: x[0])
	j = 0
	for i in range(n):
		if j >= k:
			break
		dp[i] = arr[j][0] - i
		while j < k and i >= arr[j][1]:
			j += 1
	arr = [(val[i] - pos[i] + n, pos[i]) for i in range(k)]
	arr.sort(key=lambda x: x[0])
	j = 0
	for i in range(n - 1, 0, -1):
		if j >= k:
			break
		dp[i] = min(dp[i], arr[j][0] - n + i)
		while j < k and i <= arr[j][1]:
			j += 1
	print(*dp, sep=' ')


q = int(input())
for i in range(q):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	at = []
	for (i, j) in zip(a, t):
		at.append((i, j))
	at = sorted(at, key=lambda x: x[0])
	ans = [float('inf')] * n
	keep = float('inf')
	idx = 0
	for i in range(n):
		if 0 <= idx < k and i == at[idx][0] - 1:
			keep = min(keep, at[idx][1])
			idx += 1
		ans[i] = min(ans[i], keep)
		keep += 1
	keep = float('inf')
	idx = k - 1
	for i in reversed(range(n)):
		if 0 <= idx < k and i == at[idx][0] - 1:
			keep = min(keep, at[idx][1])
			idx -= 1
		ans[i] = min(ans[i], keep)
		keep += 1
	print(*ans)


def solve(n, A, T):
	mx = n + max(T)
	(L, R) = ([mx] * n, [mx] * n)
	airconds = [mx] * n
	for i in range(len(A)):
		(a, t) = (A[i], T[i])
		airconds[a - 1] = t
	for i in range(n)[::-1]:
		v1 = L[i + 1] + 1 if i + 1 < len(L) else mx
		L[i] = min(v1, airconds[i])
	for i in range(n):
		v1 = R[i - 1] + 1 if i > 0 else mx
		R[i] = min(v1, airconds[i])
	return [min(L[i], R[i]) for i in range(n)]

def main():
	for _ in range(int(input())):
		input()
		(n, k) = (*map(int, input().split(' ')),)
		A = [*map(int, input().split(' '))]
		T = [*map(int, input().split(' '))]
		print(*solve(n, A, T))
main()


for t in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	karr = list(map(int, input().split()))
	tarr = list(map(int, input().split()))
	arr = [float('inf')] * n
	kt = {k - 1: t for (k, t) in zip(karr, tarr)}
	for k in kt:
		arr[k] = kt[k]
	for (k, t) in sorted(list(kt.items()), key=lambda x: x[1]):
		(i, d) = (k + 1, 1)
		while i < n and arr[i] > t + d:
			arr[i] = t + d
			d += 1
			i += 1
		(i, d) = (k - 1, 1)
		while i >= 0 and arr[i] > t + d:
			arr[i] = t + d
			d += 1
			i -= 1
	sarr = [str(a) for a in arr]
	print(' '.join(sarr))


q = int(input())
while q > 0:
	q -= 1
	input()
	(n, k) = map(int, input().split())
	A = list(map(int, input().split()))
	T = list(map(int, input().split()))
	C = [max(T) + n] * n
	for air in range(k):
		C[A[air] - 1] = T[air]
	leftmost = min(A) - 1
	for cell in range(leftmost + 1, n):
		if C[cell] > C[cell - 1] + 1:
			C[cell] = C[cell - 1] + 1
	for cell in range(n - 2, -1, -1):
		if C[cell] > C[cell + 1] + 1:
			C[cell] = C[cell + 1] + 1
	print(*C)


import math
t = int(input())
for i in range(t):
	input()
	(n, k) = map(int, input().split())
	a = [int(x) for x in input().split()]
	t = [int(x) for x in input().split()]
	arr = [0] * n
	for i in range(k):
		arr[a[i] - 1] = t[i]
	lr = [math.inf] * n
	for i in range(n):
		if arr[i] != 0:
			lr[i] = arr[i] + i
	prelr = [0] * n
	x = math.inf
	for i in range(n - 1, -1, -1):
		prelr[i] = x - i
		x = min(x, lr[i])
	rl = [math.inf] * n
	for i in range(n):
		if arr[i] != 0:
			rl[i] = arr[i] + (n - 1 - i)
	prerl = [0] * n
	x = math.inf
	for i in range(n):
		prerl[i] = x - (n - 1 - i)
		x = min(x, rl[i])
	ans = [0] * n
	for i in range(n):
		ans[i] = min(prelr[i], prerl[i])
		if arr[i] != 0:
			ans[i] = min(ans[i], arr[i])
	print(*ans)


def naiveSolve():
	return

def main():
	q = int(input())
	allans = []
	for _ in range(q):
		input()
		(n, k) = readIntArr()
		a = readIntArr()
		t = readIntArr()
		t2 = [inf] * n
		for i in range(k):
			t2[a[i] - 1] = t[i]
		m = inf
		ans = [inf] * n
		for i in range(n):
			m = min(m, t2[i])
			ans[i] = min(ans[i], m)
			m += 1
		m = inf
		for i in range(n - 1, -1, -1):
			m = min(m, t2[i])
			ans[i] = min(ans[i], m)
			m += 1
		allans.append(ans)
	multiLineArrayOfArraysPrint(allans)
	return
import sys
input = sys.stdin.buffer.readline

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]

def makeArr(defaultValFactory, dimensionArr):
	dv = defaultValFactory
	da = dimensionArr
	if len(da) == 1:
		return [dv() for _ in range(da[0])]
	else:
		return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(x):
	print('{}'.format(x))
	sys.stdout.flush()
	return int(input())

def answerInteractive(ans):
	print('! {}'.format(ans))
	sys.stdout.flush()
inf = float('inf')
MOD = 998244353
from math import gcd, floor, ceil
for _abc in range(1):
	main()


for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	airconditioners = [float('inf')] * n
	positions = map(int, input().split())
	temperatures = map(int, input().split())
	for (position, temperature) in zip(positions, temperatures):
		airconditioners[position - 1] = temperature
	l = [float('inf')] * n
	r = [float('inf')] * n
	l[0] = airconditioners[0]
	r[-1] = airconditioners[-1]
	for i in range(1, n):
		l[i] = min(l[i - 1] + 1, airconditioners[i])
	for i in range(n - 2, -1, -1):
		r[i] = min(r[i + 1] + 1, airconditioners[i])
	results = []
	for i in range(n):
		results.append(min(l[i], r[i]))
	print(*results)


import sys
input = sys.stdin.buffer.readline
for _ in range(int(input())):
	x = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	ac = {}
	for i in range(k):
		ac[a[i]] = t[i]
	temp = [10000000000.0] * n
	min_temp = 10000000000.0
	for i in range(1, n + 1):
		if i in ac:
			min_temp = min(min_temp, ac[i])
		temp[i - 1] = min(temp[i - 1], min_temp)
		min_temp += 1
	min_temp = 10000000000.0
	for i in range(n, 0, -1):
		if i in ac:
			min_temp = min(min_temp, ac[i])
		temp[i - 1] = min(temp[i - 1], min_temp)
		min_temp += 1
	print(*temp)


def main():
	for _ in range(int(input())):
		input()
		(n, k) = map(int, input().split())
		a = list(map(int, input().split()))
		t = list(map(int, input().split()))
		b = {}
		for i in range(k):
			b[a[i] - 1] = t[i]
		r = [float('inf')] * n
		c = float('inf')
		for i in range(n):
			if i in b and b[i] < c:
				c = b[i]
			r[i] = min(r[i], c)
			c += 1
		c = float('inf')
		for i in range(n - 1, -1, -1):
			if i in b and b[i] < c:
				c = b[i]
			r[i] = min(r[i], c)
			c += 1
		print(' '.join([str(x) for x in r]))
main()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	arr = [float('inf')] * (n + 1)
	for i in range(k):
		arr[a[i]] = t[i]
	l = [float('inf')] * (n + 2)
	r = l.copy()
	for i in range(1, n + 1):
		l[i] = min(l[i - 1] + 1, arr[i])
	for i in range(n, 0, -1):
		r[i] = min(r[i + 1] + 1, arr[i])
	ans = []
	for i in range(1, n + 1):
		ans.append(min(l[i], r[i]))
	print(*ans)


import os, sys
from io import BytesIO, IOBase

def main():
	for _ in range(rint()):
		s = rstr()
		(n, k) = rints()
		(ixs, t) = (rints(), rints())
		(a, ans, cur) = ([float('inf')] * n, [float('inf')] * n, float('inf'))
		for i in range(k):
			a[ixs[i] - 1] = t[i]
		for i in range(n):
			cur = min(cur, a[i])
			ans[i] = cur
			cur += 1
		cur = float('inf')
		for i in range(n - 1, -1, -1):
			cur = min(cur, a[i])
			ans[i] = min(cur, ans[i])
			cur += 1
		print(*ans)

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
BUFSIZE = 8192
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
rstr = lambda : input().strip()
rstrs = lambda : [str(x) for x in input().split()]
rstr_2d = lambda n: [rstr() for _ in range(n)]
rint = lambda : int(input())
rints = lambda : [int(x) for x in input().split()]
rint_2d = lambda n: [rint() for _ in range(n)]
rints_2d = lambda n: [rints() for _ in range(n)]
ceil1 = lambda a, b: (a + b - 1) // b
main()


import time
from collections import deque

def inpt():
	return int(input())

def inpl():
	return list(map(int, input().split()))

def inpm():
	return map(int, input().split())

def solve():
	(n, k) = inpm()
	a = inpl()
	t = inpl()
	lans = [10000000000.0 for i in range(n)]
	for (i, j) in enumerate(a):
		lans[j - 1] = t[i]
	for i in range(1, n):
		lans[i] = min(lans[i - 1] + 1, lans[i])
	rans = [10000000000.0 for i in range(n)]
	for (i, j) in enumerate(a):
		rans[j - 1] = t[i]
	for i in range(n - 2, -1, -1):
		rans[i] = min(rans[i + 1] + 1, rans[i])
	ans = [min(lans[i], rans[i]) for i in range(n)]
	print(*ans)

def main():
	m = 10 ** 9 + 7
	t = int(input())
	while t:
		t -= 1
		input()
		solve()
main()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import *
from collections import deque
from operator import itemgetter, attrgetter
from decimal import *
import bisect
import math
import heapq as hq
MOD = 10 ** 9 + 7

def is_prime(n):
	if n == 2 or n == 3:
		return True
	if n < 2 or n % 2 == 0:
		return False
	if n < 9:
		return True
	if n % 3 == 0:
		return False
	r = int(n ** 0.5)
	f = 5
	while f <= r:
		if n % f == 0:
			return False
		if n % (f + 2) == 0:
			return False
		f += 6
	return True

def pow(a, b, m):
	ans = 1
	while b:
		if b & 1:
			ans = ans * a % m
		b //= 2
		a = a * a % m
	return ans

def ispalindrome(s):
	if s[:] == s[::-1]:
		return 1
	return 0
dp = []
limit = []
v = []

def dpdfs(u, t=-1):
	dp[0][u] = 0
	dp[1][u] = 0
	for i in v[u]:
		if i == t:
			continue
		if dp[1][i] == -1:
			dpdfs(i, u)
		dp[0][u] += max(abs(limit[0][u] - limit[1][i]) + dp[1][i], abs(limit[0][u] - limit[0][i]) + dp[0][i])
		dp[1][u] += max(abs(limit[1][u] - limit[1][i]) + dp[1][i], abs(limit[1][u] - limit[0][i]) + dp[0][i])
vis = []
f = 0

def dfs(i):
	vis[i] = 1
	act[i] = 1
	for j in v[i]:
		if act[j]:
			f = 1
			return -1
		if vis[j] == 0:
			if dfs(j) == -1:
				return -1
	act[i] = 0
	ans.append(i)
	return 0
from queue import PriorityQueue

def z_algorithm(s):
	res = [0] * len(s)
	res[0] = len(s)
	(i, j) = (1, 0)
	while i < len(s):
		while i + j < len(s) and s[j] == s[i + j]:
			j += 1
		res[i] = j
		if j == 0:
			i += 1
			continue
		k = 1
		while i + k < len(s) and k + res[k] < j:
			res[i + k] = res[k]
			k += 1
		(i, j) = (i + k, j - k)
	return res

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a / gcd(a, b) * b
for _ in range(int(input())):
	input()
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	left = [math.inf for i in range(n)]
	right = [math.inf for i in range(n)]
	for i in range(m):
		left[a[i] - 1] = t[i]
		right[a[i] - 1] = t[i]
	for i in range(1, n):
		left[i] = min(left[i - 1] + 1, left[i])
	for i in range(n - 2, -1, -1):
		right[i] = min(right[i + 1] + 1, right[i])
	ans = []
	for i in range(n):
		ans.append(min(left[i], right[i]))
	print(*ans)


import os
import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict, Counter
from bisect import bisect_left as bl, bisect_right as br
from itertools import combinations
sys.setrecursionlimit(100000000)
int_inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
mod = 1000000007
max_val = sys.maxsize

def solve():
	for _ in range(int_inp()):
		stdin.readline()
		(n, k) = mul()
		a = seq()
		t = seq()
		dpl = [max_val for x in range(n + 1)]
		dpr = [max_val for x in range(n + 1)]
		res = []
		for i in range(k):
			dpl[a[i]] = t[i]
		for i in range(k):
			dpr[a[i]] = t[i]
		for i in range(n - 1, 0, -1):
			dpr[i] = min(dpr[i], dpr[i + 1] + 1)
		for i in range(2, n + 1, 1):
			dpl[i] = min(dpl[i], dpl[i - 1] + 1)
		for i in range(1, n + 1):
			res.append(min(dpl[i], dpr[i]))
		print(*res)
solve()


for _ in range(int(input())):
	empty = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	INF_MAX = 1000000000000001
	ans = [INF_MAX] * n
	for i in range(k):
		ans[a[i] - 1] = t[i]
	for i in range(1, n):
		ans[i] = min(ans[i], ans[i - 1] + 1)
	i = n - 2
	while i >= 0:
		ans[i] = min(ans[i], ans[i + 1] + 1)
		i -= 1
	for i in ans:
		print(i, end=' ')
	print()


import os, sys, time, collections, math, pprint, itertools as it, operator as op, bisect as bs, functools as fn
(maxx, localsys, mod) = (1 << 60, 0, int(1000000000.0 + 7))
nCr = lambda n, r: reduce(mul, range(n - r + 1, n + 1), 1) // factorial(r)
ceil = lambda n, x: (n + x - 1) // x
(osi, oso) = ('/home/priyanshu/Documents/cp/input.txt', '/home/priyanshu/Documents/cp/output.txt')
if os.path.exists(osi):
	sys.stdin = open(osi, 'r')
	sys.stdout = open(oso, 'w')
input = sys.stdin.readline

def maps():
	return map(int, input().split())
for _ in range(int(input())):
	input()
	(n, k) = maps()
	a = list(maps())
	t = list(maps())
	c = [maxx] * n
	for i in range(k):
		c[a[i] - 1] = t[i]
	(l, r) = ([0] * n, [0] * n)
	p = maxx
	for i in range(n):
		p = min(p + 1, c[i])
		l[i] = p
	p = maxx
	for i in range(n - 1, -1, -1):
		p = min(p + 1, c[i])
		r[i] = p
	[print(min(i, j), end=' ') for (i, j) in zip(l, r)]
	print()


from sys import stdin, stdout, setrecursionlimit
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())
from collections import defaultdict as dd, Counter as C, deque
from math import ceil, gcd, sqrt, factorial, log2, floor
from bisect import bisect_right as br, bisect_left as bl
import heapq
for _ in range(it()):
	input()
	(n, k) = mp()
	l = mp()
	p = mp()
	l = list(zip(l, p))
	l.sort()
	j = 0
	(left, right) = ([0] * n, [0] * n)
	if l[j][0] == 1:
		left[0] = l[j][1]
		j += 1
	else:
		left[0] = float('inf')
	for i in range(1, n):
		if j < len(l):
			if i + 1 == l[j][0]:
				left[i] = min(l[j][1], left[i - 1] + 1)
				j += 1
			else:
				left[i] = left[i - 1] + 1
		else:
			left[i] = left[i - 1] + 1
	j = len(l) - 1
	if l[j][0] == n:
		right[-1] = l[j][1]
		j -= 1
	else:
		right[-1] = float('inf')
	for i in range(n - 2, -1, -1):
		if j >= 0:
			if l[j][0] == i + 1:
				right[i] = min(l[j][1], right[i + 1] + 1)
				j -= 1
			else:
				right[i] = right[i + 1] + 1
		else:
			right[i] = right[i + 1] + 1
	for i in range(n):
		print(min(left[i], right[i]), end=' ')
	print()


import sys
from collections import deque

def rl():
	return sys.stdin.readline().strip()

def rl_types(types):
	str_list = [x for x in sys.stdin.readline().strip().split(' ')]
	return [types[i](str_list[i]) for i in range(len(str_list))]

def pr(something):
	sys.stdout.write(str(something) + '\n')

def pra(array):
	sys.stdout.write(' '.join([str(x) for x in array]) + '\n')

def solve(array):
	return array
NT = int(rl())
for _ in range(NT):
	rl()
	(n, k) = [int(x) for x in rl().split(' ')]
	locs = [int(x) for x in rl().split(' ')]
	temps = [int(x) for x in rl().split(' ')]
	acs_raw = sorted([(locs[i] - 1, temps[i]) for i in range(k)])
	INF = float('inf')
	fromleft = [INF] * n
	fromright = [INF] * n
	acs = deque(acs_raw)
	ctemp = INF
	for i in range(n):
		if len(acs) > 0 and acs[0][0] == i:
			ctemp = min(ctemp, acs.popleft()[1])
		fromleft[i] = ctemp
		ctemp += 1
	acs = deque(acs_raw)
	ctemp = INF
	for i in range(n - 1, -1, -1):
		if len(acs) > 0 and acs[-1][0] == i:
			ctemp = min(ctemp, acs.pop()[1])
		fromright[i] = ctemp
		ctemp += 1
	result = [0] * n
	for i in range(n):
		result[i] = min(fromleft[i], fromright[i])
	pra(result)


for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	ans = [float('inf')] * n
	for i in range(k):
		ans[a[i] - 1] = t[i]
	for i in range(n - 1):
		ans[i + 1] = min(ans[i + 1], ans[i] + 1)
	for i in range(n - 1, 0, -1):
		ans[i - 1] = min(ans[i - 1], ans[i] + 1)
	print(*ans)


from sys import stdin
input = stdin.readline
q = int(input())
for _ in range(q):
	input()
	(n, k) = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	t = [int(x) for x in input().split()]
	conditioners = [0] * n
	for i in range(k):
		conditioners[a[i] - 1] = t[i]
	best_prefix = []
	last = float('inf')
	for i in range(n):
		if conditioners[i] == 0:
			best_prefix.append(last + 1)
			last += 1
		elif conditioners[i] < last + 1:
			best_prefix.append(conditioners[i])
			last = conditioners[i]
		else:
			best_prefix.append(last + 1)
			last += 1
	best_suffix = []
	last = float('inf')
	for i in range(n - 1, -1, -1):
		if conditioners[i] == 0:
			best_suffix.append(last + 1)
			last += 1
		elif conditioners[i] < last + 1:
			best_suffix.append(conditioners[i])
			last = conditioners[i]
		else:
			best_suffix.append(last + 1)
			last += 1
	best_suffix.reverse()
	ans = []
	for i in range(n):
		ans.append(min(best_prefix[i], best_suffix[i]))
	print(*ans)


def solve():
	maxn = 10 ** 19
	ans = []
	l = [maxn] * n
	r = [maxn] * n
	c = [maxn] * n
	for i in range(k):
		c[a[i] - 1] = t[i]
	l[0] = c[0]
	r[-1] = c[-1]
	for i in range(1, n):
		l[i] = min(l[i - 1] + 1, c[i])
	for i in range(n - 2, -1, -1):
		r[i] = min(r[i + 1] + 1, c[i])
	for i in range(n):
		ans.append(min(l[i], r[i]))
	return ans
for q in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	print(*solve())


n = int(input())
for i in range(n):
	input()
	(n, k) = [int(x) for x in input().split()]
	c = [2 ** 32] * n
	cond = [int(x) for x in input().split()]
	temperatures = [int(x) for x in input().split()]
	for i in range(k):
		c[cond[i] - 1] = temperatures[i]
	p = 2 ** 32
	lefts = [0] * n
	rights = [0] * n
	for i in range(n):
		p = min(p + 1, c[i])
		lefts[i] = p
	for i in range(n - 1, -1, -1):
		p = min(p + 1, c[i])
		rights[i] = p
	result = [0] * n
	for i in range(n):
		result[i] = min(lefts[i], rights[i])
	print(*result)


for _ in range(int(input())):
	tt = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	dp1 = [10000000000] * n
	dp2 = [10000000000] * n
	for i in range(k):
		dp1[a[i] - 1] = t[i]
		dp2[a[i] - 1] = t[i]
	for i in range(n):
		if i == 0:
			minn = dp1[i]
			pos = i
		else:
			dp1[i] = min(dp1[i - 1] + 1, minn + (i - pos), dp1[i])
			if dp1[i] <= minn:
				minn = dp1[i]
				pos = i
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			minn = dp2[i]
			pos = i
		else:
			dp2[i] = min(dp2[i + 1] + 1, minn + (pos - i), dp2[i])
			if dp2[i] <= minn:
				minn = dp2[i]
				pos = i
	for i in range(n):
		print(min(dp1[i], dp2[i]), end=' ')
	print()


def main():
	alpha = 'abcdefghijklmnopqrstuvwxyz'
	ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
	inf = 1e+17
	mod = 10 ** 9 + 7

	def factorial(n):
		f = 1
		for i in range(1, n + 1):
			f = f * i % mod
		return f

	def ncr(n, r):
		num = den = 1
		for i in range(r):
			num = num * (n - i) % mod
			den = den * (i + 1) % mod
		return num * pow(den, mod - 2, mod) % mod

	def solve(n, k, ai, ti):
		dp = [inf] * n
		for i in range(k):
			dp[ai[i] - 1] = ti[i]
		for i in range(1, n):
			dp[i] = min(dp[i - 1] + 1, dp[i])
		for i in range(n - 2, -1, -1):
			dp[i] = min(dp[i + 1] + 1, dp[i])
		return ' '.join(list(map(str, dp)))
		pass
	t = int(input())
	ans = []
	for _ in range(t):
		s = input()
		(n, k) = map(int, input().split())
		ai = [int(x) for x in input().split()]
		ti = [int(x) for x in input().split()]
		ans.append(solve(n, k, ai, ti))
	for answer in ans:
		print(answer)
import sys, threading
import bisect
import math
import itertools
from sys import stdout
import heapq
from queue import PriorityQueue
input = sys.stdin.readline
thread = threading.Thread(target=main)
thread.start()
thread.join()


q = int(input())
for _ in range(q):
	input()
	(n, k) = [int(x) for x in input().split()]
	pos = [int(x) for x in input().split()]
	temp = [int(x) for x in input().split()]
	temperature = [-1] * n
	for i in range(k):
		temperature[pos[i] - 1] = temp[i]
	L = [float('inf')] * (n + 1)
	R = [float('inf')] * (n + 1)
	for i in range(1, n + 1):
		if temperature[i - 1] != -1:
			L[i] = min(L[i - 1] + 1, temperature[i - 1])
		else:
			L[i] = L[i - 1] + 1
	for i in range(n - 1, -1, -1):
		if temperature[i] != -1:
			R[i] = min(R[i + 1] + 1, temperature[i])
		else:
			R[i] = R[i + 1] + 1
	ans = [min(L[x + 1], R[x]) for x in range(n)]
	print(' '.join([str(x) for x in ans]))


for _ in range(int(input())):
	sss = input()
	(n, k) = map(int, input().split())
	A = list(map(int, input().split()))
	T = list(map(int, input().split()))
	L = [10 ** 10] * n
	R = [10 ** 10] * n
	ans = [10 ** 10] * n
	for i in range(k):
		ans[A[i] - 1] = T[i]
	p = 10 ** 10
	for i in range(n):
		p = min(p + 1, ans[i])
		L[i] = p
	p = 10 ** 10
	for i in range(n - 1, -1, -1):
		p = min(p + 1, ans[i])
		R[i] = p
	out = [0] * n
	for i in range(n):
		out[i] = min(L[i], R[i])
	print(*out)


from math import inf
for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	L = [inf] * n
	for i in range(len(a)):
		L[a[i] - 1] = t[i]
	R = L.copy()
	for l in range(1, len(L)):
		L[l] = min(L[l - 1] + 1, L[l])
	for r in range(1, len(R)):
		R[-r - 1] = min(R[-r] + 1, R[-r - 1])
	res = [0] * n
	for i in range(len(res)):
		res[i] = min(L[i], R[i])
	print(*res)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import ceil

def prod(a, mod=10 ** 9 + 7):
	ans = 1
	for each in a:
		ans = ans * each % mod
	return ans

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
from math import inf, log2

class SegmentTree:

	def __init__(self, array, func=max):
		self.n = len(array)
		self.size = 2 ** (int(log2(self.n - 1)) + 1) if self.n != 1 else 1
		self.func = func
		self.default = 0 if self.func != min else inf
		self.data = [self.default] * (2 * self.size)
		self.process(array)

	def process(self, array):
		self.data[self.size:self.size + self.n] = array
		for i in range(self.size - 1, -1, -1):
			self.data[i] = self.func(self.data[2 * i], self.data[2 * i + 1])

	def query(self, alpha, omega):
		if alpha == omega:
			return self.data[alpha + self.size]
		res = self.default
		alpha += self.size
		omega += self.size + 1
		while alpha < omega:
			if alpha & 1:
				res = self.func(res, self.data[alpha])
				alpha += 1
			if omega & 1:
				omega -= 1
				res = self.func(res, self.data[omega])
			alpha >>= 1
			omega >>= 1
		return res

	def update(self, index, value):
		index += self.size
		self.data[index] = value
		index >>= 1
		while index:
			self.data[index] = self.func(self.data[2 * index], self.data[2 * index + 1])
			index >>= 1

class SortedList:

	def __init__(self, iterable=[], _load=200):
		values = sorted(iterable)
		self._len = _len = len(values)
		self._load = _load
		self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]
		self._list_lens = [len(_list) for _list in _lists]
		self._mins = [_list[0] for _list in _lists]
		self._fen_tree = []
		self._rebuild = True

	def _fen_build(self):
		self._fen_tree[:] = self._list_lens
		_fen_tree = self._fen_tree
		for i in range(len(_fen_tree)):
			if i | i + 1 < len(_fen_tree):
				_fen_tree[i | i + 1] += _fen_tree[i]
		self._rebuild = False

	def _fen_update(self, index, value):
		if not self._rebuild:
			_fen_tree = self._fen_tree
			while index < len(_fen_tree):
				_fen_tree[index] += value
				index |= index + 1

	def _fen_query(self, end):
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		x = 0
		while end:
			x += _fen_tree[end - 1]
			end &= end - 1
		return x

	def _fen_findkth(self, k):
		_list_lens = self._list_lens
		if k < _list_lens[0]:
			return (0, k)
		if k >= self._len - _list_lens[-1]:
			return (len(_list_lens) - 1, k + _list_lens[-1] - self._len)
		if self._rebuild:
			self._fen_build()
		_fen_tree = self._fen_tree
		idx = -1
		for d in reversed(range(len(_fen_tree).bit_length())):
			right_idx = idx + (1 << d)
			if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:
				idx = right_idx
				k -= _fen_tree[idx]
		return (idx + 1, k)

	def _delete(self, pos, idx):
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len -= 1
		self._fen_update(pos, -1)
		del _lists[pos][idx]
		_list_lens[pos] -= 1
		if _list_lens[pos]:
			_mins[pos] = _lists[pos][0]
		else:
			del _lists[pos]
			del _list_lens[pos]
			del _mins[pos]
			self._rebuild = True

	def _loc_left(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(lo, pos) = (-1, len(_lists) - 1)
		while lo + 1 < pos:
			mi = lo + pos >> 1
			if value <= _mins[mi]:
				pos = mi
			else:
				lo = mi
		if pos and value <= _lists[pos - 1][-1]:
			pos -= 1
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value <= _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def _loc_right(self, value):
		if not self._len:
			return (0, 0)
		_lists = self._lists
		_mins = self._mins
		(pos, hi) = (0, len(_lists))
		while pos + 1 < hi:
			mi = pos + hi >> 1
			if value < _mins[mi]:
				hi = mi
			else:
				pos = mi
		_list = _lists[pos]
		(lo, idx) = (-1, len(_list))
		while lo + 1 < idx:
			mi = lo + idx >> 1
			if value < _list[mi]:
				idx = mi
			else:
				lo = mi
		return (pos, idx)

	def add(self, value):
		_load = self._load
		_lists = self._lists
		_mins = self._mins
		_list_lens = self._list_lens
		self._len += 1
		if _lists:
			(pos, idx) = self._loc_right(value)
			self._fen_update(pos, 1)
			_list = _lists[pos]
			_list.insert(idx, value)
			_list_lens[pos] += 1
			_mins[pos] = _list[0]
			if _load + _load < len(_list):
				_lists.insert(pos + 1, _list[_load:])
				_list_lens.insert(pos + 1, len(_list) - _load)
				_mins.insert(pos + 1, _list[_load])
				_list_lens[pos] = _load
				del _list[_load:]
				self._rebuild = True
		else:
			_lists.append([value])
			_mins.append(value)
			_list_lens.append(1)
			self._rebuild = True

	def discard(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_right(value)
			if idx and _lists[pos][idx - 1] == value:
				self._delete(pos, idx - 1)

	def remove(self, value):
		_len = self._len
		self.discard(value)
		if _len == self._len:
			raise ValueError('{0!r} not in list'.format(value))

	def pop(self, index=-1):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		value = self._lists[pos][idx]
		self._delete(pos, idx)
		return value

	def bisect_left(self, value):
		(pos, idx) = self._loc_left(value)
		return self._fen_query(pos) + idx

	def bisect_right(self, value):
		(pos, idx) = self._loc_right(value)
		return self._fen_query(pos) + idx

	def count(self, value):
		return self.bisect_right(value) - self.bisect_left(value)

	def __len__(self):
		return self._len

	def __getitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		return self._lists[pos][idx]

	def __delitem__(self, index):
		(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)
		self._delete(pos, idx)

	def __contains__(self, value):
		_lists = self._lists
		if _lists:
			(pos, idx) = self._loc_left(value)
			return idx < len(_lists[pos]) and _lists[pos][idx] == value
		return False

	def __iter__(self):
		return (value for _list in self._lists for value in _list)

	def __reversed__(self):
		return (value for _list in reversed(self._lists) for value in reversed(_list))

	def __repr__(self):
		return 'SortedList({0})'.format(list(self))
for _ in range(int(input()) if True else 1):
	e = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	pairs = []
	for i in range(k):
		pairs += [[a[i], t[i]]]
	pairs = sorted(pairs, key=lambda x: x[0])
	ans = [10 ** 18] * (n + 1)
	available = []
	for (pos, t) in pairs:
		available += [t + pos]
	available = SortedList(available)
	available2 = SortedList([])
	cur = 0
	i = 0
	while i <= n:
		if available.__len__():
			ans[i] = min(ans[i], available.__getitem__(0) - i)
		if cur != k and pairs[cur][0] == i:
			available.remove(pairs[cur][0] + pairs[cur][1])
			available2.add(pairs[cur][1] - pairs[cur][0])
			cur += 1
		if available2.__len__():
			ans[i] = min(ans[i], available2.__getitem__(0) + i)
		i += 1
	print(*ans[1:])


for _ in range(int(input())):
	empty = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	ac = []
	for i in range(k):
		ac.append((a[i], t[i]))
	ac.sort()
	pre = [1000000000000000000] * n
	suf = [1000000000000000000] * n
	idx = 0
	mn = 1000000000000000000
	for i in range(n):
		if idx < k and ac[idx][0] == i + 1:
			mn += 1
			mn = min(mn, ac[idx][1])
			idx += 1
			pre[i] = mn
		elif mn != 1000000000000000000:
			mn += 1
			pre[i] = mn
	mn = 1000000000000000000
	idx = k - 1
	for i in range(n - 1, -1, -1):
		if idx >= 0 and ac[idx][0] == i + 1:
			mn += 1
			mn = min(mn, ac[idx][1])
			idx -= 1
			suf[i] = mn
		elif mn != 1000000000000000000:
			mn += 1
			suf[i] = mn
	ans = []
	for i in range(n):
		print(min(pre[i], suf[i]), end=' ')
	print('')


def read_ints():
	return list(map(int, input().split()))

def fill_temps(a):
	temps = [0 for _ in range(n + 1)]
	i = 0
	for j in range(1, len(temps)):
		if i != len(a) and j == a[i][0]:
			i += 1
		if i == len(a):
			temps[j] = a[-1][1] + abs(a[-1][0] - j)
			continue
		if i == 0:
			temps[j] = a[0][1] + abs(a[0][0] - j)
			continue
		temps[j] = min(a[i][1] + abs(a[i][0] - j), a[i - 1][1] + abs(a[i - 1][0] - j))
	return temps

def filter_conds(a):
	i = 0
	skipped = [False]
	for j in range(1, len(a)):
		if a[j][1] > a[i][1] + abs(a[j][0] - a[i][0]):
			skipped.append(True)
		else:
			skipped.append(False)
			i = j
	return [x for (sk, x) in zip(skipped, a) if not sk]

def calc_temps(a, n):
	a = sorted(a, key=lambda x: x[0])
	a = filter_conds(list(reversed(a)))
	a = filter_conds(list(reversed(a)))
	return fill_temps(a)
q = int(input())
for _ in range(q):
	input()
	(n, k) = read_ints()
	a = read_ints()
	t = read_ints()
	print(' '.join(map(str, calc_temps(zip(a, t), n)[1:])))


q = int(input())
for _ in range(q):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	ans = [(10 ** 18, 1)] * n
	for i in range(k):
		ans[a[i] - 1] = (t[i], a[i])
	for i in range(1, n):
		pt = ans[i - 1][0]
		pa = ans[i - 1][1]
		pv = pt + abs(pa - (i + 1))
		ct = ans[i][0]
		ca = ans[i][1]
		cv = ct + abs(ca - (i + 1))
		if pv < cv:
			ans[i] = (pt, pa)
		else:
			ans[i] = (ct, ca)
	for i in range(n - 2, -1, -1):
		st = ans[i + 1][0]
		sa = ans[i + 1][1]
		sv = st + abs(sa - (i + 1))
		ct = ans[i][0]
		ca = ans[i][1]
		cv = ct + abs(ca - (i + 1))
		if sv < cv:
			ans[i] = (st, sa)
		else:
			ans[i] = (ct, ca)
	res = []
	for i in range(n):
		res.append(ans[i][0] + abs(ans[i][1] - (i + 1)))
	print(*res)


import sys
from sys import stdin, stdout
from math import ceil, floor, sqrt, log, log2, log10
from collections import Counter
int_max = float('inf')
int_min = -float('inf')
ANS = []
input = stdin.readline

def solve():
	pass
t = int(input())
for i in range(t):
	input()
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	temp = list(map(int, input().split()))
	l = [int_max] * n
	r = [int_max] * n
	for i in range(k):
		l[arr[i] - 1] = temp[i]
		r[arr[i] - 1] = temp[i]
	for i in range(1, n):
		l[i] = min(l[i], l[i - 1] + 1)
	for i in range(n - 2, -1, -1):
		r[i] = min(r[i], r[i + 1] + 1)
	for i in range(n):
		print(min(l[i], r[i]), end=' ')
	print()
print('\n'.join(ANS))


for _ in range(int(input())):
	input()
	(k, _) = [int(i) for i in input().split()]
	idx = [int(i) for i in input().split()]
	temp = [int(i) for i in input().split()]
	ret = [float('inf') for _ in range(k)]
	ac = {}
	for (iii, kt) in enumerate(idx):
		kt -= 1
		wd = temp[iii]
		ac[kt] = wd
	acc = list(ac.items())
	acc.sort(key=lambda x: x[1])
	for (kt, wd) in acc:
		ret[kt] = min(ret[kt], wd)
		dist = 1
		for i in range(kt - 1, -1, -1):
			t = wd + dist
			if t >= ret[i]:
				break
			elif i in ac and ac[i] <= t:
				break
			else:
				ret[i] = t
			dist += 1
		dist = 1
		for i in range(kt + 1, k):
			t = wd + dist
			if t >= ret[i]:
				break
			elif i in ac and ac[i] <= t:
				break
			else:
				ret[i] = t
			dist += 1
	print(' '.join([str(i) for i in ret]))


for i in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	l = [[a[i], t[i]] for i in range(k)]
	l.sort()
	dp = [10 ** 10] * (n + 1)
	x = 0
	for i in range(n):
		if x < k and i == l[x][0] - 1:
			dp[i] = min(dp[i - 1] + 1, l[x][1])
			x += 1
		else:
			dp[i] = 1 + dp[i - 1]
	dp2 = [10 ** 10] * (n + 1)
	x = k - 1
	for i in range(n - 1, -1, -1):
		if x > -1 and i == l[x][0] - 1:
			dp2[i] = min(dp2[i + 1] + 1, l[x][1])
			x -= 1
		else:
			dp2[i] = 1 + dp2[i + 1]
	ans = [0] * n
	for i in range(n):
		ans[i] = min(dp[i], dp2[i])
	print(*ans)


from __future__ import print_function
from collections import *
from sys import stdin
import sys
from heapq import *
from math import log2, ceil, sqrt, gcd, log
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
rr = lambda x: reversed(range(x))
mod = int(1000000000.0) + 7
inf = float('inf')
(t,) = gil()
for _ in range(t):
	g()
	(n, k) = gil()
	ans = [inf] * (n + 1)
	for (pos, temp) in zip(gil(), gil()):
		ans[pos] = temp
	for i in range(1, n):
		ans[i + 1] = min(ans[i + 1], ans[i] + 1)
	for i in reversed(range(1, n + 1)):
		ans[i - 1] = min(ans[i - 1], ans[i] + 1)
	print(*ans[1:])


import math
import os
import sys
from sys import *
from decimal import *
from io import BytesIO, IOBase
from itertools import accumulate, combinations, permutations, combinations_with_replacement, product
from collections import *
M = 10 ** 9 + 7
import heapq
from bisect import *
from functools import lru_cache
from queue import PriorityQueue
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def inpu():
	return int(inp())

def luckynumber(x, n, a):
	if x > 0:
		a.append(x)
	if x > 10 ** 9:
		return a
	elif x < 1000000000000.0:
		luckynumber(x * 10 + 4, n, a)
		luckynumber(x * 10 + 7, n, a)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def query1(l, r):
	if l >= r:
		return -1
	print('?', l + 1, r + 1)
	sys.stdout.flush()
	return int(input()) - 1

def answer(p):
	print('!', p + 1)
	sys.stdout.flush()
	exit()

def rse(arr, n):
	stack = []
	ans = []
	for i in range(n - 1, -1, -1):
		if len(stack) == 0:
			ans.append(n)
		else:
			while len(stack) != 0:
				if stack[-1][0] >= arr[i]:
					stack.pop()
				else:
					break
			if len(stack) == 0:
				ans.append(n)
			else:
				ans.append(stack[-1][1])
		stack.append([arr[i], i])
	ans.reverse()
	return ans

def lse(arr, n):
	stack = []
	ans = []
	for i in range(n):
		if len(stack) == 0:
			ans.append(-1)
		else:
			while len(stack) != 0:
				if stack[-1][0] >= arr[i]:
					stack.pop()
				else:
					break
			if len(stack) == 0:
				ans.append(-1)
			else:
				ans.append(stack[-1][1])
		stack.append([arr[i], i])
	return ans

def mah(arr):
	max1 = 0
	p = rse(arr, len(arr))
	q = lse(arr, len(arr))
	for i in range(len(arr)):
		a = (p[i] - q[i] - 1) * arr[i]
		max1 = max(a, max1)
	return max1

def maxSumIS(arr, n):
	msis = arr.copy()
	for i in range(1, n):
		for j in range(i):
			if arr[i] > arr[j] and msis[i] < msis[j] + arr[i]:
				msis[i] = msis[j] + arr[i]
	c = max(msis)
	p = 5
	return c

def binarysearch2(arr, target):
	lo = 0
	hi = len(arr) - 1
	while lo <= hi:
		mid = (lo + hi) // 2
		if arr[mid] == target:
			if mid != len(arr) - 1:
				if arr[mid + 1] != target:
					return mid
				else:
					lo += 1
			else:
				return mid
			continue
		if arr[mid] > target:
			hi = mid - 1
		else:
			lo = mid + 1

def ngr(arr, n):
	stack = []
	ans = []
	for i in range(n - 1, -1, -1):
		if len(stack) == 0:
			ans.append(-1)
		else:
			while len(stack) > 0:
				if stack[-1][0] < arr[i]:
					stack.pop()
				else:
					break
			if len(stack) == 0:
				ans.append(-1)
			else:
				ans.append(stack[-1][1])
		stack.append([arr[i], i])
	ans.reverse()
	return ans

def alperm(nums, path, result):
	if not nums:
		result.add(tuple(path))
		return
	for i in range(0, len(nums)):
		alperm(nums[:i] + nums[i + 1:], path + [nums[i]], result)
	return result

def minsum(arr, n, m, res, l):
	if n == 1 and m == 1:
		res += arr[0][0]
		l.append(res)
	else:
		if n != 1:
			p = res + arr[n - 1][m - 1]
			minsum(arr, n - 1, m, p, l)
		if m != 1:
			p = res + arr[n - 1][m - 1]
			minsum(arr, n, m - 1, p, l)
	return min(l)

def subset(arr, n, ans, c):
	if n == 0:
		print(c)
		ans.add(tuple(sorted(c)))
		return
	else:
		op = c + [arr[n - 1]]
		subset(arr, n - 1, ans, op)
		subset(arr, n - 1, ans, c)

def catalan(n):
	if n == 0 or n == 1:
		return 1
	catalan = [0] * (n + 1)
	catalan[0] = 1
	catalan[1] = 1
	for i in range(2, n + 1):
		for j in range(i):
			catalan[i] += catalan[j] * catalan[i - j - 1]
	return catalan[n]

def isP(p, i, j):
	if j <= i:
		return True
	if p[i] != p[j]:
		return False
	return isP(p, i + 1, j - 1)

def main():
	t = 1
	t = inpu()
	for _ in range(t):
		inp()
		(n, k) = sep()
		a = lis()
		t = lis()
		ans = [10 ** 12] * n
		for i in range(k):
			ans[a[i] - 1] = t[i]
		for i in range(n - 1):
			ans[i + 1] = min(ans[i + 1], ans[i] + 1)
		for i in range(n - 1, 0, -1):
			ans[i - 1] = min(ans[i - 1], ans[i] + 1)
		print(*ans)
main()


t = int(input())
for _ in range(t):
	input()
	(n, k) = map(int, input().split())
	strip = list(map(int, input().split()))
	temp = list(map(int, input().split()))
	answer = [float('inf')] * n
	for i in range(k):
		answer[strip[i] - 1] = temp[i]
	for i in range(1, n):
		answer[i] = min(answer[i], answer[i - 1] + 1)
	for i in range(n - 1, 0, -1):
		answer[i - 1] = min(answer[i - 1], 1 + answer[i])
	print(*answer)


for _ in range(int(input())):
	space = input()
	(n, k) = map(int, input().split())
	arr = [0 for i in range(n)]
	arr1 = [0 for i in range(n)]
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for i in range(k):
		arr[a[i] - 1] = b[i]
		arr1[a[i] - 1] = b[i]
	val = -1
	co = -1
	for i in range(n):
		if val == -1 and arr1[i] != 0:
			val = arr1[i]
			co = i
		elif arr1[i] != 0:
			if val + abs(co - i) > arr[i]:
				co = i
				val = arr[i]
		if val != -1:
			arr[i] = max(0, val + abs(co - i))
	val = -1
	co = -1
	for i in range(n - 1, -1, -1):
		if val == -1 and arr1[i] != 0:
			val = arr1[i]
			co = i
		elif arr1[i] != 0:
			if val + abs(co - i) > arr[i]:
				co = i
				val = arr[i]
		if val != -1:
			if arr[i] == 0:
				arr[i] = val + abs(co - i)
			else:
				arr[i] = min(arr[i], val + abs(co - i))
	print(*arr)


import os
import sys
from io import BytesIO, IOBase

def main():
	for _ in range(int(input())):
		input()
		(n, k) = map(int, input().split(' '))
		temp = [x + 1 for x in range(n)]
		ac = list(map(int, input().split(' ')))
		tm = list(map(int, input().split(' ')))
		comb = []
		for x in range(len(tm)):
			comb.append([ac[x], tm[x]])
		comb.sort()
		stk = []
		for x in range(k):
			if len(stk) == 0:
				stk.append(comb[x])
			elif stk[-1][1] + comb[x][0] - stk[-1][0] <= comb[x][1]:
				continue
			else:
				while len(stk) > 0 and comb[x][1] + comb[x][0] - stk[-1][0] <= stk[-1][1]:
					stk.pop()
				stk.append(comb[x])
		ans = [1000000000000.0 for x in range(n + 1)]
		done = set()
		for x in range(len(stk)):
			for y in range(stk[x][0], 0, -1):
				ans[y] = min(ans[y], stk[x][1] + stk[x][0] - y)
				if y in done:
					break
			for y in range(stk[x][0], n + 1):
				ans[y] = min(ans[y], stk[x][1] + y - stk[x][0])
				if x + 1 < len(stk):
					if y >= stk[x + 1][0]:
						break
			done.add(stk[x][0])
		print(*ans[1:])
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import os, sys, math
from io import BytesIO, IOBase
it = lambda : int(input())
ma = lambda : map(int, input().split(' '))
li = lambda : list(map(int, input().split(' ')))
mod = 10 ** 9 + 7
tx = 10 ** 9 + 10 ** 6

def main():
	for _ in range(int(input())):
		emp = input()
		(n, k) = ma()
		a = li()
		t = li()
		ac = [0 for i in range(n)]
		tc = [0] * n
		for i in range(k):
			ac[a[i] - 1] = 1
			tc[a[i] - 1] = t[i]
		o = 0
		lastind = -1
		lasttemp = tx
		ans = [tx] * n
		for i in range(n):
			if ac[i]:
				o += 1
				if o == 1:
					lastind = i
					lasttemp = tc[i]
				elif i - lastind + lasttemp > tc[i]:
					lastind = i
					lasttemp = tc[i]
			ans[i] = min(ans[i], i - lastind + lasttemp)
		lastind = -1
		lasttemp = tx
		o = 0
		for i in range(n - 1, -1, -1):
			if ac[i]:
				o += 1
				if o == 1:
					lastind = i
					lasttemp = tc[i]
				elif lastind - i + lasttemp > tc[i]:
					lastind = i
					lasttemp = tc[i]
			ans[i] = min(ans[i], lasttemp + lastind - i)
		print(*ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	l = [[a[i] - 1, b[i]] for i in range(k)]
	l.sort(key=lambda x: x[0])
	m = 1 << 60
	dp = [m] * n
	dp[l[0][0]] = l[0][1]
	prev = l[0][0]
	preva = l[0][1]
	for i in range(k):
		x = l[i][0]
		y = l[i][1]
		if abs(x - prev) + preva >= y:
			prev = x
			preva = y
			dp[x] = preva
		else:
			dp[x] = abs(x - prev) + preva
	for i in range(1, n):
		dp[i] = min(dp[i - 1] + 1, dp[i])
	dp2 = [m] * n
	dp2[l[-1][0]] = l[-1][1]
	prev = l[-1][0]
	preva = l[-1][1]
	for i in range(k - 1, -1, -1):
		x = l[i][0]
		y = l[i][1]
		if abs(x - prev) + preva >= y:
			prev = x
			preva = y
			dp2[x] = preva
		else:
			dp2[x] = abs(x - prev) + preva
	for i in range(n - 1, 0, -1):
		dp2[i - 1] = min(dp2[i] + 1, dp2[i - 1])
	ans = []
	for i in range(n):
		ans.append(min(dp[i], dp2[i]))
	print(*ans)


def decimalToBinary(n):
	return '{0:b}'.format(int(n))
t = int(input())
for w in range(t):
	st = input()
	(n, k) = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	t = [int(x) for x in input().split()]
	arr = []
	for i in range(k):
		arr.append([a[i], t[i]])
	arr.sort()
	v = [10 ** 12 + 1 for i in range(n)]
	j = arr[0][0] - 1
	r = 1
	val = arr[0][1]
	while j < n:
		if r < len(arr):
			if arr[r][0] - 1 == j:
				val = min(val, arr[r][1])
				r += 1
		v[j] = min(v[j], val)
		val += 1
		j += 1
	j = arr[k - 1][0] - 1
	r = k - 1
	val = arr[r][1]
	while j >= 0:
		if r >= 0:
			if arr[r][0] - 1 == j:
				val = min(val, arr[r][1])
				r -= 1
		v[j] = min(v[j], val)
		val += 1
		j -= 1
	print(*v)


def f(n, k, ind, val):
	ar = [float('inf')] * n
	for i in range(len(ind)):
		ar[ind[i] - 1] = val[i]
	l = [float('inf')] * n
	r = [float('inf')] * n
	base = float('inf')
	for i in range(0, n):
		base = min(base + 1, ar[i])
		l[i] = base
	base = float('inf')
	for i in range(n - 1, -1, -1):
		base = min(base + 1, ar[i])
		r[i] = base
	for i in range(n):
		print(min(l[i], r[i]), end=' ')
	print('\n')
for _ in range(int(input())):
	s = input()
	(n, k) = map(int, input().strip().split())
	lst = list(map(int, input().strip().split()))
	lst2 = list(map(int, input().strip().split()))
	f(n, k, lst, lst2)


from collections import defaultdict, deque
import math

def inp():
	l = list(map(int, input().split()))
	return l

def main():
	for _ in range(int(input())):
		input()
		(n, k) = inp()
		a = inp()
		t = inp()
		m = [99999999999] * (n + 2)
		for i in range(k):
			m[a[i]] = t[i]
		for i in range(n):
			m[i] = min(m[i], m[i + 1] + 1, m[i - 1] + 1)
		for i in range(n, 0, -1):
			m[i] = min(m[i], m[i - 1] + 1, m[i + 1] + 1)
		print(*m[1:-1])
import os, sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import heapq

def solve(pos, temps, n, k, ans):
	left = []
	right = []
	acs = {}
	for i in range(k):
		(t, a) = (temps[i], pos[i])
		heapq.heappush(right, (t + a, a))
		acs[a] = t
	final = []
	for i in range(1, n + 1):
		if i in acs.keys():
			heapq.heappush(left, acs[i] - i)
		if not left:
			left_min = float('inf')
		else:
			left_min = heapq.heappop(left)
			heapq.heappush(left, left_min)
		right_min = float('inf')
		while right:
			(right_min, index) = heapq.heappop(right)
			if index >= i:
				heapq.heappush(right, (right_min, index))
				break
			right_min = float('inf')
		final.append(min(i + left_min, right_min - i))
	ans.append(' '.join(map(str, final)))

def main():
	t = int(input())
	ans = []
	for i in range(t):
		s = input()
		(n, k) = map(int, input().split())
		pos = list(map(int, input().split()))
		temps = list(map(int, input().split()))
		solve(pos, temps, n, k, ans)
	print('\n'.join(ans))
main()


import sys
input = sys.stdin.readline
T = int(input())
for iiii in range(T):
	ppp = input()
	(n, k) = map(int, input().split())
	a = [0] + list(map(int, input().split())) + [n + 1]
	t = [10 ** 17] + list(map(int, input().split())) + [10 ** 17]
	dic = {}
	for i in range(1, k + 1):
		dic[a[i]] = i
	l = [10 ** 18] * (n + 2)
	r = [10 ** 18] * (n + 2)
	for i in range(1, n + 1):
		l[i] = l[i - 1] + 1
		if i in dic:
			j = dic[i]
			l[i] = min(l[i], t[j])
	for i in range(n, 0, -1):
		r[i] = r[i + 1] + 1
		if i in dic:
			j = dic[i]
			r[i] = min(r[i], t[j])
	ans = []
	for i in range(1, n + 1):
		sub = min(l[i], r[i])
		ans.append(sub)
	print(*ans)


Q = int(input())
for qi in range(Q):
	input()
	(N, K) = map(int, input().split())
	A = list(map(int, input().split()))
	T = list(map(int, input().split()))
	for i in range(K):
		A[i] = (A[i] - 1, T[i])
	A.sort()
	import heapq
	prev = []
	ans = [float('inf') for i in range(N)]
	j = 0
	for i in range(N):
		if j < K and A[j][0] == i:
			heapq.heappush(prev, A[j][1] - i)
			j += 1
		if prev:
			ans[i] = prev[0] + i
	prev = []
	j = K - 1
	for i in range(N - 1, -1, -1):
		if j >= 0 and A[j][0] == i:
			heapq.heappush(prev, A[j][1] - (N - 1 - i))
			j -= 1
		if prev:
			ans[i] = min(ans[i], prev[0] + (N - 1 - i))
	for x in ans:
		print(x, end=' ')
	print()


import sys, os, io
import math, bisect, operator
(inf, mod) = (float('inf'), 10 ** 9 + 7)
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, defaultdict
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
Neo = lambda : list(map(int, input().split()))
(test,) = Neo()
for i in range(test):
	s = input()
	(n, k) = Neo()
	a = Neo()
	t = Neo()
	H = [(i, j - 1) for (i, j) in zip(t, a)]
	heapify(H)
	ans = [inf] * n
	for i in range(k):
		ans[a[i] - 1] = t[i]
	t = ans[:]
	while H:
		(temp, pos) = heappop(H)
		if pos - 1 >= 0:
			if temp + 1 < t[pos - 1]:
				t[pos - 1] = temp + 1
				heappush(H, (temp + 1, pos - 1))
		if pos + 1 < n:
			if temp + 1 < t[pos + 1]:
				t[pos + 1] = temp + 1
				heappush(H, (temp + 1, pos + 1))
	print(*t)


for i in range(int(input())):
	input()
	(n, a) = map(int, input().split(' '))
	idx = list(map(int, input().split(' ')))
	temprature = list(map(int, input().split(' ')))
	ans = [100000000000] * n
	helper = []
	for (i, t) in zip(idx, temprature):
		ans[i - 1] = t
		helper.append([t, i - 1])
	helper.sort()
	for (t, i) in helper:
		l = i - 1
		r = i + 1
		notfound = True
		while r < n and notfound:
			if ans[r] > t + r - i:
				ans[r] = t + r - i
			else:
				notfound = False
			r += 1
		notfound = True
		while l >= 0 and notfound:
			if ans[l] > t + i - l:
				ans[l] = t + i - l
			else:
				notfound = False
			l -= 1
	print(' '.join(map(str, ans)))


from __future__ import division, print_function
import itertools
import math
import sys
from atexit import register
if sys.version_info[0] < 3:
	from io import BytesIO as stream
else:
	from io import StringIO as stream
if sys.version_info[0] < 3:

	class dict(dict):

		def items(self):
			return dict.iteritems(self)

		def keys(self):
			return dict.iterkeys(self)

		def values(self):
			return dict.itervalues(self)
	input = raw_input
	range = xrange
	filter = itertools.ifilter
	map = itertools.imap
	zip = itertools.izip

def sync_with_stdio(sync=True):
	global input, flush
	if sync:
		flush = sys.stdout.flush
	else:
		sys.stdin = stream(sys.stdin.read())
		input = lambda : sys.stdin.readline().rstrip('\r\n')
		sys.stdout = stream()
		register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
mod = 1000000007
for _ in range(int(input())):
	z = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	ans = [math.inf] * n
	for i in range(k):
		ans[a[i] - 1] = t[i]
	val = ans.copy()
	c1 = 0
	c2 = 0
	fl = math.inf
	fr = math.inf
	for i in range(n):
		fl = min(fl + 1, ans[i])
		ans[i] = fl
	for i in range(n - 1, -1, -1):
		fr = min(fr + 1, ans[i])
		ans[i] = fr
	print(*ans)


import math, bisect, heapq, sys
p = 10 ** 10 + 7
for t in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	ans = []
	done = set()
	store = {}
	(left, right) = (0, [])
	for i in range(k):
		store[a[i]] = b[i]
		right.append((a[i] + b[i], a[i]))
	right.sort()
	right = right[::-1]
	ac = set(a)
	for i in range(1, n + 1):
		z = p
		if i in ac:
			done.add(i)
			temp = store[i]
			if left == 0:
				left = (store[i], i)
			if left[0] + i - left[1] > temp:
				left = (store[i], i)
		if left:
			z = left[0] + i - left[1]
		while right:
			x = right.pop()
			if x[1] not in done:
				z = min(z, x[0] - i)
				right.append(x)
				break
		ans.append(z)
	print(*ans)


for _ in range(int(input())):
	emp = input()
	(n, k) = map(int, input().split())
	a = [int(i) for i in input().split()]
	t = [int(i) for i in input().split()]
	ans = [float('inf')] * n
	for i in range(k):
		ans[a[i] - 1] = t[i]
	for i in range(1, n):
		ans[i] = min(ans[i - 1] + 1, ans[i])
	for i in range(n - 2, -1, -1):
		ans[i] = min(ans[i], ans[i + 1] + 1)
	print(*ans)


from sys import stdin
input = stdin.readline

def im():
	return map(int, input().split())

def ii():
	return int(input())

def il():
	return list(map(int, input().split()))

def ins():
	return input()[:-1]
for _ in range(ii()):
	_ = input()
	(n, k) = im()
	pos = il()
	temp = il()
	lis = [[pos[i], temp[i]] for i in range(k)]
	lis.sort(key=lambda x: x[0])
	left = [float('inf')] * n
	right = [float('inf')] * n
	curr = float('inf')
	ac = 0
	for i in range(n):
		if ac < len(temp) and lis[ac][0] - 1 == i:
			curr = min(curr, lis[ac][1])
			ac += 1
		left[i] = curr
		curr += 1
	curr = float('inf')
	ac = k - 1
	for i in range(n - 1, -1, -1):
		if ac >= 0 and lis[ac][0] - 1 == i:
			curr = min(curr, lis[ac][1])
			ac -= 1
		right[i] = curr
		curr += 1
	ans = [min(left[i], right[i]) for i in range(n)]
	print(*ans)


for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	ans = [float('inf')] * n
	for (i, j) in zip(a, t):
		ans[i - 1] = j
	a.sort()
	for i in range(a[0], n):
		ans[i] = min(ans[i], ans[i - 1] + 1)
	for i in range(n - 2, -1, -1):
		ans[i] = min(ans[i], ans[i + 1] + 1)
	print(*ans)


def read_line():
	return [int(x) for x in input().split()]

def read_int():
	return int(input())

def solve():
	_ = read_line()
	(n, k) = read_line()
	a = [1e+18 for x in range(n)]
	pos = read_line()
	temp = read_line()
	for i in range(k):
		a[pos[i] - 1] = temp[i]
	for i in range(1, n):
		a[i] = min(a[i], a[i - 1] + 1)
	i = n - 2
	while i >= 0:
		a[i] = min(a[i], a[i + 1] + 1)
		i -= 1
	print(*a)
t = read_int()
while t > 0:
	solve()
	t -= 1


t = int(input())
for _ in range(t):
	f = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	temp = [-1 for i in range(n)]
	for i in range(k):
		temp[a[i] - 1] = b[i]
	dp = [float('inf') for i in range(n)]
	ans = float('inf')
	for i in range(n):
		if temp[i] != -1:
			ans = min(ans + 1, temp[i])
			dp[i] = ans
			continue
		dp[i] = ans + 1
		ans += 1
	ans = float('inf')
	for i in range(n - 1, -1, -1):
		if temp[i] != -1:
			ans = min(ans + 1, temp[i])
			dp[i] = min(dp[i], ans)
			continue
		dp[i] = min(dp[i], ans + 1)
		ans += 1
	print(*dp)


from collections import deque
t = int(input())
res = []
for _ in range(t):
	l = input()
	(n, k) = list(map(int, input().split()))
	positions = list(map(int, input().split()))
	values = list(map(int, input().split()))
	land = []
	temp_dict = {}
	for i in range(len(positions)):
		temp_dict[positions[i]] = values[i]
	if n == 1:
		res.append([values[0]])
		continue
	for i in range(n):
		if i + 1 in temp_dict:
			land.append(temp_dict[i + 1])
		else:
			land.append(float('inf'))
	for i in range(1, len(land)):
		land[i] = min(land[i - 1] + 1, land[i])
	for i in range(len(land) - 2, -1, -1):
		land[i] = min(land[i], land[i + 1] + 1)
	res.append(land)
for r in res:
	for val in r:
		print(val, end=' ')
	print()


import sys
input = sys.stdin.readline

def miis():
	return map(int, input().split())
inf = 10 ** 10
for _ in range(int(input())):
	input()
	(n, k) = miis()
	c1 = miis()
	c2 = miis()
	c = [inf for i in range(n)]
	for (i, j) in zip(c1, c2):
		c[i - 1] = j
	(l, r) = ([inf for i in range(n)], [inf for i in range(n)])
	for i in range(n):
		if i:
			l[i] = l[i - 1] + 1
		l[i] = min(l[i], c[i])
	for i in range(n - 1, -1, -1):
		if i != n - 1:
			r[i] = r[i + 1] + 1
		r[i] = min(r[i], c[i])
	for (i, j) in zip(l, r):
		print(min(i, j), end=' ')
	print()


for _ in range(int(input())):
	x = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	l = [0] * n
	r = [0] * n
	c = [10000000000] * n
	for i in range(k):
		c[a[i] - 1] = t[i]
	p = 10000000000
	for i in range(n):
		p = min(p + 1, c[i])
		l[i] = p
	p = 10000000000
	for i in range(n - 1, -1, -1):
		p = min(p + 1, c[i])
		r[i] = p
	for i in range(n):
		print(min(l[i], r[i]), end=' ')
	print()


for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	(a, b) = (list(map(lambda x: int(x) - 1, input().split())), list(map(int, input().split())))
	arr = [2 * 10 ** 9 for _ in range(n)]
	for i in range(k):
		arr[a[i]] = b[i]
	for i in range(1, n):
		arr[i] = min(arr[i], arr[i - 1] + 1)
	for i in range(n - 2, -1, -1):
		arr[i] = min(arr[i], arr[i + 1] + 1)
	print(*arr)


from heapq import heapify, heappush, heappop, heappushpop
from collections import defaultdict as dd, deque as dq, Counter as C
from math import factorial as f, ceil, gcd, sqrt, log
from bisect import bisect_left as bl, bisect_right as br
from itertools import combinations as c, permutations as p
from math import factorial as f, ceil, gcd, sqrt, log
from sys import stdin, stdout
input = stdin.readline
mp = lambda : map(int, input().split())
it = lambda : int(input())
ls = lambda : list(input().strip())
mt = lambda r: [ls() for _ in range(r)]
lcm = lambda a, b: a * b // gcd(a, b)

def fibo_n(n):
	return ((1 + sqrt(5)) / 2) ** n / sqrt(5)
mod = 10 ** 9 + 7
for _ in range(it()):
	input()
	(a, b) = mp()
	l = list(mp())
	m = list(mp())
	temp = [0] * (a + 1)
	mn = float('inf')
	mx = -float('inf')
	for i in range(b):
		temp[l[i]] = m[i]
		mn = min(l[i], mn)
		mx = max(l[i], mx)
	ans1 = [0] * (a + 1)
	prev = float('inf')
	for j in range(mn, mx + 1):
		if temp[j]:
			if temp[j] < prev:
				prev = temp[j]
		ans1[j] = prev
		prev += 1
	ans2 = [0] * (a + 1)
	prev1 = float('inf')
	for k in range(mx, mn - 1, -1):
		if temp[k]:
			if temp[k] < prev1:
				prev1 = temp[k]
		ans2[k] = prev1
		prev1 += 1
	ans = [0] * (a + 1)
	for re in range(mn, mx + 1):
		ans[re] = min(ans1[re], ans2[re])
	for el in range(mx + 1, a + 1):
		ans[el] = ans[el - 1] + 1
	for ele in range(mn - 1, -1, -1):
		ans[ele] = ans[ele + 1] + 1
	print(*ans[1:])


t = int(input())
for j in range(t):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	tmp = list(zip(a, t))
	Air = []
	ans = []
	tmp.sort()
	for i in range(k):
		while len(Air) > 0 and Air[len(Air) - 1][1] >= abs(Air[len(Air) - 1][0] - tmp[i][0]) + tmp[i][1]:
			Air.pop()
		if len(Air) > 0 and Air[len(Air) - 1][1] + abs(Air[len(Air) - 1][0] - tmp[i][0]) <= tmp[i][1]:
			continue
		Air.append(tmp[i])
	Air.sort(reverse=True)
	k = len(Air) - 1
	for i in range(1, n + 1):
		while k > 0:
			if abs(i - Air[k][0]) + Air[k][1] >= abs(i - Air[k - 1][0]) + Air[k - 1][1]:
				k = k - 1
				Air.pop()
			else:
				break
		ans.append(abs(i - Air[k][0]) + Air[k][1])
	print(*ans)


from sys import *
from collections import defaultdict as dd
from collections import deque
from math import *
from bisect import *

def sinp():
	return input()

def inp():
	return int(sinp())

def minp():
	return map(int, sinp().split())

def linp():
	return list(minp())

def strl():
	return list(sinp())

def pr(x):
	print(x)
mod = int(1000000000.0 + 7)
for _ in range(inp()):
	sinp()
	(n, k) = minp()
	a = linp()
	t = linp()
	res_1 = [maxsize for i in range(n + 2)]
	res_2 = [maxsize for i in range(n + 2)]
	for i in range(k):
		res_1[a[i]] = t[i] - a[i]
		res_2[a[i]] = t[i] + a[i]
	for i in range(1, n + 1):
		res_1[i] = min(res_1[i], res_1[i - 1])
	for i in range(n, 0, -1):
		res_2[i] = min(res_2[i], res_2[i + 1])
	for i in range(1, n + 1):
		print(min(res_1[i] + i, res_2[i] - i), end=' ')
	print()


from sys import stdin
input = stdin.readline
rn = lambda : int(input())
rns = lambda : map(int, input().split())
rl = lambda : list(map(int, input().split()))
rs = lambda : input().strip()
YN = lambda x: print('YES') if x else print('NO')
mod = 10 ** 9 + 7
for _ in range(rn()):
	input()
	(n, k) = rns()
	a = rl()
	t = rl()
	data = sorted(zip(a, t))
	lm = [float('inf')]
	i = 0
	for j in range(n):
		if i < k and data[i][0] == j + 1:
			lm.append(min(lm[-1] + 1, data[i][1]))
			i += 1
		else:
			lm.append(lm[-1] + 1)
	lm.pop(0)
	rm = [float('inf')]
	i = k - 1
	for j in range(n - 1, -1, -1):
		if data[i][0] == j + 1:
			rm.append(min(rm[-1] + 1, data[i][1]))
			i -= 1
		else:
			rm.append(rm[-1] + 1)
	rm.reverse()
	rm.pop()
	ans = []
	for i in range(n):
		ans.append(min(rm[i], lm[i]))
	print(*ans)


import sys
input = sys.stdin.readline
inf = 10000000000
t = int(input())
for u in range(t):
	p = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	s = []
	for i in range(k):
		s.append([a[i] - 1, t[i]])
	s.sort()
	left = [10000000000 for i in range(n)]
	tt = 10000000000
	tk = -1
	j = 0
	for i in range(n):
		if j < k:
			if s[j][0] > i:
				if tk != -1:
					tt += 1
					left[i] = tt
			elif tk == -1 or s[j][1] < tt + 1:
				tt = s[j][1]
				tk = j
				j += 1
				left[i] = tt
			else:
				tt += 1
				left[i] = tt
				j += 1
		else:
			tt += 1
			left[i] = tt
	right = [10000000000 for i in range(n)]
	tt = 10000000000
	tk = -1
	j = k - 1
	for i in range(n - 1, -1, -1):
		if j > -1:
			if s[j][0] < i:
				if tk != -1:
					tt += 1
					right[i] = tt
			elif tk == -1 or s[j][1] < tt + 1:
				tt = s[j][1]
				tk = j
				j -= 1
				right[i] = tt
			else:
				tt += 1
				right[i] = tt
				j -= 1
		else:
			tt += 1
			right[i] = tt
	for i in range(n):
		print(min(left[i], right[i]), end=' ')
	print()


import sys, os.path
from collections import *
from heapq import *
from copy import *
mod = 10 ** 9 + 7
read = lambda : map(int, input().split())
inf = float('inf')
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for t in range(int(input())):
	emp = input()
	(n, k) = read()
	a = list(read())
	t = list(read())
	c = [inf] * n
	for i in range(k):
		c[a[i] - 1] = t[i]
	l = [inf] * n
	r = [inf] * n
	p = inf
	for i in range(n):
		p = min(p + 1, c[i])
		l[i] = p
	for i in range(n - 1, -1, -1):
		p = min(p + 1, c[i])
		r[i] = p
	res = []
	for i in range(n):
		res.append(min(l[i], r[i]))
	print(*res)


import sys, math, bisect
sys.setrecursionlimit(10 ** 5)
from random import randint
inf = float('inf')
mod = 10 ** 9 + 7

def lcm(a, b):
	return int(a / math.gcd(a, b) * b)

def gcd(a, b):
	return int(math.gcd(a, b))

def tobinary(n):
	return bin(n)[2:]

def binarySearch(a, x):
	i = bisect.bisect_left(a, x)
	if i != len(a) and a[i] == x:
		return i
	else:
		return -1

def lowerBound(a, x):
	i = bisect.bisect_left(a, x)
	if i:
		return i - 1
	else:
		return -1

def upperBound(a, x):
	i = bisect.bisect_right(a, x)
	if i != len(a) + 1 and a[i - 1] == x:
		return i - 1
	else:
		return -1

def primesInRange(n):
	ans = []
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, n + 1):
		if prime[p]:
			ans.append(p)
	return ans

def primeFactors(n):
	factors = []
	while n % 2 == 0:
		factors.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			factors.append(i)
			n = n // i
	if n > 2:
		factors.append(n)
	return factors

def isPrime(n, k=5):
	if n < 2:
		return True
	for i in range(0, k):
		a = randint(1, n - 1)
		if pow(a, n - 1, n) != 1:
			return False
	return True
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop, heapify
import string
for _ in range(int(input())):
	vain = input()
	(n, k) = map(int, input().split())
	pos = list(map(int, input().split()))
	temp = list(map(int, input().split()))
	left = [1e+19] * n
	right = [1e+19] * n
	for i in range(k):
		left[pos[i] - 1] = temp[i]
		right[pos[i] - 1] = temp[i]
	for i in range(1, n):
		left[i] = min(left[i], left[i - 1] + 1)
	for i in range(n - 2, -1, -1):
		right[i] = min(right[i], right[i + 1] + 1)
	ans = [0] * n
	for i in range(n):
		ans[i] = min(left[i], right[i])
	print(*ans)


import collections
import math
import sys
from functools import reduce
import decimal
sys.setrecursionlimit(10 ** 9)
decimal.getcontext().rounding = decimal.ROUND_HALF_UP
graphDict = collections.defaultdict
queue = collections.deque()

class Graphs:

	def __init__(self):
		self.graph = graphDict(list)

	def add_edge(self, u, v):
		self.graph[u].append(v)

	def dfs_utility(self, nodes, visited_nodes):
		visited_nodes.add(nodes)
		for neighbour in self.graph[nodes]:
			if neighbour not in visited_nodes:
				self.dfs_utility(neighbour, visited_nodes)
			else:
				return neighbour

	def dfs(self, node):
		Visited = set()
		self.dfs_utility(node, Visited)

	def bfs(self, node):
		visited = set()
		if node not in visited:
			queue.append(node)
			visited.add(node)
		while queue:
			parent = queue.popleft()
			print(parent)
			for item in self.graph[parent]:
				if item not in visited:
					queue.append(item)
					visited.add(item)

def rounding(n):
	return int(decimal.Decimal(f'{n}').to_integral_value())

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def inp():
	return sys.stdin.readline().strip()

def map_inp(v_type):
	return map(v_type, inp().split())

def list_inp(v_type):
	return list(map_inp(v_type))
for _ in range(int(inp())):
	inp()
	(n, k) = map_inp(int)
	index = list_inp(int)
	value = list_inp(int)
	arr = [float('inf') for item in range(n)]
	for (i, val) in zip(index, value):
		arr[i - 1] = val
	for i in range(1, n):
		arr[i] = min(arr[i], arr[i - 1] + 1)
	for i in range(n - 2, -1, -1):
		arr[i] = min(arr[i], arr[i + 1] + 1)
	print(*arr)


from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from heapq import heappush, heappop
import math
import io
import os
import math
import bisect

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	temp = n * n
	temp %= m
	return temp

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def digits(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def ceil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def mapin():
	return [int(x) for x in input().split()]

def sapin():
	return [int(x) for x in input()]
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
q = int(input())
m = 1000000007
mm = ['YES', 'NO']
for _ in range(q):
	z = input()
	(n, k) = mapin()
	a = mapin()
	t = mapin()
	lo = [1e+20] * n
	ri = [1e+20] * n
	temp = 1e+20
	for i in range(k):
		lo[a[i] - 1] = -a[i] + t[i]
		ri[a[i] - 1] = t[i] + a[i]
	for i in range(n):
		temp = min(temp, lo[i])
		lo[i] = temp
	temp = 1e+20
	for ii in range(n):
		i = n - ii - 1
		temp = min(temp, ri[i])
		ri[i] = temp
	ans = [0] * n
	for i in range(n):
		ans[i] = min(lo[i] + (i + 1), ri[i] - i - 1)
	print(*ans)


import sys
N = int(200000.0 + 5)
sys.setrecursionlimit(N)

def charming():
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	pos = [int(1 << 32)] * (n + 5)
	for i in range(k):
		pos[a[i]] = min(pos[a[i]], b[i])
	res = [int(1 << 32)] * (n + 5)
	mn = int(1 << 32)
	for i in range(1, n + 1):
		if pos[i] < mn:
			mn = pos[i]
		res[i] = min(res[i], mn)
		mn += 1
	mn = int(1 << 32)
	for i in list(reversed(range(1, n + 1))):
		if pos[i] < mn:
			mn = pos[i]
		res[i] = min(res[i], mn)
		mn += 1
	for i in range(1, n + 1):
		print(res[i], end=' ')
	print()
for t in range(int(input())):
	charming()


import heapq, math
from collections import defaultdict, deque
from os import getcwd
import sys, os.path
if os.path.exists('D:\\CP\\programs\\input.txt'):
	sys.stdout = open('D:\\CP\\programs\\output.txt', 'w')
	sys.stdin = open('D:\\CP\\programs\\input.txt', 'r')
input = sys.stdin.readline

def find(i, pos, temp):
	res = 9999999999
	for j in range(len(pos)):
		res = min(res, temp[j] + abs(pos[j] - i - 1))
	return res
test = int(input())
for _ in range(test):
	input()
	(n, k) = map(int, input().split())
	pos = list(map(int, input().split()))
	temp = list(map(int, input().split()))
	res = [0] * n
	for i in range(k):
		res[pos[i] - 1] = temp[i]
	left = [i if i != 0 else 10 ** 15 + 1 for i in res]
	right = [i if i != 0 else 10 ** 15 for i in res]
	for i in range(1, n):
		left[i] = min(left[i - 1] + 1, left[i])
	for i in range(n - 2, -1, -1):
		right[i] = min(right[i + 1] + 1, right[i])
	for i in range(n):
		res[i] = min(left[i], right[i])
	print(*res)


import os
import sys
import math
import collections
import functools
import itertools
import heapq
import bisect
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcde'
MOD = 10 ** 9 + 7
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def INT():
	return int(input())

def STR():
	return input()

def INTs():
	return tuple(map(int, input().split()))

def ARRINT():
	return [int(i) for i in input().split()]

def ARRSTR():
	return [i for i in input().split()]
for _ in range(INT()):
	input()
	(n, k) = INTs()
	a = ARRINT()
	t = ARRINT()
	memo = {idx - 1: temp for (idx, temp) in zip(a, t)}
	res = [float('inf')] * n
	cur = float('inf')
	for i in range(n):
		if i in memo:
			cur = min(cur, memo[i])
		res[i] = cur
		cur += 1
	cur = float('inf')
	for i in range(n - 1, -1, -1):
		if i in memo:
			cur = min(cur, memo[i])
		res[i] = min(res[i], cur)
		cur += 1
	print(*res)


q = int(input())
for _ in range(q):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	d = [0] * (n + 1)
	for i in range(len(a)):
		d[a[i]] = t[i]
	l = [0] * (n + 2)
	l[0] = 10 ** 10
	r = [0] * (n + 2)
	r[-1] = 10 ** 10
	for i in range(1, n + 1):
		if d[i] != 0:
			l[i] = min(l[i - 1] + 1, d[i])
		else:
			l[i] = l[i - 1] + 1
	for i in range(n, 0, -1):
		if d[i] != 0:
			r[i] = min(r[i + 1] + 1, d[i])
		else:
			r[i] = r[i + 1] + 1
	for i in range(1, n + 1):
		print(min(l[i], r[i]), end=' ')
	print()


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
from bisect import bisect_left as lower_bound, bisect_right as upper_bound

def so():
	return int(input())

def st():
	return input()

def mj():
	return map(int, input().strip().split(' '))

def msj():
	return map(str, input().strip().split(' '))

def le():
	return list(map(int, input().split()))

def lebe():
	return list(map(int, input()))

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()

def joro(L):
	return ''.join(map(str, L))

def cheems(c, d):
	a = 1
	while d != 0:
		if d % 2 == 1:
			a *= c
		c = c * c
		d = d // 2
	return a

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def isprime(n):
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			return False
	return True

def npr(n, r):
	return factorial(n) // factorial(n - r) if n >= r else 0

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(n - r)) if n >= r else 0

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def tir(a, b, c):
	if 0 == c:
		return 1
	if len(a) <= b:
		return 0
	if c != -1:
		return tir(a, 1 + b, c + a[b]) or tir(a, b + 1, c - a[b]) or tir(a, 1 + b, c)
	else:
		return tir(a, 1 + b, a[b]) or tir(a, b + 1, -a[b]) or tir(a, 1 + b, -1)

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = -1
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = mid
			break
	return ans

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = max(sum_so_far, current_sum)
	return sum_so_far

def wubu(m):
	import math as my
	d = 0
	while not m % 2:
		m = m // 2
		d = 1 + d
	for i in range(3, int(my.sqrt(m)) + 1, 2):
		while not m % i:
			m = m // i
			d = 1 + d
	return int(m > 1) + d

def pref(li):
	pref_sum = [0]
	for i in li:
		pref_sum.append(pref_sum[-1] + i)
	return pref_sum

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	li = []
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, len(prime)):
		if prime[p]:
			li.append(p)
	return li

def primefactors(n):
	factors = []
	while n % 2 == 0:
		factors.append(2)
		n //= 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0:
			factors.append(i)
			n //= i
	if n > 2:
		factors.append(n)
	return factors

def read():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def tr(n):
	return n * (n + 1) // 2

def bro(q, r, c):
	(bec, pot, h) = (0, 0, 0)
	ds = []
	bs = []
	es = []
	for i in range(c):
		while h < len(r) and r[h] <= q[i][0]:
			pot = 0
			ds.clear()
			bs.clear()
			h = 1 + h
	for i in range(c):
		ds.append(q[i][1])
		es.append(q[i][1])
	ds = list(set(ds))
	for i in ds:
		bs.append(es.count(i))
	print(bs)
	for i in range(c):
		if q[i][0] > r[h - 1]:
			bec = bec + pot - bs[i]
			pot = 1 + pot
	return bec

def iu():
	import sys
	input = sys.stdin.readline
	import math as my
	import bisect as op
	c = 0
	t = st()
	(m, q) = mj()
	L = le()
	M = le()
	bec = [sys.maxsize] * m
	for i in range(q):
		bec[L[i] - 1] = M[i]
	r = 1
	while r < m:
		bec[r] = min(bec[r], 1 + bec[r - 1])
		r = 1 + r
	op = m - 2
	while op >= 0:
		bec[op] = min(bec[op], 1 + bec[1 + op])
		op = op - 1
	print(*bec)

def main():
	for i in range(so()):
		iu()
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


from collections import Counter
import math

def solve():
	n = input()
	(n, k) = list(map(int, input().split()))
	conds = []
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	inf = 100000000000000000
	c = [inf for i in range(n)]
	ans_l = [0 for i in range(n)]
	ans_r = [0 for i in range(n)]
	for i in range(k):
		c[a[i - 1] - 1] = t[i - 1]
	ans_l[0] = c[0]
	for i in range(1, n):
		ans_l[i] = min(ans_l[i - 1] + 1, c[i])
	ans_r[-1] = c[-1]
	for i in range(n - 2, -1, -1):
		ans_r[i] = min(ans_r[i + 1] + 1, c[i])
	for i in range(n):
		print(min(ans_l[i], ans_r[i]), end=' ')
	print()
t = int(input())
while t:
	t -= 1
	solve()


from collections import deque
import heapq
from math import sqrt
import operator
from operator import itemgetter
import sys
import copy
inf_var = 0
if inf_var == 1:
	inf = open('input.txt', 'r')
else:
	inf = sys.stdin
input = inf.readline

def read_one_int():
	return int(input().rstrip('\n'))

def read_one_float():
	return float(input().rstrip('\n'))

def read_list_of_ints():
	res = [int(val) for val in input().rstrip('\n').split(' ')]
	return res

def read_str():
	return input().rstrip()

def main():
	tests = read_one_int()
	inf = 10 ** 10
	for _ in range(tests):
		_ = read_str()
		(n, k) = read_list_of_ints()
		positions = read_list_of_ints()
		temperatures = read_list_of_ints()
		n_temp = [inf] * n
		for i in range(k):
			n_temp[positions[i] - 1] = temperatures[i]
		left_res = [inf] * n
		right_res = [inf] * n
		cur_res = inf
		for i in range(n):
			cur_res = min(cur_res + 1, n_temp[i])
			left_res[i] = cur_res
		cur_res = inf
		for i in range(n - 1, -1, -1):
			cur_res = min(cur_res + 1, n_temp[i])
			right_res[i] = cur_res
		res = []
		for i in range(n):
			res.append(min(left_res[i], right_res[i]))
		print(' '.join(map(str, res)))
main()


import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def LI1():
	return list(map(int1, sys.stdin.readline().split()))

def LLI1(rows_number):
	return [LI1() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline().rstrip()
inf = 10 ** 16
md = 10 ** 9 + 7

def solve():

	def toll(air):
		res = [inf] * n
		mn = inf
		for (i, t) in enumerate(air):
			if t:
				mn = min(mn, t - i)
			res[i] = mn + i
		return res
	SI()
	(n, k) = LI()
	aa = LI1()
	tt = LI()
	air = [0] * n
	for (a, t) in zip(aa, tt):
		air[a] = t
	ll = toll(air)
	rr = toll(air[::-1])[::-1]
	ans = []
	for (l, r) in zip(ll, rr):
		ans.append(min(l, r))
	print(*ans)
for testcase in range(II()):
	solve()


def solve(arr):
	n = len(arr)
	inf = 10 ** 18
	dp = [inf for x in range(n)]
	j = 0
	if arr[0] == 1:
		dp[0] = t[0]
		j = j + 1
	for i in range(1, n):
		if arr[i] == 0:
			dp[i] = min(dp[i], dp[i - 1] + 1)
		else:
			dp[i] = min(dp[i - 1] + 1, t[i])
			j = j + 1
	return dp

def solve1(arr):
	n = len(arr)
	inf = 10 ** 18
	dp = [inf for x in range(n)]
	j = len(t) - 1
	if arr[n - 1] == 1:
		dp[n - 1] = t[n - 1]
		j = j - 1
	for i in range(n - 2, -1, -1):
		if arr[i] == 0:
			dp[i] = min(dp[i], dp[i + 1] + 1)
		else:
			dp[i] = min(dp[i + 1] + 1, t[i])
			j = j - 1
	return dp
for _ in range(int(input())):
	b = input()
	(n, k) = map(int, input().split(' '))
	arr = [0] * n
	c = [int(w) for w in input().split(' ')]
	for item in c:
		arr[item - 1] = 1
	x = [int(w) for w in input().split(' ')]
	t = dict()
	for i in range(k):
		t[c[i] - 1] = x[i]
	dp1 = solve(arr)
	dp2 = solve1(arr)
	ans = []
	for i in range(n):
		ans.append(min(dp1[i], dp2[i]))
	for item in ans:
		print(item, end=' ')
	print('')


t = int(input())
for _ in range(t):
	l = input()
	(n, k) = map(int, input().split())
	bunch = []
	l = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	for i in range(k):
		bunch.append([l[i], l2[i]])
	bunch.sort(key=lambda x: x[0])
	ind = 0
	res = [10 ** 9 * 2 + 1 for _ in range(n)]
	for i in range(bunch[0][0] - 1, n):
		if i == 0:
			res[i] = bunch[0][1]
			ind += 1
		elif ind < k and bunch[ind][0] - 1 == i:
			res[i] = min(res[i - 1] + 1, bunch[ind][1])
			ind += 1
		else:
			res[i] = res[i - 1] + 1
	ind = k - 1
	for i in range(bunch[-1][0] - 1, -1, -1):
		if i == n - 1:
			res[i] = min(res[i], bunch[ind][1])
			ind -= 1
		elif ind >= 0 and bunch[ind][0] - 1 == i:
			res[i] = min(res[i + 1] + 1, bunch[ind][1], res[i])
			ind -= 1
		else:
			res[i] = min(res[i + 1] + 1, res[i])
	print(' '.join(map(str, res)))


from collections import deque, Counter
from collections import defaultdict as dfd
from bisect import bisect, bisect_left
from math import sqrt, gcd, ceil, factorial
from heapq import heapify, heappush, heappop
MOD = 10 ** 9 + 7
inf = float('inf')
ans_ = []

def nin():
	return int(input())

def ninf():
	return int(file.readline())

def st():
	return input().strip()

def stf():
	return file.readline().strip()

def read():
	return list(map(int, input().strip().split()))

def readf():
	return list(map(int, file.readline().strip().split()))

def readfl():
	return list(map(float, input().strip().split()))

def readffl():
	return list(map(float, file.readline().strip().split()))

def solve():
	for _ in range(nin()):
		st()
		(n, k) = read()
		a = read()
		t = read()
		ans = [inf] * n
		for i in range(k):
			ans[a[i] - 1] = t[i]
		x = ans[0]
		y = 0
		for i in range(n):
			if ans[i] < x + y:
				x = ans[i]
				y = 1
			else:
				ans[i] = x + y
				y += 1
		for i in range(n - 1, -1, -1):
			if ans[i] < x + y:
				x = ans[i]
				y = 1
			else:
				ans[i] = x + y
				y += 1
		ans_.append(ans)
solve()
for i in ans_:
	print(*i)


q = int(input())
bigTemp = 2 * 10 ** 9
for _ in range(q):
	input()
	(n, k) = map(int, input().split())
	airCons = list(map(int, input().split()))
	airCons = [item - 1 for item in airCons]
	airConTemps = list(map(int, input().split()))
	dictionary = dict(zip(airCons, airConTemps))
	leftTemps = [bigTemp] * n
	rightTemps = [bigTemp] * n
	temperature = bigTemp
	for i in range(n):
		if i in dictionary:
			temperature = min(temperature, dictionary[i])
		leftTemps[i] = temperature
		temperature += 1
	temperature = bigTemp
	for i in range(n - 1, -1, -1):
		if i in dictionary:
			temperature = min(temperature, dictionary[i])
		rightTemps[i] = temperature
		temperature += 1
	answer = [0] * n
	for i in range(n):
		answer[i] = min(leftTemps[i], rightTemps[i])
	print(*answer)


for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	(a, b) = zip(*sorted(zip(a, b)))
	x = [-1] * n
	for i in range(k):
		x[a[i] - 1] = b[i]
	pre = [float('inf')] * n
	start = a[0] - 1
	mi = float('inf')
	mii = -1
	for i in range(start, n):
		if x[i] != -1:
			if mi > x[i]:
				mi = x[i]
				mii = i
		pre[i] = min(pre[i], mi)
		mi += 1
	suf = [float('inf')] * n
	start = a[-1] - 1
	mi = float('inf')
	mii = -1
	for i in range(start, -1, -1):
		if x[i] != -1:
			if mi > x[i]:
				mi = x[i]
				mii = i
		suf[i] = min(suf[i], mi)
		mi += 1
	res = []
	for i in range(n):
		res.append(min(pre[i], suf[i]))
	print(*res)


import sys
input = sys.stdin.readline
INF = 1101101101

def main():
	input()
	(N, K) = map(int, input().split())
	A = list(map(int, input().split()))
	T = list(map(int, input().split()))
	Cell = [INF] * N
	for i in range(K):
		Cell[A[i] - 1] = T[i]
	MIN = INF
	L = [0] * N
	for i in range(N):
		if MIN > Cell[i] - i:
			MIN = Cell[i] - i
		L[i] = MIN + i
	MIN = INF
	R = [0] * N
	for i in range(N - 1, -1, -1):
		if MIN > Cell[i] - N + 1 + i:
			MIN = Cell[i] - N + 1 + i
		R[i] = MIN + N - 1 - i
	Ans = [0] * N
	for i in range(N):
		if L[i] < R[i]:
			Ans[i] = L[i]
		else:
			Ans[i] = R[i]
	print(*Ans)
T = int(input())
for _ in range(T):
	main()


for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	arr = [0] * (n + 2)
	for i in range(k):
		arr[a[i]] = t[i]
	l = arr
	r = l.copy()
	for i in range(1, n + 1):
		if r[i - 1] != 0 and (r[i] > r[i - 1] + 1 or r[i] == 0):
			r[i] = r[i - 1] + 1
	for i in range(n, 0, -1):
		if l[i + 1] != 0 and (l[i] > l[i + 1] + 1 or l[i] == 0):
			l[i] = l[i + 1] + 1
	for i in range(1, n + 1):
		v = l[i]
		if v == 0:
			v = r[i]
		elif r[i] != 0 and r[i] < v:
			v = r[i]
		print(v, end=' ')
	print()


import math
q = int(input())
for i in range(q):
	input()
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	r = []
	for j in range(k):
		r.append((t[j], a[j] - 1))
	r.sort(key=lambda tup: tup[0] + tup[1])
	r.reverse()
	mr = r[-1]
	ml = (math.inf, 0)
	ans = []
	for j in range(n):
		if r and r[-1][1] < j:
			passed = r.pop()
			if passed[0] + abs(passed[1] - j) < ml[0] + abs(ml[1] - j):
				ml = passed
			if r:
				mr = r[-1]
		while r and r[-1][1] < j:
			r.pop()
			if r:
				mr = r[-1]
		if not r:
			mr = (math.inf, 0)
		best = mr[0] + abs(mr[1] - j)
		if ml[0] + abs(ml[1] - j) < best:
			best = ml[0] + abs(ml[1] - j)
		ans.append(best)
	print(*ans)


from collections import *
from math import *
TT = int(input())
for y in range(TT):
	s = input()
	(n, m) = map(int, input().split())
	lst1 = list(map(int, input().split()))
	lst2 = list(map(int, input().split()))
	bas = 10 ** 10 + 1
	arr = [bas] * n
	for i in range(m):
		arr[lst1[i] - 1] = lst2[i]
	cur = arr[0]
	for i in range(1, n):
		cur = min(cur + 1, arr[i])
		arr[i] = cur
	cur = arr[-1]
	for i in range(n - 1, -1, -1):
		cur = min(cur + 1, arr[i])
		arr[i] = cur
	print(*arr)


t = int(input())
for tt in range(t):
	e = input()
	(n, k) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	t = [int(i) for i in input().split()]
	mb = [(a[i] - 1, t[i]) for i in range(k)]
	mb.sort()
	v = list()
	for i in range(k):
		if i == 0:
			v.append(mb[i])
		elif v[-1][1] + mb[i][0] - v[-1][0] > mb[i][1]:
			v.append(mb[i])
			j = len(v) - 1
			while v[j - 1][1] >= v[j][1] + v[j][0] - v[j - 1][0] and j >= 1:
				del v[j - 1]
				j -= 1
	ind = 0
	for i in range(n):
		if i < v[0][0]:
			g = v[0][1] + v[0][0] - i
		elif i > v[-1][0]:
			g = v[-1][1] + i - v[-1][0]
		elif i == v[ind][0]:
			g = v[ind][1]
			ind += 1
		else:
			g = min(v[ind - 1][1] + i - v[ind - 1][0], v[ind][1] - i + v[ind][0])
		print(g, end=' ')
	print()


from collections import defaultdict
import sys
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.buffer.readline())

def MI():
	return map(int, sys.stdin.buffer.readline().split())

def LI():
	return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def BI():
	return sys.stdin.buffer.readline().rstrip()

def SI():
	return sys.stdin.buffer.readline().rstrip().decode()

def li():
	return [int(i) for i in input().split()]

def lli(rows):
	return [li() for _ in range(rows)]

def si():
	return input()

def ii():
	return int(input())

def ins():
	return input().split()

def solve():
	SI()
	(n, k) = LI()
	a = LI()
	t = LI()
	st = [x for (_, x) in sorted(zip(a, t))]
	sa = sorted(a)
	pall = [(sa[0], st[0])]
	for i in range(1, k):
		while pall and sa[i] - pall[-1][0] <= pall[-1][1] - st[i]:
			pall.pop()
		pall.append((sa[i], st[i]))
	all = [pall[0]]
	for i in range(1, len(pall)):
		if pall[i][0] - all[-1][0] > pall[i][1] - all[-1][1]:
			all.append(pall[i])
	tn = []
	for i in range(1, n + 1):
		if i <= all[0][0]:
			tn.append(all[0][1] + all[0][0] - i)
			if i == n:
				return ' '.join(map(str, tn))
		else:
			break
	l = len(all)
	k = 1
	while k <= l - 1:
		tn.append(min(all[k - 1][1] + i - all[k - 1][0], all[k][1] + all[k][0] - i))
		if i == all[k][0]:
			k += 1
		i += 1
	for j in range(i, n + 1):
		tn.append(all[-1][1] + j - all[-1][0])
	return ' '.join(map(str, tn))

def main():
	for _ in range(II()):
		sys.stdout.write(str(solve()) + '\n')
main()


for _ in range(int(input())):
	st = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	store = [float('inf') for i in range(n + 2)]
	for i in range(k):
		store[a[i]] = t[i]
	l_r = [float('inf') for i in range(n + 2)]
	r_l = [float('inf') for i in range(n + 2)]
	for i in range(1, n + 1):
		value = min(store[i], l_r[i - 1] + 1)
		l_r[i] = value
	for i in range(n, -1, -1):
		value = min(store[i], r_l[i + 1] + 1)
		r_l[i] = value
	for i in range(1, n + 1):
		print(min(l_r[i], r_l[i]), end=' ')
	print()


import sys
input = sys.stdin.buffer.readline
import bisect

def main():
	t = int(input())
	INF = pow(10, 18)
	for _ in range(t):
		dummy = input()
		(n, k) = map(int, input().split())
		A = list(map(int, input().split()))
		T = list(map(int, input().split()))
		dic = {}
		for (aa, tt) in zip(A, T):
			dic[aa] = tt
		L = [INF] * (n + 2)
		for i in range(1, n + 1):
			if i in dic:
				L[i] = min(L[i - 1] + 1, dic[i])
			else:
				L[i] = L[i - 1] + 1
		R = [INF] * (n + 2)
		for i in reversed(range(1, n + 1)):
			if i in dic:
				R[i] = min(R[i + 1] + 1, dic[i])
			else:
				R[i] = R[i + 1] + 1
		ans = []
		for i in range(1, n + 1):
			ans.append(min(L[i], R[i]))
		print(*ans)
main()


from sys import stdin
input = stdin.buffer.readline

def func():
	big = 10000000000.0
	dpl = [big for x in range(n)]
	dpr = [big for x in range(n)]
	temp = [big for x in range(n)]
	for (pos, chill) in enumerate(a):
		temp[chill - 1] = t[pos]
	for i in range(n):
		dpl[i] = min(dpl[i - 1] + 1, temp[i])
	dpr[-1] = dpl[-1]
	for i in range(n - 2, -1, -1):
		dpr[i] = min(dpr[i + 1] + 1, temp[i])
	for i in range(n):
		temp[i] = min(dpr[i], dpl[i])
	print(*temp)
for _ in range(int(input())):
	input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	func()


import sys
input = sys.stdin.readline

def miis():
	return map(int, input().split())
for _ in range(int(input())):
	input()
	(n, k) = miis()
	c1 = miis()
	c2 = miis()
	c = [(j, i - 1) for (i, j) in zip(c1, c2)]
	c.sort()
	ans = [0 for i in range(n)]
	l = 0
	tmp = c[0][0]
	need = set()
	while l != k and c[l][0] == tmp:
		need.add(c[l][1])
		l += 1
	while need:
		newneed = set()
		while need:
			ind = need.pop()
			if ans[ind]:
				ans[ind] = min(tmp, ans[ind])
			else:
				ans[ind] = tmp
			if ind + 1 < n and (not ans[ind + 1]):
				newneed.add(ind + 1)
			if ind - 1 > -1 and (not ans[ind - 1]):
				newneed.add(ind - 1)
		need = need.union(newneed)
		tmp += 1
		while l != k and c[l][0] == tmp:
			if not ans[c[l][1]]:
				need.add(c[l][1])
			l += 1
	print(*ans)


from math import inf
for _ in range(int(input())):
	_ = input()
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	t = list(map(int, input().split()))
	T = [inf] * n
	for i in range(k):
		T[a[i] - 1] = t[i]
	l = [inf] * n
	for i in range(n):
		if i:
			l[i] = l[i - 1] + 1
		l[i] = min(l[i], T[i])
	r = [inf] * n
	for i in reversed(range(n)):
		if i + 1 < n:
			r[i] = r[i + 1] + 1
		r[i] = min(r[i], T[i])
	for i in range(n):
		T[i] = min(l[i], r[i])
	print(*T)