Polycarp has invited $n$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side.

Each friend is characterized by two values $h_i$ (their height) and $w_i$ (their width). On the photo the $i$-th friend will occupy a rectangle $h_i \times w_i$ (if they are standing) or $w_i \times h_i$ (if they are lying on the side).

The $j$-th friend can be placed in front of the $i$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $i$-th friend. Formally, at least one of the following conditions must be fulfilled:

$h_j < h_i$ and $w_j < w_i$ (both friends are standing or both are lying);

$w_j < h_i$ and $h_j < w_i$ (one of the friends is standing and the other is lying).

For example, if $n = 3$, $h=[3,5,3]$ and $w=[4,4,3]$, then:

the first friend can be placed in front of the second: $w_1 < h_2$ and $h_1 < w_2$ (one of the them is standing and the other one is lying);

the third friend can be placed in front of the second: $h_3 < h_2$ and $w_3 < w_2$ (both friends are standing or both are lying).

In other cases, the person in the foreground will overlap the person in the background.

Help Polycarp for each $i$ find any $j$, such that the $j$-th friend can be located in front of the $i$-th friend (i.e. at least one of the conditions above is fulfilled).

Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $i$ any other friend $j$ who can be located in front of him. Think about it as you need to solve $n$ separate independent subproblems.


-----Input-----

The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Then $t$ test cases follow.

The first line of each test case contains one integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the number of friends.

This is followed by $n$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $h_i$ and $w_i$ ($1 \leq h_i, w_i \leq 10^9$) — height and width of the $i$-th friend, respectively.

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


-----Output-----

For each test case output $n$ integers on a separate line, where the $i$-th number is the index of a friend that can be placed in front of the $i$-th. If there is no such friend, then output -1.

If there are several answers, output any.


-----Examples-----

Input
4
3
3 4
5 4
3 3
3
1 3
2 2
3 1
4
2 2
3 1
6 3
5 4
4
2 2
2 3
1 1
4 4
Output
-1 3 -1 
-1 -1 -1 
-1 -1 2 2 
3 3 -1 3


-----Note-----

The first test case is described in the statement.

In the third test case, the following answers are also correct:

$[-1, -1, 1, 2]$;

$[-1, -1, 1, 1]$;

$[-1, -1, 2, 1]$.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Data structures

Sorting

Amortized analysis


Solutions:
import sys

def main():
	res = ''
	input = sys.stdin.readline
	print = sys.stdout.write
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = []
		origin = []
		answer = {}
		sub_res = ''
		for i in range(n):
			(w, h) = map(int, input().split())
			if w > h:
				origin.append((w, h))
				a.append((w, h, i))
			else:
				origin.append((h, w))
				a.append((h, w, i))
		a.sort()
		last_ind = 0
		answer[last_ind] = (1000000001, -1)
		for (w, h, ind) in a:
			if not answer.get(w):
				answer[w - 1] = answer[last_ind]
				answer[w] = answer[last_ind]
				last_ind = w
			if h < answer[w][0]:
				answer[w] = [h, ind]
		for (w, h) in origin:
			if answer[w - 1][0] < h:
				sub_res += str(answer[w - 1][1] + 1) + ' '
			else:
				sub_res += '-1 '
		res += str(sub_res) + '\n'
	print(res)
main()


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))
import sys, io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
out = []
for _ in range(int(input())):
	n = int(input())
	s = SortedList()
	o = [-1] * n
	for i in range(n):
		(h, w) = map(int, input().split())
		skip = False
		(x, y) = (h, -w)
		idx = s.bisect_left((x, y, i + 1))
		if idx:
			(px, py, pi) = s[idx - 1]
			if y < py:
				o[i] = pi + 1
				skip = True
		if not skip:
			while len(s) > idx:
				(nx, ny, ni) = s[idx]
				if ny < y:
					o[ni] = i + 1
					s.pop(idx)
				else:
					break
			s.add((x, y, i))
		skip = False
		(x, y) = (w, -h)
		idx = s.bisect_left((x, y, i + 1))
		if idx:
			(px, py, pi) = s[idx - 1]
			if y < py:
				o[i] = pi + 1
				skip = True
		if not skip:
			while len(s) > idx:
				(nx, ny, ni) = s[idx]
				if ny < y:
					o[ni] = i + 1
					s.pop(idx)
				else:
					break
			s.add((x, y, i))
	out.append(' '.join(map(str, o)))
print('\n'.join(out))


import sys
import math, bisect, operator
(inf, mod) = (float('inf'), 10 ** 9 + 7)
sys.setrecursionlimit(10 ** 5)
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, defaultdict
I = lambda : int(sys.stdin.readline())
Neo = lambda : list(map(int, sys.stdin.readline().split()))
for _ in range(I()):
	N = I()
	Xs = []
	for i in range(N):
		[h, w] = Neo()
		if w > h:
			(h, w) = (w, h)
		Xs.append((i, h, w))
	Xs.sort(key=lambda x: (x[1], -x[2]))
	ans = [-1] * N
	mn = 10 ** 18
	mni = -1
	for (i, h, w) in Xs:
		if w > mn:
			ans[i] = mni
		if mn > w:
			(mn, mni) = (w, i + 1)
	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 bisect import bisect_left as bsl

def main():
	for j in range(int(input())):
		n = int(input())
		vals = []
		for s in range(n):
			(h, w) = map(int, input().split())
			vals.append((h, w, s))
		vals.sort(key=lambda x: [x[0], x[1]])
		srch = [x[0] for x in vals]
		check = [vals[0][1]]
		index = [vals[0][2]]
		for s in range(1, n):
			if vals[s][1] < check[-1]:
				check.append(vals[s][1])
				index.append(vals[s][2])
			else:
				check.append(check[-1])
				index.append(index[-1])
		ans = [-1 for s in range(n)]
		ind = 0
		for s in range(n):
			if s > 0 and vals[s][0] != vals[s - 1][0]:
				ind = s - 1
			if vals[s][1] > check[ind] and vals[s][0] != vals[ind][0]:
				ans[vals[s][2]] = index[ind] + 1
			else:
				i = min(bsl(srch, vals[s][1]), n - 1)
				if srch[i] >= vals[s][1]:
					i -= 1
				if i >= 0 and vals[s][0] > check[i] and (index[i] != vals[s][2]):
					ans[vals[s][2]] = index[i] + 1
		print(*ans)
main()


for i in range(int(input())):
	n = int(input())
	a = []
	arr = [-1]
	for j in range(n):
		(h, w) = map(int, input().split())
		if h < w:
			(h, w) = (w, h)
		a.append([h, w, j + 1])
		arr.append(-1)
	a.sort()
	width = 1000000000
	a.append(a[-1])
	j = 0
	pos = -1
	position = -1
	while j < n:
		w = 1000000000
		while j < n and a[j][0] == a[j + 1][0]:
			if a[j][1] > width:
				arr[a[j][2]] = position
			if a[j][1] < w:
				w = a[j][1]
				pos = a[j][2]
			j += 1
		else:
			if a[j][1] > width:
				arr[a[j][2]] = position
			if w > a[j][1]:
				w = a[j][1]
				pos = a[j][2]
			j += 1
		if width > w:
			width = w
			position = pos
	print(*arr[1:])


import sys
input = sys.stdin.buffer.readline
T = int(input())

def solve():
	n = int(input())
	res = [-1] * n
	sizes = []
	for i in range(n):
		(w, h) = map(int, input().split())
		if w > h:
			sizes += ((h, w, i),)
		else:
			sizes += ((w, h, i),)
	sizes.sort(key=lambda x: x[0])
	l = 0
	(lw, lh, lj) = sizes[l]
	(min_h, min_h_j) = (lh, lj)
	while l < n and lw == sizes[l][0]:
		(lw, lh, lj) = sizes[l]
		if lh < min_h:
			(min_h, min_h_j) = (lh, lj)
		l += 1
	r = l
	while r < n:
		(lw, lh, lj) = sizes[l]
		(rh, rj) = (lh, lj)
		while r < n and sizes[r][0] == lw:
			(_, rh, rj) = sizes[r]
			if rh > min_h:
				res[rj] = min_h_j + 1
			r += 1
		if r == n:
			break
		while l < r:
			(lw, lh, lj) = sizes[l]
			if lh < min_h:
				(min_h, min_h_j) = (lh, lj)
			l += 1
	print(*res)
while T:
	solve()
	T -= 1


t = int(input())
for _ in range(t):
	n = int(input())
	hw = [0] * n
	for i in range(n):
		(h, w) = map(int, input().split())
		if h > w:
			(h, w) = (w, h)
		hw[i] = [i, h, w]
	hw.sort(key=lambda x: x[2], reverse=True)
	hw.sort(key=lambda x: x[1])
	prev = (-1, 10 ** 10)
	ans = [-1] * n
	for (i, h, w) in hw:
		if prev[1] < w:
			ans[i] = prev[0] + 1
		else:
			prev = (i, w)
	print(*ans)


mxn = 10 ** 10
for i in range(int(input())):
	n = int(input())
	ls = []
	lsr = []
	res = [-1] * (n + 1)
	for i in range(n):
		(a, b) = map(int, input().split())
		ls.append([a, b, i + 1])
		lsr.append([b, a, i + 1])
	ls.sort()
	lsr.sort()
	mini = [0] * n
	mini[0] = mxn
	minir = [0] * n
	minir[0] = mxn
	mn = mxn
	idx = -1
	mnr = mxn
	idxr = -1
	for i in range(n):
		if mn > ls[i][1]:
			mn = ls[i][1]
			idx = ls[i][2]
		mini[i] = [mn, idx]
		if mnr > lsr[i][1]:
			mnr = lsr[i][1]
			idxr = lsr[i][2]
		minir[i] = [mnr, idxr]
	for i in range(n):
		lo = 0
		hi = n - 1
		idx = -1
		while lo <= hi:
			mid = lo + (hi - lo) // 2
			if ls[mid][0] < ls[i][0]:
				idx = mid
				lo = mid + 1
			else:
				hi = mid - 1
		if idx != -1 and ls[i][1] > mini[idx][0]:
			res[ls[i][2]] = mini[idx][1]
		else:
			lo = 0
			hi = n - 1
			idx = -1
			while lo <= hi:
				mid = lo + (hi - lo) // 2
				if lsr[mid][0] < ls[i][0]:
					idx = mid
					lo = mid + 1
				else:
					hi = mid - 1
			if idx != -1:
				if ls[i][1] > minir[idx][0]:
					res[ls[i][2]] = minir[idx][1]
	print(*res[1:])


import sys, bisect as bi
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(n,) = I()
	ar = [I() for i in range(n)]
	x = [ar[i] + [i] for i in range(n)]
	x.sort()
	seg = [[10 ** 9 + 2, 10 ** 9 + 2] for i in range(4 * n)]

	def cons(l, r, i, fun):
		if l == r:
			seg[i] = fun(seg[i], x[l][1:])
			return x[l][1:]
		md = (l + r) // 2
		lm = cons(l, md, 2 * i + 1, fun)
		rm = cons(md + 1, r, 2 * i + 2, fun)
		seg[i] = fun(lm, rm)
		return seg[i]

	def query(l, r, rl, rr, i):
		if rl > rr:
			return [10 ** 9 + 2, 10 ** 9 + 2]
		if l == rl and r == rr:
			return seg[i]
		md = (l + r) // 2
		lm = query(l, md, rl, min(rr, md), 2 * i + 1)
		rm = query(md + 1, r, max(rl, md + 1), rr, 2 * i + 2)
		an = min(lm, rm)
		return an
	cons(0, n - 1, 0, min)
	hh = [x[i][0] for i in range(n)]

	def func(a, n):
		(h, w) = a
		ix = bi.bisect_left(hh, h)
		if ix > 0:
			(xx, y) = query(0, n - 1, 0, ix - 1, 0)
			if xx < w:
				return y
		(w, h) = a
		ix = bi.bisect_left(hh, h)
		if ix > 0:
			(xx, y) = query(0, n - 1, 0, ix - 1, 0)
			if xx < w:
				return y
		return -1
	fn = [-1] * n
	for i in range(n):
		x = func(ar[i], n)
		if x != -1:
			fn[i] = x + 1
	print(*fn)


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

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

def fi():
	return float(input())

def si():
	return input()

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

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

def li():
	return list(mi())

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

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

def lcm(x, y):
	return x * y // gcd(x, y)
mod = 1000000007

def modInverse(b, m):
	g = gcd(b, m)
	if g != 1:
		return -1
	else:
		return pow(b, m - 2, m)

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

def modu(a, b, m):
	a = a % m
	inv = modInverse(b, m)
	if inv == -1:
		return -999999999
	else:
		return inv * a % m
from math import log, factorial, cos, tan, sin, radians, floor, log2
import bisect
from decimal import *
getcontext().prec = 50
abc = 'abcdefghijklmnopqrstuvwxyz'
pi = 3.141592653589793

def gcd1(a):
	if len(a) == 1:
		return a[0]
	ans = a[0]
	for i in range(1, len(a)):
		ans = gcd(ans, a[i])
	return ans

def mykey(x):
	return (x[0], -x[1])

def main():
	for _ in range(ii()):
		n = ii()
		x = []
		y = []
		l = []
		for i in range(n):
			(c, d) = mi()
			x.append(c)
			y.append(d)
			l.append([c, d, i + 1])
		for i in range(n):
			l.append([y[i], x[i], i + 1])
		l = sorted(l, key=mykey)
		ans = [-1] * n
		mx = 1000000000.0
		pr = 1
		for i in range(2 * n):
			if l[i][1] > mx:
				ans[l[i][2] - 1] = pr
			else:
				mx = l[i][1]
				pr = l[i][2]
		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')

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()


import bisect
import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	arr = []
	b = []
	for i in range(n):
		(h, w) = map(int, input().split())
		if h < w:
			(w, h) = (h, w)
		arr.append((h, w, i))
		b.append(w)
	b.sort()
	ar = sorted(arr, key=lambda x: x[1])
	pre = [[10 ** 9, 0]]
	for i in range(n):
		if pre[-1][0] > ar[i][0]:
			pre.append([ar[i][0], ar[i][2] + 1])
		else:
			pre.append([pre[-1][0], pre[-1][1]])
	out = []
	for i in range(n):
		p = arr[i]
		loc = bisect.bisect_left(b, p[1])
		if loc:
			if pre[loc][0] < p[0]:
				out.append(pre[loc][1])
			else:
				out.append(-1)
		else:
			out.append(-1)
	print(*out)


t = int(input())
for _ in range(t):
	n = int(input())
	dimensions = []
	for i in range(n):
		(h, w) = map(int, input().split())
		dimensions.append((i, min(h, w), max(h, w)))
	dimensions = sorted(dimensions, key=lambda item: item[2], reverse=True)
	dimensions = sorted(dimensions, key=lambda item: item[1], reverse=False)
	ans = [-1] * n
	min_b = dimensions[0][2]
	index = 0
	for (i, l, b) in dimensions:
		if min_b < b:
			ans[i] = index + 1
		else:
			index = i
			min_b = b
	print(*ans)


import sys
from math import ceil
from collections import defaultdict

def read_generator():
	while True:
		tokens = sys.stdin.readline().rstrip().split(' ')
		for t in tokens:
			yield t
reader = read_generator()

def read():
	return next(reader)

def readint():
	return int(next(reader))

def readfloat():
	return float(next(reader))

def readline():
	return input()

def readlistint(n):
	return [readint() for _ in range(n)]

def readliststr(n):
	return [read() for _ in range(n)]

def readlistfloat(n):
	return [readfloat() for _ in range(n)]

def solve(n, d):
	d = list(sorted(d))
	res = ['-1'] * n
	hmax = [-1] * (n * 2)
	hmax[0] = 0
	for i in range(1, len(hmax)):
		if d[i][1] < d[hmax[i - 1]][1]:
			hmax[i] = i
		else:
			hmax[i] = hmax[i - 1]
	prev = -1
	for i in range(1, len(hmax)):
		if d[i][0] != d[i - 1][0]:
			prev = i - 1
		if prev == -1:
			continue
		if d[hmax[prev]][1] < d[i][1]:
			res[d[i][2]] = str(d[hmax[prev]][2] + 1)
	return ' '.join(res)
tests = readint()
for test in range(tests):
	n = readint()
	d = [(readint(), readint(), i) for i in range(n)]
	for i in range(n):
		d.append((d[i][1], d[i][0], i))
	res = solve(n, d)
	print(res)


cases = int(input())
for _ in range(cases):
	n = int(input())
	arr = []
	oarr = []
	seen = {}
	for i in range(n):
		(a, b) = [int(i) for i in input().strip().split()]
		oarr.append((a, b))
		if (a, b) not in seen:
			arr.append((a, b, i))
			seen[a, b] = 1
		if (b, a) not in seen:
			arr.append((b, a, i))
			seen[b, a] = 1
	arr.sort()
	res = {}

	def solve(arr):
		n = len(arr)
		lo = 0
		idx = 0
		while idx < n:
			first = idx
			(aa, bb, ii) = arr[lo]
			while idx < n and arr[idx][0] == arr[first][0]:
				(a, b, i) = arr[idx]
				if a > aa and b > bb or (a > bb and b > aa):
					res[a, b] = str(ii + 1)
				idx += 1
			if arr[first][1] < arr[lo][1]:
				lo = first
	solve(arr)
	rs = []
	for (i, (a, b)) in enumerate(oarr):
		ans = '-1'
		for (x, y) in [(a, b), (b, a)]:
			if (x, y) in res:
				ans = res[x, y]
		rs.append(ans)
	print(' '.join(rs))


import os
import sys
from io import BytesIO, IOBase
from collections import Counter
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))

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

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

def main():
	for _ in range(int(input())):
		n = int(input())
		a = []
		for i in range(n):
			(h, w) = map(int, input().split())
			a.append([[h, w], i])
			a.append([[w, h], i])
		a.sort()
		minw = float('inf')
		gmin = float('inf')
		ind = -1
		gind = -1
		ans = [-1] * n
		for i in range(len(a)):
			if i and a[i][0][0] != a[i - 1][0][0]:
				minw = min(gmin, minw)
				ind = gind
			if a[i][0][1] > minw:
				ans[a[i][1]] = ind + 1
			if a[i][0][1] <= gmin:
				gmin = a[i][0][1]
				gind = a[i][1]
		print(*ans)
	return
main()


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

def read_ints():
	return map(int, input().split(' '))
t = read_int()
for case_num in range(t):
	n = read_int()
	people = []
	for i in range(n):
		(h, w) = read_ints()
		people.append((h, w, i))
		people.append((w, h, i))
	ans = [-1] * n
	people.sort()
	lo = int(10000000000.0)
	idx = -1
	l = -1
	for (h, w, i) in people:
		while l + 1 < n and people[l + 1][0] < h:
			l += 1
			if people[l][1] < lo:
				lo = people[l][1]
				idx = people[l][2]
		if lo < w:
			ans[i] = idx + 1
	print(' '.join(map(str, ans)))


t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	for i in range(1, n + 1):
		(h, w) = map(int, input().split())
		if h > w:
			a.append((h, w, i))
		else:
			a.append((w, h, i))
	a.sort()
	ans = ['-1'] * n
	j = 0
	k = -1
	for i in range(n):
		while a[j][0] != a[i][0]:
			if k == -1 or a[j][1] < a[k][1]:
				k = j
			j += 1
		if k != -1 and a[k][1] < a[i][1]:
			ans[a[i][2] - 1] = str(a[k][2])
	print(' '.join(ans))


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	ans = [-1] * n
	idx = defaultdict(list)
	people = list()
	for i in range(n):
		(x, y) = map(int, input().split())
		people.append((x, y))
		people.append((y, x))
		idx[x, y].append(i)
		idx[y, x].append(i)
	new_people = sorted(people, key=lambda x: x[0])
	min_y_till_prev = int(1000000000.0 + 7)
	idx_min_y_till_prev = -1
	min_y_in_x = int(1000000000.0 + 7)
	idx_min_y_in_x = -1
	prev_x = -1
	for (val, i) in enumerate(new_people):
		(x, y) = i
		if x == prev_x:
			if y < min_y_in_x:
				min_y_in_x = min(min_y_in_x, y)
				idx_min_y_in_x = val
		else:
			if min_y_till_prev > min_y_in_x:
				idx_min_y_till_prev = idx_min_y_in_x
				min_y_till_prev = min(min_y_till_prev, min_y_in_x)
			min_y_in_x = y
			idx_min_y_in_x = val
			prev_x = x
		if min_y_till_prev < y and ans[idx[x, y][0]] == -1:
			for j in idx[x, y]:
				ans[j] = idx[new_people[idx_min_y_till_prev]][0] + 1
	for i in ans:
		print(i, end=' ')
	print('')


from collections import defaultdict
tests = int(input())
for t in range(tests):
	n = int(input())
	ls_a = []
	ls_b = []
	for idx in range(n):
		(a, b) = list(map(int, input().split()))
		if a > b:
			(a, b) = (b, a)
		ls_a.append((a, idx + 1))
		ls_b.append((b, idx + 1))
	ls_a = sorted(ls_a)
	ls_b = sorted(ls_b)
	a_dic = {}
	b_dic = {}
	for (pos, item) in enumerate(ls_a):
		(val, idx) = item
		a_dic[idx] = (val, pos)
	for (pos, item) in enumerate(ls_b):
		(val, idx) = item
		b_dic[idx] = (val, pos)
	res_dic = {}
	curr_best = None
	curr_b = None
	pointer = 0
	while pointer < len(ls_a):
		item = ls_a[pointer]
		(a, idx) = item
		curr_point = pointer
		tracker_list = []
		while curr_point < len(ls_a) and a == ls_a[curr_point][0]:
			curr_item = ls_a[curr_point]
			tracker_list.append((b_dic[curr_item[1]][0], curr_item[1]))
			if curr_best is not None:
				if b_dic[curr_item[1]][0] > curr_b:
					res_dic[curr_item[1]] = curr_best
			curr_point += 1
		pointer = curr_point
		tracker_list = sorted(tracker_list)
		b_val = tracker_list[0][0]
		if curr_best is None or curr_b > b_val:
			curr_b = b_val
			curr_best = tracker_list[0][1]
	res = []
	for i in range(n):
		if i + 1 in res_dic:
			res.append(res_dic[i + 1])
		else:
			res.append(-1)
	print(' '.join([str(item) for item in res]))


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = []
	for i in range(n):
		(w, h) = map(int, input().split())
		a.append([w, h, i])
		a.append([h, w, i])
	a.sort(key=lambda x: (x[0], -x[1]))
	ans = [-1] * n
	minh = 1000000000.0
	mini = 1
	for i in range(2 * n):
		(w, h, j) = (a[i][0], a[i][1], a[i][2])
		if h > minh:
			ans[j] = mini
		else:
			minh = h
			mini = j + 1
	print(*ans)


import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
for _ in range(t):
	n = int(input())
	ppl = []
	for i in range(n):
		(h, w) = map(int, input().split())
		if h < w:
			ppl.append([h, w, i])
		else:
			ppl.append([w, h, i])
	ppl.sort()
	ans = [0] * n
	currmin = 10 ** 9 + 1
	currbest = -1
	tmpbest = 10 ** 9 + 1
	for i in range(n):
		if ppl[i][1] > currmin:
			ans[ppl[i][2]] = str(currbest)
		else:
			ans[ppl[i][2]] = str(-1)
		if ppl[i][1] < tmpbest:
			tmpbest = ppl[i][1]
			tmploc = ppl[i][2] + 1
		if i < n - 1 and ppl[i + 1][0] > ppl[i][0]:
			if tmpbest < currmin:
				currmin = tmpbest
				currbest = tmploc
	print(' '.join(ans))


from bisect import bisect_left, bisect_right

class Result:

	def __init__(self, index, value):
		self.index = index
		self.value = value

class BinarySearch:

	def __init__(self):
		pass

	@staticmethod
	def greater_than(num: int, func, size: int=1):
		if isinstance(func, list):
			index = bisect_right(func, num)
			if index == len(func):
				return Result(None, None)
			else:
				return Result(index, func[index])
		else:
			(alpha, omega) = (0, size - 1)
			if func(omega) <= num:
				return Result(None, None)
			while alpha < omega:
				if func(alpha) > num:
					return Result(alpha, func(alpha))
				if omega == alpha + 1:
					return Result(omega, func(omega))
				mid = (alpha + omega) // 2
				if func(mid) > num:
					omega = mid
				else:
					alpha = mid

	@staticmethod
	def less_than(num: int, func, size: int=1):
		if isinstance(func, list):
			index = bisect_left(func, num) - 1
			if index == -1:
				return Result(None, None)
			else:
				return Result(index, func[index])
		else:
			(alpha, omega) = (0, size - 1)
			if func(alpha) >= num:
				return Result(None, None)
			while alpha < omega:
				if func(omega) < num:
					return Result(omega, func(omega))
				if omega == alpha + 1:
					return Result(alpha, func(alpha))
				mid = (alpha + omega) // 2
				if func(mid) < num:
					alpha = mid
				else:
					omega = mid
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
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 gcd, ceil

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

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
bs = BinarySearch()
for _ in range(int(input()) if True else 1):
	n = int(input())
	hw = []
	for i in range(n):
		(h, w) = map(int, input().split())
		hw += [[h, w, i]]
	hws = sorted(hw)
	heights = [hws[i][0] for i in range(n)]
	widths = [hws[i][1] for i in range(n)]
	di = {}
	for i in range(n):
		if hws[i][1] not in di:
			di[hws[i][1]] = []
		di[hws[i][1]] += [hws[i][2]]
	st = SegmentTree(widths, func=min)
	ans = [-1] * n
	for i in range(n):
		pos = False
		(hi, wi, ind) = hw[i]
		j = bs.less_than(hi, heights).index
		if j is not None:
			wj = st.query(0, j)
			if wj < wi:
				pos = True
				ans[i] = di[wj][0] + 1
		if pos:
			continue
		j = bs.less_than(wi, heights).index
		if j is not None:
			wj = st.query(0, j)
			if wj < hi:
				pos = True
				ans[i] = di[wj][0] + 1
	print(*ans)


import sys
import itertools

class RMQ:

	def __init__(self, n):
		self.sz = 1
		self.inf = (1 << 40) - 1
		while self.sz <= n:
			self.sz = self.sz << 1
		self.dat = [self.inf] * (2 * self.sz - 1)

	def update(self, idx, x):
		idx += self.sz - 1
		self.dat[idx] = x
		while idx > 0:
			idx = idx - 1 >> 1
			self.dat[idx] = min(self.dat[idx * 2 + 1], self.dat[idx * 2 + 2])

	def query(self, a, b):
		return self.query_help(a, b, 0, 0, self.sz)

	def query_help(self, a, b, k, l, r):
		if r <= a or b <= l:
			return self.inf
		elif a <= l and r <= b:
			return self.dat[k]
		else:
			return min(self.query_help(a, b, 2 * k + 1, l, l + r >> 1), self.query_help(a, b, 2 * k + 2, l + r >> 1, r))
msk = (1 << 20) - 1

def solve(a, b, n):
	rmq = RMQ(2 * n)
	for i in range(n):
		aa = a[i]
		aaa = (a[i] << 20) + i
		bb = b[i]
		bbb = (b[i] << 20) + i
		if bbb < rmq.query(aa, aa + 1):
			rmq.update(aa, bbb)
		if aaa < rmq.query(bb, bb + 1):
			rmq.update(bb, aaa)
	ans = []
	for i in range(n):
		rmm = rmq.query(0, b[i])
		if rmm >> 20 < a[i]:
			ans.append((rmm & msk) + 1)
		else:
			ans.append(-1)
	sys.stdout.write(' '.join(map(str, ans)) + '\n')
for t in range(int(sys.stdin.readline())):
	n = int(sys.stdin.readline())
	d = [[int(i) for i in sys.stdin.readline().split()] for j in range(n)]
	d_ = [d[i][0] for i in range(n)] + [d[i][1] for i in range(n)]
	d_.sort()
	d__ = {}
	for i in range(2 * n):
		d__[d_[i]] = i
	dda = [d__[d[i][0]] for i in range(n)]
	ddb = [d__[d[i][1]] for i in range(n)]
	solve(dda, ddb, n)


from sys import stdin, stdout
import math, bisect
from collections import Counter, deque, defaultdict
L = lambda : list(map(int, stdin.readline().strip().split()))
I = lambda : int(stdin.readline().strip())
S = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()

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

def solve():
	n = I()
	a = []
	for i in range(n):
		(h, w) = L()
		a.append([i, min(h, w), max(h, w)])
	a.sort()
	a.sort(key=lambda x: x[2], reverse=True)
	a.sort(key=lambda x: x[1])
	ans = [-1] * n
	(x, y) = (0, 10 ** 9 + 1)
	for (i, h, w) in a:
		if w > y:
			ans[i] = x
		else:
			(x, y) = (i + 1, w)
	pr(ans)
for _ in range(I()):
	solve()


import math
import string
import random
import sys
from random import randrange
from collections import deque
from collections import defaultdict
from bisect import bisect, bisect_right, bisect_left

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

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
for _ in range(int(input())):
	n = int(data())
	arr = []
	for i in range(n):
		(h, w) = mdata()
		arr.append([h, w, i])
		arr.append([w, h, i])
	arr.sort(key=lambda k: (k[0], k[1]))
	ans = [-1] * n
	mn = ind = 10 ** 18
	front = deque()
	for i in arr:
		while front and front[0][0] < i[0]:
			temp = front.popleft()
			if temp[1] < mn:
				mn = temp[1]
				ind = temp[-1]
		if ans[i[2]] == -1 and i[1] > mn:
			ans[i[2]] = ind + 1
		front.append(i)
	print(*ans)


import math
import os
from io import BytesIO, IOBase
import sys
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')
t = int(input())
for _ in range(t):
	n = int(input())
	persons = []
	for i in range(n):
		persons.append(tuple(map(int, input().split())))
	persons = [(x_0, x_1, idx) if x_0 <= x_1 else (x_1, x_0, idx) for (idx, (x_0, x_1)) in enumerate(persons)]
	results = [-1] * n
	persons.sort()
	smallest_right = 1000000000000000000
	smallest_right_idx = -1
	i = 0
	while i < len(persons):
		curr_left = persons[i][0]
		temp_smallest_right = smallest_right
		temp_smallest_right_idx = smallest_right_idx
		j = i
		while j < len(persons) and persons[j][0] == curr_left:
			if smallest_right < persons[j][1]:
				results[persons[j][2]] = smallest_right_idx + 1
			elif temp_smallest_right > persons[j][1]:
				temp_smallest_right_idx = persons[j][2]
				temp_smallest_right = persons[j][1]
			j += 1
		i = j
		smallest_right_idx = temp_smallest_right_idx
		smallest_right = temp_smallest_right
	print(' '.join([str(x) for x in results]))


import sys
zz = 1
sys.setrecursionlimit(10 ** 5)
if zz:
	input = sys.stdin.readline
else:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('all.txt', 'w')
di = [[-1, 0], [1, 0], [0, 1], [0, -1]]

def fori(n):
	return [fi() for i in range(n)]

def inc(d, c, x=1):
	d[c] = d[c] + x if c in d else x

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

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

def fli():
	return [float(x) for x in input().split()]

def comp(a, b):
	if a > b:
		return 2
	return 2 if a == b else 0

def gi():
	return [xx for xx in input().split()]

def gtc(tc, ans):
	print('Case #' + str(tc) + ':', ans)

def cil(n, m):
	return n // m + int(n % m > 0)

def fi():
	return int(input())

def pro(a):
	return reduce(lambda a, b: a * b, a)

def swap(a, i, j):
	(a[i], a[j]) = (a[j], a[i])

def si():
	return list(input().rstrip())

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

def gh():
	sys.stdout.flush()

def isvalid(i, j, n, m):
	return 0 <= i < n and 0 <= j < m

def bo(i):
	return ord(i) - ord('a')

def graph(n, m):
	for i in range(m):
		(x, y) = mi()
		a[x].append(y)
		a[y].append(x)
t = fi()
uu = t
while t > 0:
	t -= 1
	n = fi()
	d = {}
	a = []
	for i in range(n):
		(h, w) = mi()
		a.append([min(h, w), max(h, w)])
		if min(h, w) not in d:
			d[min(h, w)] = []
		d[min(h, w)].append([max(h, w), i])
	r = []
	for i in d:
		d[i].sort()
		r.append(i)
	ans = []
	r.sort()
	p = []
	for i in range(len(r)):
		if len(p):
			if p[-1][0] > d[r[i]][0][0]:
				p.append(d[r[i]][0])
			else:
				p.append(p[-1])
		else:
			p.append(d[r[i]][0])
	ans = []
	for i in range(n):
		l = anss = 0
		if a[i][0] <= r[0]:
			ans.append(-1)
			continue
		rr = len(r) - 1
		while l <= rr:
			mid = (l + rr) // 2
			if r[mid] >= a[i][0]:
				rr = mid - 1
			else:
				anss = mid
				l = mid + 1
		if p[anss][0] < a[i][1]:
			ans.append(p[anss][1] + 1)
		else:
			ans.append(-1)
	print(*ans)


from collections import deque, defaultdict, Counter
import sys
mod = 10 ** 9 + 7

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

def sol():
	n = int(input())
	Friends_standing = []
	Friends_lying = []
	for i in range(n):
		(h, w) = map(int, input().split())
		Friends_lying.append((w, h, i, 0))
		Friends_lying.append((h, w, i, 1))
		Friends_standing.append((h, w, i))
	Friends_lying.sort(key=lambda x: x[1])
	Friends_standing.sort(key=lambda x: x[1])
	Friends_lying = deque(Friends_lying)
	Friends_standing = deque(Friends_standing)
	ans = [-1] * n
	minh = 1 << 60
	minw = Friends_standing[0][1]
	mindex = -1
	while Friends_standing:
		nextmin = 1 << 60
		nextmindex = -1
		while Friends_standing and Friends_standing[0][1] == minw:
			(h, w, idx) = Friends_standing.popleft()
			if minh < h:
				ans[idx] = mindex
			if h < nextmin:
				nextmin = h
				nextmindex = idx + 1
		if nextmin < minh:
			minh = nextmin
			mindex = nextmindex
		if Friends_standing:
			minw = Friends_standing[0][1]
	minh = 1 << 60
	minw = Friends_lying[0][1]
	mindex = -1
	while Friends_lying:
		nextmin = 1 << 60
		nextmindex = -1
		while Friends_lying and Friends_lying[0][1] == minw:
			(h, w, idx, status) = Friends_lying.popleft()
			if status == 1:
				if minh < h:
					ans[idx] = mindex
			elif status == 0:
				if h < nextmin:
					nextmin = h
					nextmindex = idx + 1
		if nextmin < minh:
			minh = nextmin
			mindex = nextmindex
		if Friends_lying:
			minw = Friends_lying[0][1]
	print(*ans)
	return

def main():
	testcase = int(input())
	for i in range(testcase):
		sol()
main()


import bisect
from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	w = []
	di = {}
	di = defaultdict(lambda : -1, di)
	for i in range(n):
		(a, b) = map(int, input().split())
		w.append([min(a, b), max(a, b)])
	j = 1
	for i in w:
		di[f'{i}'] = j
		j = j + 1
	ww = w.copy()
	ww.sort()
	x = []
	y = []
	for i in ww:
		x.append(i[0])
		y.append(i[1])
	mins = []
	mi = 0
	for i in range(len(y)):
		if y[i] < y[mi]:
			mi = i
			mins.append(i)
		else:
			mins.append(mi)
	res = []
	for i in w:
		c = bisect.bisect_left(x, i[0])
		if c == 0:
			res.append(-1)
		else:
			z = mins[c - 1]
			if y[z] >= i[1]:
				res.append(-1)
			else:
				k = ww[z]
				res.append(di[f'{k}'])
	print(*res)


for _ in range(int(input())):
	n = int(input())
	ans = [-1 for i in range(n)]
	(p, q) = ([], [])
	for i in range(n):
		(h, w) = map(int, input().split())
		p.append((h, w, i))
		q.append((w, h, i))
	p.sort(key=lambda x: x[0])
	q.sort(key=lambda x: x[0])
	(bstp, bstq) = ((0, -1), (0, -1))
	(j, k) = (0, 0)
	for i in range(n):
		while j < n and p[j][0] < p[i][0]:
			if bstp[1] == -1 or bstp[0] > p[j][1]:
				bstp = (p[j][1], p[j][2])
			j += 1
		while k < n and q[k][0] < p[i][0]:
			if bstq[1] == -1 or bstq[0] > q[k][1]:
				bstq = (q[k][1], q[k][2])
			k += 1
		if bstp[1] != -1 and bstp[0] < p[i][1]:
			ans[p[i][2]] = bstp[1] + 1
		if bstq[1] != -1 and bstq[0] < p[i][1]:
			ans[p[i][2]] = bstq[1] + 1
	for i in range(n):
		print(ans[i], end=' ')
	print()


from collections import defaultdict

def solution(n, l):
	d = defaultdict(list)
	min_width_f_smaller_heights = {}
	sol = [-1 for _ in range(n)]
	Big = 10 ** 10
	for pair in l:
		pair.sort(reverse=True)
	l = [[pair[0], pair[1], i] for (i, pair) in enumerate(l)]
	l.sort()
	for [h, w, i] in l:
		d[h].append([w, i])
	for h in d:
		(smallest_w, ind_) = (Big, 0)
		for [w, i] in d[h]:
			if w < smallest_w:
				(smallest_w, ind_) = (w, i)
		d[h] = [smallest_w, ind_]
	heights_sorted = list(sorted(d.keys()))
	min_width_f_smaller_heights[heights_sorted[0]] = [Big, None]
	prev_min_width = [Big, None]
	prev_height = heights_sorted[0]
	for h in heights_sorted[1:]:
		[smallest_w, ind_] = d[prev_height]
		if smallest_w < prev_min_width[0]:
			min_width_f_smaller_heights[h] = [smallest_w, ind_]
		else:
			min_width_f_smaller_heights[h] = min_width_f_smaller_heights[prev_height]
		prev_min_width = min_width_f_smaller_heights[h]
		prev_height = h
	for [h, w, i] in l:
		if w > min_width_f_smaller_heights[h][0]:
			sol[i] = int(min_width_f_smaller_heights[h][1] + 1)
	print(*sol, sep=' ')
T = int(input())
for t in range(1, T + 1):
	n = int(input())
	l = [list(map(int, input().split())) for _ in range(n)]
	solution(n, l)


import sys
input = sys.stdin.readline
from collections import defaultdict
import bisect
for _ in range(int(input())):
	n = int(input())
	(A, B) = ([], [])
	for _ in range(n):
		(a, b) = map(int, input().split())
		A.append(a)
		B.append(b)
	ans = [-1] * n
	X = []
	for (i, (a, b)) in enumerate(zip(A, B), 1):
		X.append((a, b, i))
		X.append((b, a, i))
	X.sort()
	dic = {}
	pre = (float('inf'), 0)
	for (a, b, i) in X:
		if b < pre[0]:
			dic[a] = (b, i)
			pre = (b, i)
		else:
			dic[a] = pre
	for (i, (a, b)) in enumerate(zip(A, B)):
		idx = bisect.bisect_left(X, (a, 0))
		if idx:
			x = X[idx - 1][0]
			(y, j) = dic[x]
			if y < b:
				ans[i] = j
	print(*ans)


import sys
t = int(input())
for i in range(0, t):
	n = int(input())
	people = []
	for j in range(0, n):
		(h, w) = map(int, input().split())
		people.append((w, h, j))
		people.append((h, w, j))
	people.sort(key=lambda x: x[1], reverse=True)
	people.sort(key=lambda x: x[0])
	minHeight = sys.maxsize
	ans = [-1] * n
	element = -1
	for j in people:
		w = j[0]
		h = j[1]
		ind = j[2]
		if h < minHeight:
			minHeight = h
			element = ind
		elif h > minHeight:
			ans[ind] = element + 1
	print(*ans)


import sys
for _ in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		(h, w) = map(int, input().split())
		if w > h:
			(h, w) = (w, h)
		a.append((i, h, w))
	a.sort(key=lambda x: (x[1], -x[2]))
	ans = [-1] * n
	maxi = sys.maxsize
	index = -1
	for (i, h, w) in a:
		if w > maxi:
			ans[i] = index
		if maxi > w:
			(maxi, index) = (w, i + 1)
	print(*ans)


import math
import sys

def get_ints():
	return map(int, sys.stdin.readline().strip().split())
T = int(input())
for t in range(T):
	n = int(input())
	c = [0] * n
	for i in range(n):
		(w, h) = map(int, input().split())
		c[i] = (max(w, h), min(w, h))
	b = sorted(range(n), key=lambda i: c[i])
	c.sort()
	d = [0] * n
	dc = c[0][1]
	d[0] = dc
	dci = [0] * n
	di = 0
	e = [0] * n
	ec = -1
	for i in range(1, n):
		if c[i][1] < dc:
			dc = c[i][1]
			di = i
		dci[i] = di
		d[i] = dc
		if c[i][0] != c[i - 1][0]:
			ec = i - 1
		e[i] = ec
	a = [0] * n
	for i in range(n):
		a[b[i]] = i
	for i in range(n):
		if e[a[i]] == -1:
			print(-1, end=' ')
		elif c[a[i]][1] <= d[e[a[i]]]:
			print(-1, end=' ')
		else:
			print(b[dci[e[a[i]]]] + 1, end=' ')
	print()


import math
from bisect import bisect_left
t = int(input())
for _ in range(t):
	n = int(input())
	friends = [tuple(map(int, input().split())) for _ in range(n)]
	x = list(range(n))
	x.sort(key=lambda i: friends[i])
	heights = [friends[i][0] for i in x]
	min_width_i = -1
	min_width = math.inf
	min_width_i_list = []
	min_width_list = []
	for i in x:
		width = friends[i][1]
		if width < min_width:
			min_width_i = i
			min_width = width
		min_width_i_list.append(min_width_i)
		min_width_list.append(min_width)

	def helper(height, width):
		i = bisect_left(heights, height) - 1
		if i == -1:
			return -1
		if min_width_list[i] < width:
			return min_width_i_list[i] + 1
		return -1
	result = []
	for (height, width) in friends:
		res1 = helper(height, width)
		res2 = helper(width, height)
		result.append(res1 if res1 != -1 else res2)
	print(*result)


import os
import sys
from math import *
from collections import *
from bisect import *
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 = 'abcdefghijklmnopqrstuvwxyz'
M = 998244353
EPS = 1e-06

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

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

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

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	n = Int()
	a = []
	W = defaultdict(lambda : (inf, inf))
	for i in range(n):
		(w, h) = value()
		a.append((w, h))
		if W[w][0] > h:
			W[w] = (h, i + 1)
	W_list = sorted(W.keys())
	for j in range(1, len(W_list)):
		i = W_list[j]
		k = W_list[j - 1]
		if W[i][0] > W[k][0]:
			W[i] = W[k]
	for (w, h) in a:
		ind = bisect_left(W_list, w)
		ind -= 1
		ans = -1
		if ind >= 0 and W[W_list[ind]][0] < h:
			ans = W[W_list[ind]][1]
		ind = bisect_left(W_list, h)
		ind -= 1
		if ind >= 0 and W[W_list[ind]][0] < w:
			ans = W[W_list[ind]][1]
		print(ans, end=' ')
	print()


t = int(input())
for i in range(t):
	n = int(input())
	a = []
	ans = [-1 for i in range(n)]
	for i in range(n):
		(h, w) = map(int, input().split())
		if w > h:
			(h, w) = (w, h)
		a.append((h, w, i))
	a.sort(key=lambda x: x[1], reverse=True)
	a.sort(key=lambda x: x[0])
	prei = -1
	prew = 10 ** 10
	for (h, w, i) in a:
		if prew < w:
			ans[i] = prei + 1
		else:
			prei = i
			prew = w
	print(*ans)


import sys
input = sys.stdin.readline
from operator import itemgetter
t = int(input())
for tests in range(t):
	n = int(input())
	A = dict()
	for i in range(n):
		(h, w) = map(int, input().split())
		if h > w:
			if h in A:
				A[h].append((w, i))
			else:
				A[h] = [(w, i)]
		elif w in A:
			A[w].append((h, i))
		else:
			A[w] = [(h, i)]
	ANS = [-1] * n
	LIST = sorted(A)
	MIN = 1 << 60
	MININD = -1
	for l in LIST:
		CMIN = MIN
		CMININD = -1
		for (w, i) in A[l]:
			if w > MIN:
				ANS[i] = MININD + 1
			if w < CMIN:
				CMIN = w
				CMININD = i
		if MIN != CMIN:
			MIN = CMIN
			MININD = CMININD
	print(*ANS)


test = int(input())
for faoseufn in range(test):
	n = int(input())
	l = []
	for i in range(n):
		(a, b) = list(map(int, input().split()))
		if a < b:
			(a, b) = (b, a)
		l.append((a, b, i + 1))
	l.sort()
	ans = [-1] * n
	w = {}
	ind = {}
	w[l[0][0]] = l[0][1]
	ind[l[0][0]] = l[0][2]
	low = [l[0]]
	for i in range(1, n):
		if l[i - 1][0] < l[i][0]:
			if l[i][1] < low[-1][1]:
				low.append(l[i])
	ptr = 0
	mptr = len(low) - 1
	for i in range(1, n):
		if low[0][0] == l[i][0]:
			continue
		elif low[-1][1] >= l[i][1]:
			continue
		while ptr < mptr and l[i][0] > low[ptr + 1][0]:
			ptr += 1
		if low[ptr][0] < l[i][0] and low[ptr][1] < l[i][1]:
			ans[l[i][2] - 1] = low[ptr][2]
	print(*ans)


from collections import defaultdict
import sys
import bisect
input = sys.stdin.readline
t = int(input())
while t:
	n = int(input())
	hi = []
	wi = []
	bhi = []
	bwi = []
	for i in range(n):
		(h, w) = map(int, input().split())
		hi.append((h, w, i))
		bhi.append(h)
	hi.sort()
	bhi.sort()
	hii = [(hi[0][1], hi[0][2])]
	for i in range(1, n):
		if hii[i - 1][0] > hi[i][1]:
			hii.append((hi[i][1], hi[i][2]))
		else:
			hii.append(hii[i - 1])
	res = [-1] * n
	for i in range(n):
		(h, w, ind) = hi[i]
		ind1 = bisect.bisect_left(bhi, h)
		ind2 = bisect.bisect_left(bhi, w)
		if ind1 > 0:
			ind1 -= 1
			if hii[ind1][0] < w:
				res[ind] = hii[ind1][1] + 1
		if ind2 > 0:
			ind2 -= 1
			if hii[ind2][0] < h:
				res[ind] = hii[ind2][1] + 1
	print(*res)
	t -= 1


MOD = 1000000007

def si():
	return input()

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

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

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

def sout(v):
	print(v, end=' ')

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

def twod(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def vow():
	return ['a', 'e', 'i', 'o', 'u']

def let():
	return [chr(i) for i in range(97, 123)]

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

def ispow2(x):
	return x and (not x & x - 1)

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)
t = ii()
while t:
	t -= 1
	n = ii()
	a = []
	for i in range(n):
		(h, w) = mi()
		a.append([-h, w, i])
		a.append([-w, h, i])
	w = sorted(a, key=lambda x: (x[1], x[0]))
	n *= 2
	for i in range(n):
		w[i][0] = abs(w[i][0])
	i = 0
	hmin = 999999999999
	ans = [-1 for _ in range(n // 2)]
	while i < n:
		j = w[i][2]
		if w[i][0] < hmin:
			hmin = w[i][0]
			id = w[i][2]
		elif w[i][0] > hmin:
			ans[w[i][2]] = id + 1
		i += 1
	print(' '.join((str(i) for i in ans)))


from itertools import repeat, groupby

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

def num():
	return int(input())

def nfunc(f, n, *args, **kwargs):
	return (f(*args, **kwargs) for _ in repeat(None, n))

def byw(q):
	return q[1][0]

def byh(q):
	return q[1][1]
t = num()
for _ in repeat(None, t):
	n = num()
	frs = [(min(q, w), max(q, w)) for (q, w) in nfunc(line, n)]
	wss = [list(q) for (_, q) in groupby(sorted(enumerate(frs), key=byw), key=byw)]
	hmins = [wss[0][0][0]]
	wmap = [0] * n
	for (j, q) in enumerate(wss):
		for (i, (w, h)) in q:
			wmap[i] = j
		(i, (w, h)) = min(q, key=byh)
		if h < frs[hmins[-1]][1]:
			hmins.append(i)
		else:
			hmins.append(hmins[-1])
	hmins = hmins[1:]
	ans = []
	for i in range(n):
		j = wmap[i]
		(w, h) = frs[i]
		if j and h > frs[hmins[j - 1]][1]:
			ans.append(hmins[j - 1] + 1)
		else:
			ans.append(-1)
	print(' '.join(map(str, ans)))


for z in range(int(input())):
	n = int(input())
	l = []
	ans = [-1 for i in range(n)]
	for i in range(n):
		(a, b) = map(int, input().split())
		l.append((a, 1, b, i))
		l.append((a, 0, b, i))
		l.append((b, 0, a, i))
	l.sort()
	stack = []
	for i in l:
		if i[1] == 1:
			if len(stack) == 0 or stack[-1][0] > i[2]:
				stack.append((i[2], i[3]))
		elif stack and stack[-1][0] < i[2]:
			ans[i[3]] = stack[-1][1] + 1
	print(*ans)


import sys, os, io
import bisect
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
for _ in range(int(input())):
	n = int(input())
	a = []
	h1 = []
	w1 = []
	a1 = []
	for i in range(n):
		(h, w) = [int(i) for i in input().split()]
		a.append([h, w, i])
		a1.append([w, h, i])
		h1.append(h)
		w1.append(w)
	a.sort()
	a1.sort()
	h = []
	w = []
	sw = []
	hw = []
	mw = 10 ** 10
	mw1 = 10 ** 10
	for i in a:
		h.append(i[0])
		if mw > i[1]:
			mw = i[1]
			sw.append([mw, i[2]])
		else:
			sw.append([mw, sw[-1][1]])
	for i in a1:
		w.append(i[0])
		if mw1 > i[1]:
			mw1 = i[1]
			hw.append([mw1, i[2]])
		else:
			hw.append([mw1, hw[-1][1]])
	ans = []
	for i in range(n):
		ind = bisect.bisect_left(h, h1[i])
		ind -= 1
		if ind < 0:
			ans.append(-1)
		elif sw[ind][0] < w1[i]:
			ans.append(sw[ind][1] + 1)
		else:
			ans.append(-1)
		if ans[-1] == -1:
			ind = bisect.bisect_left(h, w1[i])
			ind -= 1
			if ind < 0:
				continue
			elif sw[ind][0] < h1[i]:
				ans.pop()
				ans.append(sw[ind][1] + 1)
	print(*ans)


import sys

def correctPlacement(n, fren):
	fren.sort()
	ans = ['-1'] * n
	j = 0
	k = -1
	for i in range(n):
		while fren[j][0] != fren[i][0]:
			if k == -1 or fren[j][1] < fren[k][1]:
				k = j
			j += 1
		if k != -1 and fren[k][1] < fren[i][1]:
			ans[fren[i][2] - 1] = str(fren[k][2])
	print(' '.join(ans))

def main():
	for t in range(int(input())):
		n = int(input())
		fren = []
		for i in range(1, n + 1):
			(h, w) = map(int, input().split())
			if h > w:
				fren.append([h, w, i])
			else:
				fren.append([w, h, i])
		correctPlacement(n, fren)
main()


class BIT:

	def __init__(self, n):
		self.BIT = [0] * (n + 1)
		self.num = n

	def query(self, idx):
		res_sum = 0
		while idx > 0:
			res_sum += self.BIT[idx]
			idx -= idx & -idx
		return res_sum

	def update(self, idx, x):
		while idx <= self.num:
			self.BIT[idx] += x
			idx += idx & -idx
		return
import sys
from collections import deque
input = sys.stdin.buffer.readline
for _ in range(int(input())):
	n = int(input())
	val = set([])
	hito = []
	for i in range(n):
		(h, w) = map(int, input().split())
		val.add(h)
		val.add(w)
		hito.append((h, w, i))
		hito.append((w, h, i))
	val = list(val)
	val.sort()
	comp = {e: i for (i, e) in enumerate(val)}
	m = len(val)
	mini_w = [10 ** 17 for i in range(m)]
	mini_w_idx = [-2 for i in range(m)]
	for i in range(2 * n):
		(h, w, idx) = hito[i]
		if mini_w[comp[h]] > w:
			mini_w[comp[h]] = w
			mini_w_idx[comp[h]] = idx
	for i in range(1, m):
		if mini_w[i] > mini_w[i - 1]:
			mini_w[i] = mini_w[i - 1]
			mini_w_idx[i] = mini_w_idx[i - 1]
	ans = [-1 for i in range(n)]
	for (h, w, idx) in hito:
		c = comp[h]
		if c:
			if w > mini_w[c - 1]:
				ans[idx] = mini_w_idx[c - 1] + 1
	print(*ans)


import bisect
from itertools import accumulate
import os
import sys
import math
from decimal import *
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))

def input():
	return sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	hw = []
	for i in range(n):
		(h, w) = map(int, input().split())
		if h > w:
			(h, w) = (w, h)
		hw.append((i, h, w))
	hw.sort(key=lambda x: x[2], reverse=True)
	hw.sort(key=lambda x: x[1])
	mw = (-1, 10 ** 10)
	ans = [-1] * n
	for (i, h, w) in hw:
		if mw[1] < w:
			ans[i] = mw[0] + 1
		else:
			mw = (i, w)
	print(*ans)


import sys
import bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
rep = range
R = range

def I():
	return int(input())

def S_():
	return input()

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

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

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

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

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def NLI(n):
	return [[int(i) for i in input().split()] for i in range(n)]

def NLI_(n):
	return [[int(i) - 1 for i in input().split()] for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def RLI(n=8, a=1, b=10):
	return [random.randint(a, b) for i in range(n)]

def RI(a=1, b=10):
	return random.randint(a, b)

def INP():
	N = 9
	n = random.randint(1, N)
	A = [random.randint(1, n) for i in range(m)]
	return (n, A)

def Rtest(T):
	(case, err) = (0, 0)
	for i in range(T):
		inp = INP()
		a1 = naive(*inp)
		a2 = solve(*inp)
		if a1 != a2:
			print(inp)
			print('naive', a1)
			print('solve', a2)
			err += 1
		case += 1
	print('Tested', case, 'case with', err, 'errors')

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in R(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def accum(ls):
	rt = [0]
	for i in ls:
		rt += [rt[-1] + i]
	return rt

def bit_combination(n, base=2):
	rt = []
	for tb in R(base ** n):
		s = [tb // base ** bt % base for bt in R(n)]
		rt += [s]
	return rt

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

def YN(x):
	print(['NO', 'YES'][x])

def Yn(x):
	print(['No', 'Yes'][x])

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
mo = 10 ** 9 + 7
inf = float('inf')
FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('WENS', FourNb))
cursol = dict(zip('LRUD', FourNb))
l_alp = string.ascii_lowercase
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
input = lambda : sys.stdin.readline().rstrip()

class SparseTable:

	def __init__(self, ls, init_func=min, init_idl=float('inf')):
		self.func = init_func
		self.idl = init_idl
		self.size = len(ls)
		self.N0 = self.size.bit_length()
		self.table = [ls[:]]
		self.index = [list(range(self.size))]
		self.lg = [0] * (self.size + 1)
		for i in range(2, self.size + 1):
			self.lg[i] = self.lg[i >> 1] + 1
		for i in range(self.N0):
			tmp = [self.func(self.table[i][j], self.table[i][min(j + (1 << i), self.size - 1)]) for j in range(self.size)]
			tmp_id = [self.index[i][j] if self.table[i][j] == self.func(self.table[i][j], self.table[i][min(j + (1 << i), self.size - 1)]) else self.index[i][min(j + (1 << i), self.size - 1)] for j in range(self.size)]
			self.table += [tmp]
			self.index += [tmp_id]

	def query(self, l, r):
		if r > self.size:
			r = self.size
		N = self.lg[r - l]
		return self.func(self.table[N][l], self.table[N][max(0, r - (1 << N))])

	def query_id(self, l, r):
		if r > self.size:
			r = self.size
		N = self.lg[r - l]
		(a, b) = (self.index[N][l], self.index[N][max(0, r - (1 << N))])
		if self.table[0][a] == self.func(self.table[N][l], self.table[N][max(0, r - (1 << N))]):
			b = a
		return b

	def right_bound(self, pos, x):
		k = (self.size - pos).bit_length()
		for j in range(k)[::-1]:
			nx = pos + (1 << j)
			if nx < n and self.query(pos, nx + 1) <= x:
				pos += 1 << j
		return pos

	def left_bound(self, pos, x):
		k = pos.bit_length()
		for j in range(k)[::-1]:
			nx = pos - (1 << j)
			if 0 <= nx and self.query(nx, pos + 1) <= x:
				pos -= 1 << j
		return pos

	def __str__(self):
		return str(self.table[0])

	def print(self):
		for i in self.table:
			print(*i)
show_flg = False
show_flg = True
ans = 0

def solve(H, W):
	p = []
	for i in range(n):
		p += ((H[i], (W[i], i)),)
	p.sort(key=lambda x: x[0])
	sh = sorted(H)
	acmin = [(p[0][1][0], p[0][1][1])]
	for i in range(1, n):
		(h, (w, i)) = p[i]
		(mw, mi) = acmin[-1]
		if w < mw:
			acmin += ((w, i),)
		else:
			acmin += ((mw, mi),)
	return (sh, acmin)
for _ in range(I()):
	ans = 0
	n = I()
	H = []
	W = []
	org = []
	for i in range(n):
		(h, w) = LI()
		H += (h,)
		W += (w,)
	r1 = []
	r2 = []
	rt = []
	(SH, hacmin) = solve(W, H)
	for i in range(n):
		(h, w) = (H[i], W[i])
		idx = bisect.bisect_left(SH, h)
		if 0 < idx and hacmin[idx - 1][0] < w:
			ans = hacmin[idx - 1][1] + 1
		else:
			ans = -1
		r1 += (ans,)
	(SH, hacmin) = solve(H, W)
	for i in range(n):
		(h, w) = (H[i], W[i])
		idx = bisect.bisect_left(SH, h)
		if 0 < idx and hacmin[idx - 1][0] < w:
			ans = hacmin[idx - 1][1] + 1
		else:
			ans = -1
		r2 += (ans,)
	for i in range(n):
		if r1[i] != -1:
			rt += (r1[i],)
		elif r2[i] != -1:
			rt += (r2[i],)
		else:
			rt += (-1,)
	print(*rt)


import sys
import math
import collections
import heapq
import decimal
input = sys.stdin.readline
t = int(input())
for w in range(t):
	n = int(input())
	l = []
	for i in range(n):
		(h, w) = (int(i) for i in input().split())
		l.append((i, max(h, w), min(h, w)))
	l.sort(key=lambda kv: (kv[1], -kv[2]))
	ans = [-1] * n
	m = 1000000000000
	k = -1
	for i in range(n):
		if l[i][2] > m:
			ans[l[i][0]] = k
		elif m > l[i][2]:
			m = l[i][2]
			k = l[i][0] + 1
	print(*ans)


cases = int(input())
for _ in range(cases):
	n = int(input())
	arr = []
	for i in range(n):
		(a, b) = [int(i) for i in input().strip().split()]
		arr.append((a, b, i))
		arr.append((b, a, i))
	arr.sort()
	res = {}

	def solve(arr):
		n = len(arr)
		lo = 0
		idx = 0
		while idx < n:
			first = idx
			(aa, bb, ii) = arr[lo]
			while idx < n and arr[idx][0] == arr[first][0]:
				(a, b, i) = arr[idx]
				if a > aa and b > bb or (a > bb and b > aa):
					res[i] = str(ii + 1)
				idx += 1
			if arr[first][1] < arr[lo][1]:
				lo = first
	solve(arr)
	print(' '.join([res[i] if i in res else '-1' for i in range(n)]))


from collections import defaultdict

def solve(n, info):
	min_h = defaultdict(int)
	min_h_pos = defaultdict(int)
	for x in info:
		if x[2] not in min_h or (x[2] in min_h and min_h[x[2]] > x[1]):
			min_h[x[2]] = x[1]
			min_h_pos[x[2]] = x[0]
	min_less_w = defaultdict(int)
	min_less_w_pos = defaultdict(int)
	w_ordered = list(sorted(min_h.keys()))
	prev_w = w_ordered[0]
	for w in w_ordered[1:]:
		if min_less_w[prev_w]:
			if min_less_w[prev_w] < min_h[prev_w]:
				min_less_w[w] = min_less_w[prev_w]
				min_less_w_pos[w] = min_less_w_pos[prev_w]
			else:
				min_less_w[w] = min_h[prev_w]
				min_less_w_pos[w] = min_h_pos[prev_w]
		else:
			min_less_w[w] = min_h[prev_w]
			min_less_w_pos[w] = min_h_pos[prev_w]
		prev_w = w
	for x in info:
		(pos, h, w) = x
		if min_less_w[w] == 0:
			print(-1, end=' ')
		elif min_less_w[w] < h:
			print(min_less_w_pos[w], end=' ')
		else:
			print(-1, end=' ')
	print()
t = int(input())
for x in range(t):
	n = int(input())
	info = []
	for i in range(n):
		(h, w) = map(int, input().split())
		if h < w:
			(w, h) = (h, w)
		info.append((i + 1, h, w))
	solve(n, info)


import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from math import sqrt, pi, ceil, log, inf, gcd, floor
from itertools import combinations, permutations
from bisect import *
from fractions import Fraction
from heapq import *
from random import randint

def main():
	for _ in range(int(input())):
		n = int(input())
		(a, h, w) = ([], [], [])
		for i in range(n):
			(x, y) = map(int, input().split())
			h.append(x)
			w.append(y)
			a.append((x, y, i))
		a.sort(key=lambda x: x[0])
		p = [a[0][2]]
		mi = a[0][1]
		for i in range(1, n):
			if mi > a[i][1]:
				mi = a[i][1]
				p.append(a[i][2])
			else:
				p.append(p[-1])
		ans = [-1] * n
		b = []
		for i in a:
			b.append(i[0])
		for i in range(n):
			z = bisect_left(b, h[i])
			if z != 0:
				if w[p[z - 1]] < w[i]:
					ans[i] = p[z - 1] + 1
		for i in range(n):
			if ans[i] == -1:
				z = bisect_left(b, w[i])
				if z != 0:
					if w[p[z - 1]] < h[i]:
						ans[i] = p[z - 1] + 1
		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()


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

def prog():
	for _ in range(int(input())):
		n = int(input())
		a = [list(map(int, input().split())) + [i + 1] for i in range(n)]
		a2 = a.copy()
		b = []
		ans = []
		a.sort()
		lst = 0
		for i in a:
			if i[0] != lst:
				b.append([i[0], 10 ** 10 if lst == 0 else b[-1][1], -1 if lst == 0 else b[-1][2]])
			if i[1] < b[-1][1]:
				b[-1][1] = i[1]
				b[-1][2] = i[2]
			lst = b[-1][0]
		for i in a2:
			queries = [i, [i[1], i[0]]]
			found = -1
			for q in queries:
				idx = bisect_left(b, [q[0], 0, -1]) - 1
				if idx >= 0 and b[idx][1] < q[1]:
					found = b[idx][2]
					break
			ans.append(found)
		print(*ans)
prog()


for T in range(int(input())):
	n = int(input())
	res = ['-1' for i in range(n)]
	images = []
	for idx in range(n):
		(w, h) = map(int, input().split())
		if w < h:
			(w, h) = (h, w)
		images.append((w, h, idx))
	images.sort(key=lambda x: (x[0], x[1]), reverse=True)
	hmin = []
	hmin.append((images[-1][1], images[-1][2], images[-1][0]))
	for i in range(n - 2, -1, -1):
		if images[i + 1][1] < hmin[-1][0]:
			hmin.append((images[i + 1][1], images[i + 1][2], images[i + 1][0]))
		else:
			continue
	for i in range(n):
		while hmin and images[i][0] <= hmin[-1][2]:
			hmin.pop()
		if not hmin:
			break
		if images[i][1] > hmin[-1][0]:
			res[images[i][2]] = str(hmin[-1][1] + 1)
	print(' '.join(res))


import os
import sys
from io import BytesIO, IOBase
from operator import itemgetter

def main():
	T = int(input())
	sys.stdout.write('\n'.join([solve() for _ in range(T)]))

def solve():
	n = int(input())
	res = [-1] * n
	sizes = [None] * n
	i = 0
	while i < n:
		a = [*map(int, input().split()), i]
		if a[0] > a[1]:
			(a[0], a[1]) = (a[1], a[0])
		sizes[i] = a
		i += 1
	sizes.sort(key=itemgetter(0))
	l = 0
	(lw, lh, lj) = sizes[l]
	(min_h, min_h_j) = (lh, lj)
	while l < n and lw == sizes[l][0]:
		(lw, lh, lj) = sizes[l]
		if lh < min_h:
			(min_h, min_h_j) = (lh, lj)
		l += 1
	r = l
	while r < n:
		(lw, lh, lj) = sizes[l]
		(rh, rj) = (lh, lj)
		while r < n and sizes[r][0] == lw:
			(_, rh, rj) = sizes[r]
			if rh > min_h:
				res[rj] = min_h_j + 1
			r += 1
		if r == n:
			break
		while l < r:
			(lw, lh, lj) = sizes[l]
			if lh < min_h:
				(min_h, min_h_j) = (lh, lj)
			l += 1
	return ' '.join([str(x) for x in res])

class FastIO(IOBase):
	newlines = 0
	BUFSIZE = 8192

	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, self.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, self.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()
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

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


def binary_search(arr, item):
	l = 0
	r = len(arr) - 1
	while r - l > -1:
		middle = (l + r) // 2
		key = arr[middle]
		if key == item:
			return middle
		elif (key[0], -key[1], key[2]) < (item[0], -item[1], item[2]):
			l = middle + 1
		else:
			r = middle - 1
	print(arr, item, middle)
	return None
t = int(input())
for it in range(t):
	n = int(input())
	friends = []
	for i in range(n):
		(a, b) = tuple(map(int, input().split()))
		if b > a:
			(a, b) = (b, a)
		friend = (a, b, i + 1)
		friends.append(friend)
	sorted_friends = sorted(friends, key=lambda x: (x[0], -x[1], x[2]))
	min_friend_suffix = [sorted_friends[0]]
	for i in range(len(sorted_friends) - 1):
		smallest = min_friend_suffix[-1]
		friend = sorted_friends[i]
		next_friend = sorted_friends[i + 1]
		if friend[0] < next_friend[0] and friend[1] < smallest[1]:
			min_friend_suffix.append(friend)
		else:
			min_friend_suffix.append(smallest)
	for friend in friends:
		index = binary_search(sorted_friends, friend)
		candidate = min_friend_suffix[index]
		if candidate[0] < friend[0] and candidate[1] < friend[1]:
			print(candidate[2], end=' ')
		else:
			print(-1, end=' ')
	print()


import sys
import io, os
input = sys.stdin.buffer.readline
mod = 2 * 10 ** 5 + 50

class SegTree:

	def __init__(self, init_val, ide_ele, segfunc):
		self.n = len(init_val)
		self.num = 2 ** (self.n - 1).bit_length()
		self.ide_ele = ide_ele
		self.segfunc = segfunc
		self.seg = [ide_ele] * 2 * self.num
		for i in range(self.n):
			self.seg[i + self.num] = init_val[i]
		for i in range(self.num - 1, 0, -1):
			self.seg[i] = self.segfunc(self.seg[2 * i], self.seg[2 * i + 1])

	def update(self, k, x):
		k += self.num
		self.seg[k] = x
		while k:
			k = k >> 1
			self.seg[k] = self.segfunc(self.seg[2 * k], self.seg[2 * k + 1])

	def query(self, l, r):
		if r <= l:
			return self.ide_ele
		l += self.num
		r += self.num
		lres = self.ide_ele
		rres = self.ide_ele
		while l < r:
			if r & 1:
				r -= 1
				rres = self.segfunc(self.seg[r], rres)
			if l & 1:
				lres = self.segfunc(lres, self.seg[l])
				l += 1
			l = l >> 1
			r = r >> 1
		res = self.segfunc(lres, rres)
		return res

def segfunc(x, y):
	if x <= y:
		return x
	else:
		return y
INF = 10 ** 20
from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	B = set()
	HW = []
	for i in range(n):
		(h, w) = map(int, input().split())
		HW.append((h, w, i))
		HW.append((w, h, i))
		B.add(h)
		B.add(w)
	B = list(B)
	B.sort()
	d = {}
	for (i, b) in enumerate(B):
		d[b] = i
	seg = SegTree([INF] * (len(d) + 5), INF, segfunc)
	HW.sort(key=lambda x: (x[0], x[1]))
	ans = [-1] * n
	for (h, w, i) in HW:
		(h, w) = (d[h], d[w])
		p = seg.query(0, h)
		if p != INF:
			(w0, i0) = divmod(p, mod)
			if w0 < w:
				ans[i] = i0 + 1
		q = seg.query(0, w)
		if q != INF:
			(w0, i0) = divmod(q, mod)
			if w0 < h:
				ans[i] = i0 + 1
		r = seg.query(h, h + 1)
		temp = w * mod + i
		seg.update(h, min(temp, r))
	print(*ans)


import math, sys
from collections import defaultdict, deque
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')

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

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

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

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		n = I()
		l = set()
		d = defaultdict(list)
		for i in range(n):
			(a, b) = In()
			l.add(a)
			l.add(b)
			d[a].append([b, i])
			d[b].append([a, i])
		l = list(l)
		l.sort()
		ans = [-1 for x in range(n)]
		(mn, indx) = (P, P)
		for x in range(len(l)):
			Key = l[x]
			d[Key].sort(key=lambda x: x[0])
			(mnidx, curmn) = (P, P)
			for i in d[Key]:
				if i[0] > mn:
					ans[i[1]] = indx
				if i[0] < curmn:
					curmn = i[0]
					mnidx = i[1]
			if curmn < mn:
				mn = curmn
				indx = mnidx
		for x in range(n):
			print(ans[x] + 1, end=' ') if ans[x] != -1 else print(-1, end=' ')
		print()
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


import sys
import bisect
input = sys.stdin.readline
t = int(input())
for you in range(t):
	n = int(input())
	l = []
	for i in range(n):
		lo = input().split()
		x = int(lo[0])
		y = int(lo[1])
		l.append((x, y))
	lh1 = []
	for i in range(n):
		lh1.append((l[i][0], l[i][1], i))
	lh1.sort()
	l1 = []
	l2 = []
	for i in lh1:
		l1.append(i[0])
		l2.append((i[1], i[2]))
	ans = [-1 for i in range(n)]
	mina = [0 for i in range(n)]
	z = 1e+18
	p = 0
	for i in range(n):
		if l2[i][0] < z:
			z = l2[i][0]
			p = l2[i][1]
		mina[i] = (z, p)
	for i in range(n):
		y = bisect.bisect_left(l1, l[i][0])
		if y == 0:
			continue
		y -= 1
		if mina[y][0] < l[i][1]:
			ans[i] = mina[y][1] + 1
	lh1 = []
	for i in range(n):
		lh1.append((l[i][1], l[i][0], i))
	lh1.sort()
	l1 = []
	l2 = []
	for i in lh1:
		l1.append(i[0])
		l2.append((i[1], i[2]))
	mina = [0 for i in range(n)]
	z = 1e+18
	p = 0
	for i in range(n):
		if l2[i][0] < z:
			z = l2[i][0]
			p = l2[i][1]
		mina[i] = (z, p)
	for i in range(n):
		y = bisect.bisect_left(l1, l[i][0])
		if y == 0:
			continue
		y -= 1
		if mina[y][0] < l[i][1]:
			ans[i] = mina[y][1] + 1
	for i in ans:
		print(i, end=' ')
	print()


t = int(input())
for i in range(t):
	n = int(input())
	arr = []
	lst = [-1] * (n + 1)
	for j in range(n):
		(h, w) = map(int, input().split(' '))
		arr.append([min(h, w), max(h, w), j + 1])
	arr.sort()
	index = 0
	mnwt = arr[0][1]
	index2 = 0
	mnwt2 = arr[0][1]
	for i in range(1, n):
		if arr[i][0] > arr[index2][0]:
			index = index2
			mnwt = mnwt2
		if arr[index][0] < arr[i][0] and mnwt < arr[i][1]:
			lst[arr[i][2]] = arr[index][2]
		elif arr[index2][0] < arr[i][0] and mnwt2 < arr[i][1]:
			lst[arr[i][2]] = arr[index2][2]
		if arr[i][1] < mnwt2:
			index2 = i
			mnwt2 = arr[i][1]
	for i in range(1, n + 1):
		if i == n:
			print(lst[i])
		else:
			print(lst[i], end=' ')


import sys
from operator import itemgetter
rl = sys.stdin.readline
rlb = sys.stdin.buffer.readline

def solve():
	n = int(rlb())
	res = [-1] * n
	sizes = [None] * n
	i = 0
	while i < n:
		(w, h) = map(int, rlb().split())
		if w > h:
			sizes[i] = (h, w, i)
		else:
			sizes[i] = (w, h, i)
		i += 1
	sizes.sort(key=itemgetter(0))
	l = 0
	(best_w, best_h, best_j) = sizes[l]
	for i in range(0, len(sizes)):
		(w, h, j) = sizes[i]
		while l < i and sizes[l][0] < w:
			if best_h > sizes[l][1]:
				(best_w, best_h, best_j) = sizes[l]
			l += 1
		if w > best_w and h > best_h:
			res[j] = best_j + 1
	return ' '.join([str(x) for x in res])

def main():
	sys.stdout.write('\n'.join([solve() for t in range(int(rlb()))]))
main()


t = int(input())
while t:
	n = int(input())
	arr = []
	for i in range(n):
		(a, b) = list(map(int, input().split()))
		if a > b:
			(a, b) = (b, a)
		arr.append((a, -b, i))
		arr.append((b, -1, i))
	ans = [-1 for i in range(n)]
	a = -1
	min_height = float('inf')
	element = -1
	arr.sort()
	for (w, h, i) in arr:
		h = -h
		if min_height < h:
			ans[i] = element + 1
		elif h < min_height:
			min_height = h
			element = i
	print(*ans)
	t -= 1


t = int(input())
for __ in range(t):
	n = int(input())
	arr = []
	for i in range(n):
		(h, w) = map(int, input().split(' '))
		if h > w:
			(h, w) = (w, h)
		arr.append((h, w, i))
	arr.sort()
	new_list = []
	new_sub_list = []
	for i in range(n):
		if not new_sub_list or arr[i][0] == new_sub_list[-1][0]:
			new_sub_list.append(arr[i])
		else:
			new_list.append(new_sub_list)
			new_sub_list = [arr[i]]
	if new_sub_list:
		new_list.append(new_sub_list)
	res = [-1] * n
	wmin = new_list[0][0][1]
	widx = new_list[0][0][2]
	for i in range(1, len(new_list)):
		for (h, w, idx) in new_list[i]:
			if w > wmin:
				res[idx] = widx + 1
		if wmin > new_list[i][0][1]:
			wmin = new_list[i][0][1]
			widx = new_list[i][0][2]
	print(' '.join((str(val) for val in res)))


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
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 _ in range(I()):
	n = I()
	a = sorted([L() + [i + 1] for i in range(n)], key=lambda x: x[1])
	a.sort()
	p = [a[0][1:]]
	for i in a[1:]:
		if i[1] < p[-1][0]:
			p += (i[1:],)
		else:
			p += (p[-1],)
	ans = [-1] * n
	v = [i[0] for i in a]
	for i in range(n):
		x = bisect_left(v, v[i])
		y = bisect_left(v, a[i][1])
		if x != 0 and p[x - 1][0] < a[i][1]:
			ans[a[i][-1] - 1] = p[x - 1][-1]
		elif y != 0 and p[y - 1][0] < v[i]:
			ans[a[i][-1] - 1] = p[y - 1][-1]
	print(*ans)


import sys
input = sys.stdin.readline

def find_num(t, arr, num, find_index):
	low = 0
	high = len(arr) - 1
	while low < high:
		mid = (low + high) // 2
		if arr[mid][0] >= t[0]:
			high = mid - 1
		else:
			low = mid + 1
	if arr[low][0] >= t[0]:
		if low > 0 and num[low - 1][0] < t[1]:
			return find_index[tuple(arr[num[low - 1][1]])]
		return -1
	else:
		if num[low][0] < t[1]:
			return find_index[tuple(arr[num[low][1]])]
		return -1
for nt in range(int(input())):
	n = int(input())
	a = []
	(d1, d2) = ({}, {})
	(c1, c2) = ([], [])
	for i in range(n):
		a.append(list(map(int, input().split())))
		d1[tuple(a[-1])] = i + 1
	b = sorted(a)
	c1.append([b[0][1], 0])
	for i in range(1, n):
		if b[i][1] < c1[-1][0]:
			c1.append([b[i][1], i])
		else:
			c1.append([c1[-1][0], c1[-1][1]])
	c = []
	for i in range(n):
		c.append([a[i][1], a[i][0]])
		d2[tuple(c[-1])] = i + 1
	c.sort()
	c2.append([c[0][1], 0])
	for i in range(1, n):
		if c[i][1] < c2[-1][0]:
			c2.append([c[i][1], i])
		else:
			c2.append([c2[-1][0], c2[-1][1]])
	ans = []
	for i in a:
		x = find_num(i, b, c1, d1)
		if x == -1:
			y = find_num(i, c, c2, d2)
			ans.append(y)
		else:
			ans.append(x)
	print(*ans)


from sys import stdin
input = stdin.readline

def photo(arr):
	temp = []
	ans = [-1] * len(arr)
	for i in range(len(arr)):
		temp.append([arr[i][0], arr[i][1], i])
	temp = sorted(temp, key=lambda s: s[1], reverse=True)
	temp = sorted(temp, key=lambda s: s[0])
	mw = [10 ** 10, -1]
	for i in range(len(temp)):
		if mw[0] < temp[i][1]:
			ans[temp[i][2]] = mw[1] + 1
		else:
			mw = [temp[i][1], temp[i][2]]
	print(*ans)
	return ''
for i in range(int(input())):
	a = int(input())
	blanck = []
	for j in range(a):
		(x, y) = map(int, input().strip().split())
		if x > y:
			(x, y) = (y, x)
		blanck.append([x, y])
	print(photo(blanck))


t = int(input())
for q in range(t):
	n = int(input())
	I = []
	for i in range(n):
		ch = input()
		L = [int(i) for i in ch.split()]
		I.append((min(L[0], L[1]), max(L[0], L[1]), i + 1))
	I.sort()
	curr = [I[0]]
	T = [-1] * n
	for i in range(1, n):
		if I[i][0] > I[i - 1][0] and I[i][1] < curr[-1][1]:
			curr.append(I[i])
		if I[i][0] > curr[-1][0] and I[i][1] > curr[-1][1]:
			T[I[i][2] - 1] = curr[-1][2]
		if len(curr) >= 2 and I[i][0] > curr[-2][0] and (I[i][1] > curr[-2][1]):
			T[I[i][2] - 1] = curr[-2][2]
	ch = ''
	for i in T:
		ch += str(i) + ' '
	print(ch)


from heapq import *
T = int(input())
for t in range(T):
	n = int(input())
	lis = [0] * n
	ans = [-1] * n
	for i in range(n):
		(x, y) = map(int, input().split())
		lis[i] = (max(x, y), min(x, y), i)
	lis.sort()
	(min_y, min_j) = (10 ** 10, 0)
	temp = []
	last_x = -1
	for (x, y, i) in lis:
		if last_x < x:
			for (y2, i2) in temp:
				if y2 < min_y:
					(min_y, min_j) = (y2, i2)
			last_x = x
			temp = []
		if min_y < y:
			ans[i] = min_j + 1
		temp.append((y, i))
	print(*ans, sep=' ')


tc = int(input())
for _ in range(tc):
	n = int(input())
	fr = []
	for i in range(n):
		(h, w) = list(map(int, input().split()))
		fr.append([min(h, w), max(h, w), i + 1, -1])
	fr.sort(key=lambda x: -x[1])
	fr.sort(key=lambda x: -x[0])
	smallW = {}
	prev = (float('inf'), -1)
	for i in reversed(range(n)):
		if smallW.get(fr[i][0]) is None:
			smallW[fr[i][0]] = prev
			if prev[0] > fr[i][1]:
				prev = (fr[i][1], fr[i][2])
	for i in range(n):
		if smallW[fr[i][0]][0] < fr[i][1]:
			fr[i][3] = smallW[fr[i][0]][1]
	ret = []
	fr.sort(key=lambda x: x[2])
	for i in range(n):
		ret.append(str(fr[i][3]))
	print(' '.join(ret))


for _ in range(int(input())):
	n = int(input())
	arr = []
	for i in range(n):
		(h, w) = map(int, input().split())
		arr.append((h, -w, i))
		arr.append((w, -h, i))
	arr.sort()
	min_height = float('inf')
	element = -1
	ans = [-1] * n
	for (w, h, i) in arr:
		h = -h
		if min_height < h:
			ans[i] = element
		elif min_height > h:
			min_height = h
			element = i + 1
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	a = list()
	for i in range(n):
		(w, h) = map(int, input().split())
		a.append((w, h, i))
		a.append((h, w, i))
	a.sort()
	(mn, mk, mw) = ([a[0][1]], [0], [-1])
	for i in range(1, n * 2):
		if mn[-1] > a[i][1]:
			mn.append(a[i][1])
			mk.append(i)
		else:
			mn.append(mn[-1])
			mk.append(mk[-1])
		if a[i][0] > a[i - 1][0]:
			mw.append(i - 1)
		else:
			mw.append(mw[-1])
	ans = [-1] * n
	for i in range(n * 2):
		if mw[i] != -1:
			if mn[mw[i]] < a[i][1]:
				ans[a[i][2]] = a[mk[mw[i]]][2] + 1
	print(*ans)


import sys
input = sys.stdin.readline

def do():
	import collections
	n = int(input())
	m = collections.defaultdict(list)
	data = []
	datb = []
	for i in range(n):
		(a, b) = map(int, input().split())
		(a, b) = (min(a, b), max(a, b))
		data.append(a)
		datb.append(b)
		m[a].append(i)
	mi = 1 << 30
	ret = -1
	r = [0] * n
	kl = list(m.keys())
	kl.sort()
	for x in kl:
		for e in m[x]:
			if mi < datb[e]:
				r[e] = ret
			else:
				r[e] = -1
		for e in m[x]:
			if datb[e] < mi:
				mi = datb[e]
				ret = e + 1
	print(' '.join(list(map(str, r))))
q = int(input())
for _ in range(q):
	do()


import os
from heapq import heapify, heappop, heappush
import sys
import sys, threading
import math
import operator
import bisect
from collections import defaultdict
from io import BytesIO, IOBase
sys.setrecursionlimit(10 ** 5)

def inpt():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		n = int(input())
		ar = []
		for i in range(n):
			(a, b) = inpt()
			if a > b:
				(a, b) = (b, a)
			ar.append((a, b, i))
		ar.sort(key=lambda x: [x[0], -x[1]])
		(x, y) = (-1, 10 ** 9)
		ans = [-1] * n
		for (a, b, i) in ar:
			if y < b:
				ans[i] = x + 1
			else:
				(x, y) = (i, b)
		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()


import os
import sys
from io import BytesIO, IOBase
from collections import defaultdict, deque, Counter, OrderedDict
import threading

def main():
	for _ in range(int(input())):
		n = int(input())
		(arr, ans, ind) = ([], [-1] * n, 0)
		for i in range(n):
			(a, b) = [int(j) for j in input().split()]
			arr.append((max(a, b), -min(a, b), i))
		arr.sort()
		for i in range(1, n):
			if -arr[i][1] > -arr[ind][1]:
				ans[arr[i][2]] = arr[ind][2] + 1
			elif -arr[i][1] < -arr[ind][1]:
				ind = 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()


import sys
import math
from collections import defaultdict, Counter
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')
t = int(input())
for i in range(t):
	n = int(input())
	dw = {}
	final = defaultdict(list)
	final2 = defaultdict(list)
	for j in range(n):
		(h, w) = map(int, input().split())
		final[h].append([w, j])
		if dw.get(w):
			if h < dw[w][0]:
				dw[w] = [h, j + 1]
		else:
			dw[w] = [h, j + 1]
	ans = [-1] * n
	dh1 = sorted(final)
	dw1 = sorted(dw)
	ind = [10 ** 10, 0]
	for j in dh1:
		mi = ind[:]
		for k in final[j]:
			if ind[0] < k[0]:
				ans[k[1]] = ind[1]
			elif k[0] < mi[0]:
				mi = [k[0], k[1] + 1]
		ind = mi[:]
	ind = [10 ** 10, 0]
	start = 0
	for j in dh1:
		while start < len(dw1) and j > dw1[start]:
			if ind[0] > dw[dw1[start]][0]:
				ind = dw[dw1[start]][:]
			start += 1
		for k in final[j]:
			if ind[0] < k[0]:
				ans[k[1]] = ind[1]
	print(*ans)


from io import BytesIO, IOBase
import sys
import math
import os
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))

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

def solve():
	n = int(input())
	arr = []
	d = {}
	for i in range(n):
		(a, b) = map(int, input().split())
		if a > b:
			(a, b) = (b, a)
		x = d.get(a, [math.inf])
		if b < x[0]:
			d[a] = [b, i + 1]
		arr.append([a, b])
	keys = sorted(d.keys())
	d2 = {}
	currMin = math.inf
	minIndex = -1
	for i in keys:
		d2[i] = [currMin, minIndex]
		if d[i][0] < currMin:
			currMin = d[i][0]
			minIndex = d[i][1]
	ans = []
	for i in range(n):
		(a, b) = arr[i]
		if b > d2[a][0]:
			ans.append(d2[a][1])
		else:
			ans.append(-1)
	print(*ans)

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


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

class FastIO(IOBase):

	def __init__(self, file):
		self.newlines = 0
		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()

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

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

def N():
	return int(input())

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

def print_list(l):
	print(' '.join(map(str, l)))
for _ in range(N()):
	n = N()
	p = []
	for i in range(n):
		(h, w) = RL()
		p.append((h, w, i))
		p.append((w, h, i))
	p.sort(key=lambda x: (-x[0], x[1]))
	stack = [(0, 0, 0)]
	res = [-1] * n
	for (h, w, i) in p:
		while stack[-1][0] > h and stack[-1][1] > w:
			(_, _, j) = stack.pop()
			res[j] = i + 1
		stack.append((h, w, i))
	print_list(res)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
from bisect import bisect_left

def gift():
	for _ in range(t):
		n = int(input())
		nums = []
		for i in range(n):
			ele = list(map(int, input().split()))
			ele.sort()
			ele.append(i)
			nums.append(ele)
		nums.sort(key=lambda x: (x[0], x[1]))
		currMinMax = float('inf')
		level = nums[0][0]
		temMin = float('inf')
		ansMap = {}
		prevLoc = None
		temPrevLoc = None
		for ele in nums:
			(mine, maxe, index) = ele
			if mine != level:
				level = mine
				if temMin < currMinMax:
					currMinMax = temMin
					prevLoc = temPrevLoc
			if maxe > currMinMax:
				ansMap[index] = prevLoc + 1
			else:
				ansMap[index] = -1
			if maxe < temMin:
				temMin = maxe
				temPrevLoc = index
		ans = []
		for i in range(n):
			ans.append(ansMap[i])
		yield ' '.join([str(x) for x in ans])
t = int(input())
ans = gift()
print(*ans, sep='\n')


import os
from io import BytesIO, IOBase
from collections import defaultdict, deque, Counter, OrderedDict
import threading
import sys

def main():
	a = int(input())
	for j in range(a):
		n = int(input())
		a = []
		for i in range(n):
			(h, w) = map(int, input().split())
			if w > h:
				(h, w) = (w, h)
			a.append((i, h, w))
		a.sort(key=lambda x: (x[1], -x[2]))
		ans = [-1] * n
		maxi = sys.maxsize
		index = -1
		for (i, h, w) in a:
			if w > maxi:
				ans[i] = index
			if maxi > w:
				(maxi, index) = (w, i + 1)
		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')
sys.setrecursionlimit(400000)
threading.stack_size(40960000)
thread = threading.Thread(target=main)
thread.start()


def solve():
	n = int(input())
	data = []
	kek = dict()
	for i in range(n):
		(a, b) = map(int, input().split())
		data.append((max(a, b), min(a, b), i))
	data_1 = sorted(data, key=lambda x: x[0])
	key = 0
	kek[0] = [1000000001, -1]
	for person in data_1:
		(a, b, i) = person
		value = kek.get(a)
		if not value:
			kek[a] = kek[key][:]
			kek[a - 1] = kek[key][:]
			key = a
			value = kek.get(a)
		if value[0] > b:
			kek[a] = [b, i]
	for lol in data:
		(a, b, i) = lol
		(value, index) = kek.get(max(a, b) - 1)
		if value < min(a, b):
			print(index + 1, end=' ')
		else:
			print(-1, end=' ')
	print()
for _ in range(int(input())):
	solve()


import sys, io, os
if os.environ['USERNAME'] == 'kissz':
	inp = open('in.txt', 'r').readline

	def debug(*args):
		print(*args, file=sys.stderr)
else:
	inp = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

	def debug(*args):
		pass
for _ in range(int(inp())):
	n = int(inp())
	objs = [(0, 0)] * n
	thinests = {}
	for i in range(n):
		(w, h) = sorted(map(int, inp().split()))
		if h not in thinests:
			thinests[h] = (w, i)
		else:
			thinests[h] = min(thinests[h], (w, i))
		objs[i] = (w, h)
	thin = (1 << 30, 0)
	debug(objs)
	debug(thinests)
	smaller = {}
	for (h, thinest) in sorted(thinests.items()):
		smaller[h] = thin
		(w, i) = thinest
		if w < thin[0]:
			thin = (w, i)
	debug(smaller)
	o = []
	for (w, h) in objs:
		(wmin, ind) = smaller[h]
		if w > wmin:
			o.append(ind + 1)
		else:
			o.append(-1)
	print(*o)


def set_p(p, d, i, v):
	p[d[i][2]] = v if v == -1 else d[v][2] + 1

def solve(n, d):
	d.sort(key=lambda x: x[0])
	p = [0] * n
	m_c = m_nc = -1
	for i in range(n):
		if i:
			if d[i][0] > d[i - 1][0]:
				m_nc = min(m_c, m_nc, key=lambda x: d[x][1]) if m_nc != -1 else m_c
				m_c = -1
			if m_c == -1:
				if d[i][1] <= d[m_nc][1]:
					set_p(p, d, i, -1)
				else:
					set_p(p, d, i, m_nc)
				m_c = i
			else:
				m_c = min(m_c, i, key=lambda x: d[x][1])
				if d[i][1] <= d[m_nc][1]:
					set_p(p, d, i, -1)
				else:
					set_p(p, d, i, m_nc)
		else:
			m_c = i
			set_p(p, d, i, -1)
	print(*p)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		d = []
		for i in range(n):
			(hi, wi) = map(int, input().split())
			if hi < wi:
				d.append((wi, hi, i))
			else:
				d.append((hi, wi, i))
		solve(n, d)
main()


from bisect import *
a = int(input())
for i in range(a):
	s = int(input())
	ans = []
	dns = []
	kam = []
	for i in range(s):
		(x, y) = map(int, input().split())
		ans.append([x, y, i])
		dns.append([y, x, i])
		kam.append([x, y, i])
	z = kam
	ans.sort()
	dns.sort()
	pre1 = [[ans[0][1], ans[0][2]]]
	for i in range(1, len(ans)):
		if ans[i][1] < pre1[-1][0]:
			pre1.append([ans[i][1], ans[i][2]])
		else:
			pre1.append(pre1[-1])
	pre2 = [[dns[0][1], dns[0][2]]]
	for i in range(1, len(dns)):
		if dns[i][1] < pre2[-1][0]:
			pre2.append([dns[i][1], dns[i][2]])
		else:
			pre2.append(pre2[-1])
	gam = [ans[i][0] for i in range(len(ans))]
	tam = [dns[i][0] for i in range(len(dns))]
	dam = [-1 for i in range(s)]
	for i in range(len(z)):
		q = bisect_left(gam, kam[i][0])
		if q == len(gam):
			q -= 1
		if gam[q] >= kam[i][0]:
			q -= 1
		if q >= 0 and pre1[q][0] < kam[i][1]:
			dam[i] = pre1[q][1] + 1
		q = bisect_left(tam, kam[i][0])
		if q == len(tam):
			q -= 1
		if tam[q] >= kam[i][0]:
			q -= 1
		if q >= 0 and pre2[q][0] < kam[i][1]:
			dam[i] = pre2[q][1] + 1
	print(*dam)


rn = lambda : int(input())
rl = lambda : list(map(int, input().split()))
rns = lambda : map(int, input().split())
rs = lambda : input()
yn = lambda x: print('Yes') if x else print('No')
YN = lambda x: print('YES') if x else print('NO')
for _ in range(rn()):
	n = rn()
	p = []
	for i in range(n):
		p.append(sorted(rl()) + [i])
	p.sort(key=lambda x: x[1], reverse=True)
	p.sort(key=lambda x: x[0])
	ans = [-1] * n
	index = -1
	m = float('inf')
	for i in range(n):
		if m < p[i][1]:
			ans[p[i][2]] = index + 1
		else:
			index = p[i][2]
			m = p[i][1]
	print(*ans)


t = int(input())
big = 2 * 10 ** 9
for i in range(0, t):
	n = int(input())
	ans = [-1] * n
	pairs = []
	for j in range(0, n):
		nextStr = list(map(int, input().split(' ')))
		nextStr.append(j)
		pairs.append(nextStr)
	pairs.sort()
	k = 0
	mn = [0] * n
	ps = [0] * n
	for j in range(0, n):
		if j == 0 or pairs[j][1] < mn[j - 1]:
			mn[j] = pairs[j][1]
			ps[j] = pairs[j][2]
		else:
			mn[j] = mn[j - 1]
			ps[j] = ps[j - 1]
		while k < n and pairs[k][0] < pairs[j][0]:
			k += 1
		if not k == 0 and pairs[j][1] > mn[k - 1]:
			ans[pairs[j][2]] = ps[k - 1] + 1
	for j in range(0, n):
		if not ans[pairs[j][2]] == -1:
			continue
		l = 0
		r = n - 1
		while l < r:
			d = l + r + 1 >> 1
			if pairs[d][0] >= pairs[j][1]:
				r = d - 1
			else:
				l = d
		if pairs[j][1] <= pairs[l][0]:
			continue
		if pairs[j][0] > mn[l]:
			ans[pairs[j][2]] = ps[l] + 1
	print(*ans)


import bisect
for t in range(int(input())):
	n = int(input())
	hw = []
	for i in range(n):
		(h, w) = map(int, input().split())
		hw.append([h, w, i + 1])
	shw = sorted(hw)
	mw = [[shw[0][1], shw[0][2]]]
	for i in range(1, n):
		(w, j) = mw[-1]
		if shw[i][1] < w:
			(w, j) = (shw[i][1], shw[i][2])
		mw.append([w, j])
	ans = []
	for (h, w, _) in hw:
		a = -1
		for (y, x) in [[h, w], [w, h]]:
			i = bisect.bisect_left(shw, [y, 0]) - 1
			if i >= 0 and mw[i][0] < x:
				a = mw[i][1]
				break
		ans.append(a)
	print(*ans)


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

def func(arr, x):
	n = len(arr)
	start = 0
	end = n - 1
	y = -1
	while start <= end:
		mid = (start + end) // 2
		if arr[mid] < x:
			y = mid
			start = mid + 1
		else:
			end = mid - 1
	return y
t = int(input())
for i in range(t):
	n = int(input())
	arr = []
	for i in range(n):
		(h, w) = map(int, input().split())
		arr.append((i, h, w))
	arr.sort(key=lambda x: x[1])
	hh1 = []
	ww1 = []
	for i in range(n):
		hh1.append(arr[i][1])
		if i == 0:
			ww1.append(i)
		elif arr[i][2] <= arr[ww1[-1]][2]:
			ww1.append(i)
		else:
			ww1.append(ww1[-1])
	ans = [-1] * n
	for i in range(n):
		x = bisect_left(hh1, arr[i][1])
		if x - 1 != -1 and arr[ww1[x - 1]][2] < arr[i][2]:
			ans[arr[i][0]] = arr[ww1[x - 1]][0] + 1
	arr.sort(key=lambda x: x[2])
	hh2 = []
	ww2 = []
	for i in range(n):
		ww2.append(arr[i][2])
		if i == 0:
			hh2.append(i)
		elif arr[i][1] <= arr[hh2[-1]][1]:
			hh2.append(i)
		else:
			hh2.append(hh2[-1])
	for i in range(n):
		x = func(ww2, arr[i][1])
		if x == i:
			x = x - 1
		if x != -1 and arr[hh2[x]][1] < arr[i][2]:
			ans[arr[i][0]] = arr[hh2[x]][0] + 1
	print(*ans)


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
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()
inf = 10 ** 16
md = 10 ** 9 + 7
for _ in range(II()):
	hw = []
	n = II()
	for i in range(n):
		(h, w) = MI()
		if h > w:
			(h, w) = (w, h)
		hw.append((h, w, i))
	hw.sort()
	ci = pi = pmn = mn = inf
	ph = -1
	ans = [-1] * n
	for (h, w, i) in hw:
		if h != ph:
			pmn = mn
			pi = ci
			ph = h
		if w > pmn:
			ans[i] = pi + 1
		if w < mn:
			mn = w
			ci = 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 heapq import heapify, heappush as hp, heappop as p
from collections import defaultdict as dd
for t in range(int(input())):
	n = int(input())
	l = []
	d1 = dd(list)
	for i in range(n):
		(x, y) = map(int, input().split())
		l.append([x, y])
		d1[x].append([y, i + 1])
		d1[y].append([x, i + 1])
	ans = [-1] * n
	l2 = []
	l1 = []
	for i in d1:
		l2.append(i)
	l2.sort()
	for i in l2:
		l1.append([i, d1[i]])
	h = []
	heapify(h)
	for i in l1:
		if len(h):
			for j in i[1]:
				b = p(h)
				if b[0] < j[0]:
					ans[j[1] - 1] = b[1]
				hp(h, b)
		for j in i[1]:
			hp(h, j)
	print(*ans)


for _ in range(int(input())):
	n = int(input())
	frds = [sorted(map(int, input().split(' '))) + [i] for i in range(n)]
	overlap = [-1] * n
	frds = sorted(frds, key=lambda x: x[1], reverse=True)
	frds = sorted(frds, key=lambda x: x[0])
	(minh, minw, minfr) = frds[0]
	notsame_frds = []
	for (hi, wi, fri) in frds:
		if wi > minw:
			notsame_frds.append((hi, wi, fri))
			overlap[fri] = minfr + 1
		else:
			notsame_frds.append((wi, hi, fri))
			minw = wi
			minfr = fri
	notsame_frds = sorted(notsame_frds, key=lambda x: x[1], reverse=True)
	notsame_frds = sorted(notsame_frds, key=lambda x: x[0])
	(minh, minw, minfr) = notsame_frds[0]
	for (hi, wi, fri) in notsame_frds:
		if overlap[fri] != -1:
			continue
		if wi > minw:
			overlap[fri] = minfr + 1
		else:
			minw = wi
			minfr = fri
	print(*overlap)


import sys
input = sys.stdin.readline
from bisect import bisect_left
for _ in range(int(input())):
	n = int(input())
	l = []
	ans = [-1] * n
	for i in range(n):
		(h, w) = map(int, input().split())
		l.append((h, w, i))
	o = l.copy()
	o.sort()
	pa = []
	pb = []
	lol = []
	for i in o:
		e1 = i[0]
		e2 = i[1]
		pa.append(e1)
		pb.append(e2)
		lol.append(i[2])
	d = {}
	suf = []
	mi = 10 ** 18
	cnt = 0
	for i in pb:
		if i < mi:
			d[i] = cnt
			mi = i
		suf.append(mi)
		cnt += 1
	for i in l:
		flag = 0
		e1 = i[0]
		e2 = i[1]
		ind = bisect_left(pa, e1, 0, n)
		if ind != 0:
			if suf[ind - 1] < e2:
				ind_dash = d[suf[ind - 1]]
				ele = lol[ind_dash] + 1
				ans[i[2]] = ele
				continue
		e1 = i[1]
		e2 = i[0]
		ind = bisect_left(pa, e1, 0, n)
		if ind != 0:
			if suf[ind - 1] < e2:
				ind_dash = d[suf[ind - 1]]
				ele = lol[ind_dash] + 1
				ans[i[2]] = ele
				continue
	print(*ans)


import bisect

def find_foregrounds(friends):
	rotated_friends = [(w, h, i) for (i, (h, w)) in enumerate(friends)]
	all_friends = [(h, w, i) for (i, (h, w)) in enumerate(friends)] + rotated_friends
	all_friends.sort(key=lambda x: x[0], reverse=True)
	all_friends_minus_h = [-h for (h, w, i) in all_friends]
	min_w = -1
	min_w_index = -1
	all_friends_minw = [None] * len(all_friends)
	k = len(all_friends_minw) - 1
	for (h, w, i) in all_friends[::-1]:
		if min_w < 0 or w < min_w:
			min_w = w
			min_w_index = i
		all_friends_minw[k] = (min_w, min_w_index)
		k -= 1
	result = []
	for (h, w) in friends:
		index = bisect.bisect_right(all_friends_minus_h, -h)
		if index < len(all_friends):
			(min_w, min_w_index) = all_friends_minw[index]
			if min_w < w:
				match_index = min_w_index + 1
			else:
				match_index = -1
		else:
			match_index = -1
		result.append(match_index)
	return result
n_samples = int(input())
for t in range(n_samples):
	n = int(input())
	friends = []
	for _ in range(n):
		(h, w) = list(map(int, input().split(' ')))
		friends.append((h, w))
	possible_foregrounds = find_foregrounds(friends)
	print(' '.join(map(str, possible_foregrounds)))


import sys
from bisect import bisect_left
input = sys.stdin.buffer.readline
T = int(input())
for _ in range(T):
	n = int(input())
	fr = []
	for i in range(n):
		(a, b) = map(int, input().split())
		fr.append((a, b, i + 1))
	(ls, pos) = (list(sorted(fr)), [])
	for (_, b, p) in ls:
		(b0, p0) = (10 ** 9 + 1, 0)
		if pos:
			(b0, p0) = (pos[-1][0], pos[-1][1])
		if b < b0:
			pos.append((b, p))
		else:
			pos.append((b0, p0))
	res = []
	for (a, b, _) in fr:
		p1 = bisect_left(ls, (a, 0, 0)) - 1
		p2 = bisect_left(ls, (b, 0, 0)) - 1
		if p1 >= 0 and pos[p1][0] < b:
			res.append(pos[p1][1])
		elif p2 >= 0 and pos[p2][0] < a:
			res.append(pos[p2][1])
		else:
			res.append(-1)
	print(*res)


from heapq import heappush
import sys
for _ in range(int(sys.stdin.readline())):
	n = int(sys.stdin.readline())
	all_dudes = []
	for i in range(n):
		(w, h) = map(int, sys.stdin.readline().split())
		all_dudes.append((w, h, i))
		all_dudes.append((h, w, i))
	all_dudes.sort()
	done_dudes = []
	pend_dudes = []
	ans = [-1 for i in range(n)]
	for (w, h, i) in all_dudes:
		if pend_dudes and pend_dudes[0][1] < w:
			for dude in pend_dudes:
				heappush(done_dudes, dude)
			pend_dudes.clear()
		if done_dudes:
			(ch, cw, ci) = done_dudes[0]
			if cw < w and ch < h:
				ans[i] = ci + 1
		pend_dudes.append((h, w, i))
	print(*ans)


t = int(input())
for _ in range(t):
	n = int(input())
	lst = []
	for times in range(n):
		(w, h) = map(int, input().split())
		if w > h:
			lst.append([h, w, times])
		else:
			lst.append([w, h, times])
	lst = sorted(lst, key=lambda x: (x[0], -x[1]))
	minn = lst[0][1]
	so = lst[0][2]
	t = 0
	ans = [-1] * n
	for i in lst:
		if i[1] <= minn:
			minn = min(minn, i[1])
			so = i[2] + 1
		else:
			ans[i[2]] = so
	for i in ans:
		print(i)


def corrPlace(allPoint):
	n = len(allPoint)
	ans = [-1] * n
	allPoint.sort(reverse=True)
	message = [allPoint[-1]]
	for i in range(n - 2, -1, -1):
		if message[-1][0] != allPoint[i][0] and message[-1][1] > allPoint[i][1]:
			message.append(allPoint[i])
	cachePlace = len(message) - 1
	for imess in allPoint:
		ai = imess[0]
		bi = imess[1]
		while cachePlace != 0 and message[cachePlace][0] >= ai:
			cachePlace -= 1
		if message[cachePlace][0] < ai and message[cachePlace][1] < bi:
			ans[imess[2] - 1] = message[cachePlace][2]
	return ans
t = int(input())
for qwq in range(t):
	n = int(input())
	allPoint = []
	for i in range(n):
		l = list(map(int, input().split()))
		allPoint.append((max(l), min(l), i + 1))
	ans = corrPlace(allPoint)
	for i in ans:
		print(i, end=' ')
	print()


for _ in range(int(input())):
	n = int(input())
	a = [[0, 0, 0] for _ in range(n)]
	for (i, x) in enumerate(a):
		(x[0], x[1]) = map(int, input().split())
		if x[0] > x[1]:
			(x[0], x[1]) = (x[1], x[0])
		x[2] = i
	a.sort()
	ans = [-1] * n
	min_w = -1
	(i, j) = (0, 0)
	while i < n:
		while j < n and a[i][0] == a[j][0]:
			j += 1
		if min_w >= 0:
			for k in range(i, j):
				if a[min_w][1] < a[k][1]:
					ans[a[k][2]] = a[min_w][2] + 1
		for k in range(i, j):
			if min_w < 0 or a[min_w][1] > a[k][1]:
				min_w = k
		i = j
	print(*ans)


import sys
from collections import defaultdict
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	pairs = []
	for i in range(n):
		(d_min, d_max) = sorted(map(int, sys.stdin.readline().split()))
		pairs.append((d_max, (d_min, i + 1)))
	pairs = list(sorted(pairs, key=lambda x: (-x[0], x[1])))
	subsequent_min = [(2000000000, -1)] * n
	for i in reversed(range(n - 1)):
		if pairs[i + 1][0] == pairs[i][0]:
			subsequent_min[i] = subsequent_min[i + 1]
		else:
			subsequent_min[i] = min(pairs[i + 1][1], subsequent_min[i + 1])
	results = defaultdict(lambda : -1)
	for (i, (_, (d_min, search_id))) in enumerate(pairs):
		if d_min > subsequent_min[i][0]:
			results[search_id] = subsequent_min[i][1]
	print(' '.join((str(results[i]) for i in range(1, n + 1))))


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import List
sys.setrecursionlimit(99999)
t = int(input())
for _ in range(t):
	n = int(input())
	hg = collections.defaultdict(list)
	for i in range(n):
		(a, b) = map(int, input().split())
		if a > b:
			(a, b) = (b, a)
		hg[a].append([b, i + 1])
	ans = [-1] * n
	l = [float('inf'), -1]
	for k in sorted(hg.keys()):
		p = sorted(hg[k])
		for (h, ind) in p:
			if h > l[0]:
				ans[ind - 1] = l[1]
		if p[0][0] < l[0]:
			l = p[0]
	print(*ans)


import sys
data = sys.stdin.read().split('\n')[::-1]

def input():
	return data.pop()
res = []

def solve(hs, ws, n):
	friends = sorted(range(n), key=lambda i: (hs[i], ws[i]))
	ans = [-1] * n
	thinnest_shorter_than_current = ws.index(max(ws))
	min_height = min(hs)
	thinnest = thinnest_shorter_than_current
	for (i, f) in enumerate(friends):
		f_prev = friends[i - 1]
		if i == 0 or hs[f_prev] != hs[f]:
			if ws[thinnest_shorter_than_current] > ws[thinnest]:
				thinnest_shorter_than_current = thinnest
			thinnest = f
		if ws[thinnest] > ws[f]:
			f = thinnest
		if hs[f] == min_height:
			continue
		if ws[f] > ws[thinnest_shorter_than_current]:
			ans[f] = thinnest_shorter_than_current + 1
	return ans
for _ in range(int(input())):
	n = int(input())
	hs = [-1] * n
	ws = [-1] * n
	for i in range(n):
		(hs[i], ws[i]) = map(int, input().split())
		if hs[i] > ws[i]:
			(hs[i], ws[i]) = (ws[i], hs[i])
	res.append(' '.join(map(str, solve(hs, ws, n))))
print('\n'.join(res))