Read problems statements in Russian and Vietnamese as well. 
There is a website for playing chess online with other people, whenever you want to play a game, you can either create new game and wait for an opponent to join, or you can join already created game.

Each player has a number representing his rating, after each game the winner will take some rating points from the loser, unless the players agreed to make the game unrated before the start

Whenever you create a game you have first to choose some parameters for this game:

minimum and maximum rating for the opponent that you would like to play with
whether this game will be rated or not
the total thinking time allowed for each player
whether you would like to play with white, black or let the server choose a random color random

after choosing those parameters and creating the game you will wait for an opponent to join your game, after someone joins the game will start.

if you would like to join a game you can see the list of already created games that are still waiting for an opponent and you can see the parameters for each game after that you can select one of them to join.

Chef is one of the developers of this website and today he thought that it's good idea to add new feature to it, the feature is that whenever a player creates a new game before asking the player to wait for an opponent the website will try to automatically find suitable opponent among those who already have created a game and still waiting for opponent, the website should choose the opponent such that all parameters specified by both players must be satisfied, if more than one suitable match exists then the player who just created a game will  be matched with the suitable player who created a game first, if there's no match at all then the website will ask the player to wait for an opponent. 

rules applied to check whether two players can be matched:

rating of each player is inside the range of rating that the other player selected

both players selected the game to be rated, or both of them selected the game to be unrated

total thinking time chosen by both players are equal

both players chose random color, or one of them chose white while the other chose black

Now, given a list of players who created a new game, in the same order of creating, for each player find out if this player should be matched with another player who is at that time waiting for an opponent , if there are more than one possible match, then choose the one who created a game first (thus has waited more), if there is no match then ask the player to wait.

note that once a player is matched with another one then both players can no longer be matched with other players. also in this problem no player will join a game manually, all of them will create new game.

------ Input ------ 

First line of input contains a single integer T denoting the number of test-cases.

First line of each test-case contains a single integer N denoting the number of players who will create a new game

each of the following N lines contains a description of a player, R_{i} Min_{i} Max_{i} Time_{i}  isRated_{i} Color_{i} 

R_{i} is rating of player i, Min_{i} Max_{i} is minimum and maximum rating of the opponent that this player would like to play with, Time_{i} is the total thinking time for each player it can be only one of the following 5 15 30 60, isRated_{i} can be one of the values "rated" or "unrated", Color_{i} can be one of the following "white", "black" or "random"

------ Output ------ 

For each test-case output N lines, i-th line should be the number of the player that player i is matched when he created a new game, or word "wait" if there's no match. players are numbered from 1 to N in same order as input

------ Constraints ------ 

$1 ≤ T ≤ 100$
$1 ≤ N ≤ 100$
$1 ≤ R_{i}  ≤ 1,000,000$
$1 ≤ Min_{i} ≤ Max_{i}  ≤ 1,000,000$
$Color_{i} can only be "white", "black" or "random"$
$isRated_{i} can only be "rated" or "unrated"$
$Time_{i} can only be 5, 15, 30, 60$

----- Sample Input 1 ------ 
1
7
5 1 10 15 rated random
11 1 20 15 rated random
10 3 30 15 rated random
2 5 15 15 rated random
30 20 60 60 unrated white
50 20 40 60 unrated random
50 20 40 60 unrated black
----- Sample Output 1 ------ 
wait
wait
1
2
wait
wait
5
----- explanation 1 ------ 
Example case 1.

player 1 has to wait because there are no other players yet
player 2 has to wait because he can't play with player 1 because player 2 has rating 11 while player 1 want opponent rating to be 10 at max
player 3 can be with player 1 or player 2, so he will play with player 1 because he created a game first
player 4 can be with player 1 or player 2, but player 1 already entered a game with player 3 so he is unavailable now so player 4 will play with player  2
player 5 has to wait because there are no other players
player 6 has to wait because he can't play with player 5 because player 6 wants colored to be selected at random, while player 5 wants to take white
player 7 will play with player 5

Source: codechef | Difficulty: MEDIUM_HARD
Tags: Implementation


Solutions:
import sys
import math

def main(arr):
	for e in arr:
		e[0] = int(e[0])
		e[2] = int(e[2])
		e[1] = int(e[1])
		e[3] = int(e[3])
		e += [True]
	ans = ['wait']
	for i in range(1, len(arr)):
		p1 = arr[i]
		for j in range(0, i):
			p2 = arr[j]
			if p2[-1]:
				if p2[1] <= p1[0] and p1[0] <= p2[2] and (p1[1] <= p2[0]) and (p2[0] <= p1[2]):
					if p2[3] == p1[3] and p2[4] == p1[4]:
						if p1[5] == 'random' and p2[5] == 'random' or (p1[5] == 'black' and p2[5] == 'white') or (p2[5] == 'black' and p1[5] == 'white'):
							ans += [j + 1]
							p1[-1] = False
							p2[-1] = False
							break
			if j == i - 1:
				ans += ['wait']
	for e in ans:
		print(e)
t = int(input())
for i in range(t):
	n = int(input())
	arr = []
	for j in range(n):
		arr.append(list(map(str, input().split())))
	main(arr)


def solve():
	n = int(input())
	arr = []
	for j in range(n):
		a = list(input().split())
		a[0] = int(a[0])
		a[1] = int(a[1])
		a[2] = int(a[2])
		a[3] = int(a[3])
		arr.append(a)
	res = ['wait']
	c = 0
	for j in range(1, n):
		for k in range(0, j):
			if arr[k][0] != 0 and arr[j][1] <= arr[k][0] <= arr[j][2] and (arr[k][1] <= arr[j][0] <= arr[k][2]) and (arr[k][3] == arr[j][3]) and (arr[k][4] == arr[j][4]) and (arr[j][5] == arr[k][5] == 'random' or (arr[j][5] == 'white' and arr[k][5] == 'black') or (arr[j][5] == 'black' and arr[k][5] == 'white')):
				c = 1
				res.append(k + 1)
				arr[k][0] = 0
				arr[j][0] = 0
				break
		if c == 0:
			res.append('wait')
		c = 0
	print(*res, sep='\n')
for _ in range(int(input())):
	solve()


def fun():
	test = int(input())
	for i in range(test):
		arr = []
		n = int(input())
		for i in range(n):
			done = [0] * n
			(r, mini, maxi, time, rated, color) = input().split()
			(r, mini, maxi, time) = (int(r), int(mini), int(maxi), int(time))
			if color == 'black':
				color = 1
			elif color == 'white':
				color = 2
			elif color == 'random':
				color = 0
			rated = int(rated == 'rated')
			arr.append([r, mini, maxi, time, rated, color])
		print('wait')
		for i in range(1, n):
			for j in range(0, i):
				if done[j] == 0 and done[i] == 0:
					if arr[i][3] == arr[j][3] and arr[i][4] == arr[j][4]:
						if arr[i][5] + arr[j][5] in [0, 3]:
							if arr[j][1] <= arr[i][0] <= arr[j][2] and arr[i][1] <= arr[j][0] <= arr[i][2]:
								done[i] = 1
								done[j] = 1
								print(j + 1)
								break
			else:
				print('wait')
fun()


for _ in range(int(input())):
	n = int(input())
	w = []
	for i in range(n):
		a = list(input().split())
		(a[0], a[1], a[2], a[3]) = (int(a[0]), int(a[1]), int(a[2]), int(a[3]))
		f = 0
		if len(w) == 0:
			print('wait')
			w.append([i + 1, a])
			continue
		for j in range(len(w)):
			b = w[j][1]
			if (a[0] >= b[1] and a[0] <= b[2] and (b[0] >= a[1]) and (b[0] <= a[2])) and b[3] == a[3] and (b[4] == a[4]) and (a[5] == 'white' and b[5] == 'black' or (b[5] == 'white' and a[5] == 'black') or (a[5] == 'random' and b[5] == 'random')):
				print(w[j][0])
				del w[j]
				f = 1
				break
		if f == 0:
			print('wait')
			w.append([i + 1, a])


for i in range(int(input())):
	n = int(input())
	l1 = []
	for i in range(n):
		l1.append(list(map(str, input().split())))
	l2 = []
	for i in range(n):
		f = 0
		for j in range(i):
			if j not in l2:
				if int(l1[j][1]) <= int(l1[i][0]) <= int(l1[j][2]) and int(l1[i][1]) <= int(l1[j][0]) <= int(l1[i][2]) and (l1[i][3] == l1[j][3]) and (l1[i][4] == l1[j][4]) and (l1[i][5] == 'random' and l1[j][5] == 'random' or (l1[i][5] == 'white' and l1[j][5] == 'black') or (l1[j][5] == 'white' and l1[i][5] == 'black')):
					print(j + 1)
					f = 1
					l2.append(j)
					l2.append(i)
					break
		if f == 0:
			print('wait')


t = int(input())
for i in range(t):
	n = int(input())
	r = []
	for j in range(n):
		r.append(input().split())
		(r[j][0], r[j][1], r[j][2], r[j][3]) = (int(r[j][0]), int(r[j][1]), int(r[j][2]), int(r[j][3]))
	w = []
	for j in range(n):
		flag = 0
		for k in range(len(w)):
			if w[k] == 'wait':
				c1 = r[k][1] <= r[j][0] <= r[k][2] and r[j][1] <= r[k][0] <= r[j][2]
				c2 = r[j][3] == r[k][3]
				c3 = r[j][4] == r[k][4]
				c4 = r[j][5] == r[k][5] == 'random' or (r[j][5] != r[k][5] and r[j][5] != 'random' and (r[k][5] != 'random'))
				if c1 and c2 and c3 and c4:
					flag = 1
					print(k + 1)
					w.append(k)
					w[k] = j
					break
		if flag == 0:
			w.append('wait')
			print('wait')


for case in range(int(input())):
	N = int(input())
	print('wait')
	(R, Min, Max, color, rated, Time, match) = ([0] * N, [0] * N, [0] * N, [0] * N, [0] * N, [0] * N, [0] * N)
	for i in range(N):
		p = input().split()
		(R[i], Min[i], Max[i], Time[i], rated[i]) = (int(p[0]), int(p[1]), int(p[2]), int(p[3]), p[4])
		if p[5][0] == 'w':
			color[i] = 1
		elif p[5][0] == 'b':
			color[i] = -1
		else:
			color[i] = 0
		match[i] = 0
	for i in range(1, N):
		for j in range(i):
			if match[i] + match[j] == 0:
				if (Min[j] <= R[i] and R[i] <= Max[j]) and (Min[i] <= R[j] and R[j] <= Max[i]):
					if rated[i] == rated[j]:
						if Time[i] == Time[j]:
							if color[i] + color[j] == 0:
								match[i] = match[j] = 1
								print(j + 1)
								break
		else:
			print('wait')


def chkRated(r):
	if r == 'rated':
		return True
	else:
		return False

def chkColor(c):
	if c == 'white':
		return -1
	elif c == 'black':
		return 1
	else:
		return 0
t = int(input())
for x in range(t):
	n = int(input())
	R = [0] * n
	Min = [0] * n
	Max = [0] * n
	Time = [0] * n
	isRated = [False] * n
	Color = [0] * n
	isMatched = [False] * n
	tmpRated = 0
	tmpColor = 0
	for i in range(n):
		(R[i], Min[i], Max[i], Time[i], tmpRated, tmpColor) = input().split()
		R[i] = int(R[i])
		Min[i] = int(Min[i])
		Max[i] = int(Max[i])
		Time[i] = int(Time[i])
		isRated[i] = chkRated(tmpRated)
		Color[i] = chkColor(tmpColor)
	for i in range(n):
		flag = 0
		for j in range(i):
			if not isMatched[j]:
				if (R[i] <= Max[j] and R[i] >= Min[j]) and (R[j] <= Max[i] and R[j] >= Min[i]):
					if isRated[i] == isRated[j] and Time[i] == Time[j]:
						if Color[i] == Color[j] == 0 or Color[i] * Color[j] == -1:
							flag = 1
							isMatched[i] = True
							isMatched[j] = True
							print(j + 1)
							break
		if flag == 0:
			print('wait')


for i in range(int(input())):
	n = int(input())
	a = []
	b = []
	c = 0
	for j in range(n):
		l = input().split()
		f = 0
		for k in range(c):
			if int(l[1]) <= int(a[k][0]) <= int(l[2]) and int(a[k][1]) <= int(l[0]) <= int(a[k][2]) and (a[k][3] == l[3]) and (a[k][4] == l[4]) and (a[k][5] == l[5] and l[5] == 'random' or (a[k][5] != l[5] and l[5] != 'random' and (a[k][5] != 'random'))):
				f = 1
				x = b[k]
				del a[k]
				del b[k]
				break
		if f == 0:
			print('wait')
			a.append(l)
			b.append(j + 1)
			c += 1
		else:
			print(x)
			c -= 1


for _ in range(int(input())):
	n = int(input())
	arr = []
	for j in range(n):
		g = list(input().split())
		g[0] = int(g[0])
		g[1] = int(g[1])
		g[2] = int(g[2])
		g[3] = int(g[3])
		arr.append(g)
	ans = ['wait']
	c = 0
	for j in range(1, n):
		for k in range(0, j):
			if arr[k][0] != 0 and arr[j][1] <= arr[k][0] <= arr[j][2] and (arr[k][1] <= arr[j][0] <= arr[k][2]) and (arr[k][3] == arr[j][3]) and (arr[k][4] == arr[j][4]) and (arr[j][5] == arr[k][5] == 'random' or (arr[j][5] == 'white' and arr[k][5] == 'black') or (arr[j][5] == 'black' and arr[k][5] == 'white')):
				c = 1
				ans.append(k + 1)
				arr[k][0] = 0
				arr[j][0] = 0
				break
		if c == 0:
			ans.append('wait')
		c = 0
	print(*ans, sep='\n')


T = int(input())
for _ in range(T):
	N = int(input())
	arr = [0] * N
	for i in range(N):
		arr[i] = [0] + list(input().strip().split())
		(arr[i][1], arr[i][2], arr[i][3], arr[i][4]) = (int(arr[i][1]), int(arr[i][2]), int(arr[i][3]), int(arr[i][4]))
		flag = 0
		for j in range(i):
			if arr[j][0] != 1:
				c1 = arr[i][2] <= arr[j][1] and arr[j][1] <= arr[i][3]
				c2 = arr[j][2] <= arr[i][1] and arr[i][1] <= arr[j][3]
				c3 = arr[j][4] == arr[i][4]
				c4 = arr[j][5] == arr[i][5]
				c5 = arr[j][-1] + arr[i][-1] in {'randomrandom', 'blackwhite', 'whiteblack'}
				if c1 and c2 and c3 and c4 and c5:
					flag = 1
					(arr[i][0], arr[j][0]) = (1, 1)
					print(j + 1)
					break
		if flag == 0:
			print('wait')


def fun():
	n = int(input())
	actual = []
	time = [5, 15, 30, 60]
	for i in range(n):
		val = 0
		a = list(map(str, input().split()))
		if int(a[3]) in time:
			arr = [int(a[0]), int(a[1]), int(a[2]), int(a[3]), a[4], a[5], i]
			if len(actual) == 0:
				print('wait')
			else:
				for a in actual:
					if arr[0] in range(a[1], a[2] + 1) and a[0] in range(arr[1], arr[2] + 1) and (arr[3] == a[3]) and (a[4] == arr[4]) and (arr[5] == 'white' and a[5] == 'black' or (arr[5] == 'black' and a[5] == 'white') or (arr[5] == 'random' and a[5] == 'random')):
						print(a[6] + 1)
						actual.remove(a)
						val = 1
						break
				if val == 0:
					print('wait')
			if val == 0:
				actual.append(arr)
for _ in range(int(input())):
	fun()


def condition(i, j, hmap):
	flag = 0
	if hmap[j][-1] == 1:
		flag = 1
	if int(hmap[j][0]) < int(hmap[i][1]) or int(hmap[j][0]) > int(hmap[i][2]) or int(hmap[i][0]) < int(hmap[j][1]) or (int(hmap[i][0]) > int(hmap[j][2])):
		flag = 1
	if int(hmap[i][3]) != int(hmap[j][3]):
		flag = 1
	if hmap[i][4] != hmap[j][4]:
		flag = 1
	if hmap[i][5] != 'random' and hmap[j][5] == 'random' or (hmap[j][5] != 'random' and hmap[i][5] == 'random'):
		flag = 1
	if hmap[i][5] == 'black' and hmap[j][5] == 'black' or (hmap[i][5] == 'white' and hmap[j][5] == 'white'):
		flag = 1
	if flag == 1:
		return False
	return True
T = int(input())
for _ in range(T):
	N = int(input())
	hmap = {}
	for i in range(N):
		hmap[i] = list(map(str, input().split())) + [0]
	for i in range(N):
		flag = 0
		for j in range(i):
			if condition(i, j, hmap):
				print(j + 1)
				flag = 1
				hmap[j][-1] = 1
				hmap[i][-1] = 1
				break
		if flag == 0:
			print('wait')


class jamal:

	def __init__(self, a, i):
		self.my = int(a[0])
		self.mi = int(a[1])
		self.ma = int(a[2])
		self.t = int(a[3])
		self.israt = a[4]
		self.col = a[5]
		self.ismatched = 0
		self.ind = i
t = int(input())
for _ in range(t):
	n = int(input())
	li = []
	a = list(input().split())
	print('wait')
	playa = jamal(a, 1)
	li.append(playa)
	for i in range(1, n):
		a = list(input().split())
		playa = jamal(a, i + 1)
		l = len(li)
		for j in range(l):
			if playa.mi <= li[j].my <= playa.ma and li[j].mi <= playa.my <= li[j].ma and (playa.israt == li[j].israt) and (playa.t == li[j].t) and (playa.col == li[j].col == 'random' or (playa.col == 'black' and li[j].col == 'white') or (playa.col == 'white' and li[j].col == 'black')) and (not li[j].ismatched):
				playa.ismatched = 1
				li[j].ismatched = 1
				print(li[j].ind)
				break
		if not playa.ismatched:
			print('wait')
			li.append(playa)


t = int(input())
while t > 0:
	n = int(input())
	nn = n
	inp = []
	flag = []
	flagg = []
	while n > 0:
		flag.append('wait')
		flagg.append(0)
		a = list(input().split())
		inp.append(a)
		n -= 1
	for i in range(nn):
		for j in range(0, i):
			if flagg[j] == 0:
				r1 = int(inp[i][0])
				r2 = int(inp[j][0])
				mi1 = int(inp[i][1])
				mi2 = int(inp[j][1])
				ma1 = int(inp[i][2])
				ma2 = int(inp[j][2])
				ti = int(inp[i][3])
				tj = int(inp[j][3])
				if r1 >= mi2 and r1 <= ma2:
					if r2 >= mi1 and r2 <= ma1:
						if ti == tj:
							if inp[i][4] == inp[j][4]:
								if inp[i][5] == inp[j][5] and inp[i][5] == 'random' or (inp[i][5] != inp[j][5] and inp[i][5] != 'random' and (inp[j][5] != 'random')):
									flag[i] = j + 1
									flagg[i] = -1
									flagg[j] = -1
									break
	for i in flag:
		print(i)
	t -= 1


t = int(input())
for i in range(t):
	n = int(input())
	lis = [[0 for j in range(6)] for k in range(n)]
	ans = list()
	oth = list()
	for j in range(n):
		d = list(map(str, input().split()))
		lis[j] = d
	ans.append('wait')
	for j in range(1, n, 1):
		f = 0
		for k in range(0, j, 1):
			if k not in ans and k not in oth:
				if int(lis[k][0]) >= int(lis[j][1]) and int(lis[k][0]) <= int(lis[j][2]) and (int(lis[j][0]) >= int(lis[k][1])) and (int(lis[j][0]) <= int(lis[k][2])):
					if lis[k][3] == lis[j][3]:
						if lis[k][4] == lis[j][4]:
							if lis[k][5] == 'random' and lis[j][5] == 'random' or (lis[k][5] == 'white' and lis[j][5] == 'black') or (lis[k][5] == 'black' and lis[j][5] == 'white'):
								f = 1
								break
		if f == 0:
			ans.append('wait')
		else:
			ans.append(k)
			oth.append(j)
	for j in range(len(ans)):
		if ans[j] != 'wait':
			print(ans[j] + 1)
		else:
			print(ans[j])


for _ in range(int(input())):
	x = []
	n = int(input())
	for i in range(n):
		a = list(input().split())
		a.append('av')
		x.append(a)
		flag = 0
	print('wait')
	for i in range(1, n):
		for j in range(0, i):
			flag = 0
			if int(x[j][0]) >= int(x[i][1]) and int(x[j][0]) <= int(x[i][2]) and (int(x[i][0]) >= int(x[j][1])) and (int(x[i][0]) <= int(x[j][2])) and int(x[j][3] == x[i][3]) and (x[j][4] == x[i][4]) and (x[j][6] == 'av'):
				if x[j][5] == 'random' and x[i][5] == 'random':
					x[j][6] = 'xxx'
					x[i][6] = 'xxx'
					flag = 1
					break
				elif x[j][5] == 'white' and x[i][5] == 'black':
					flag = 1
					x[j][6] = 'xxx'
					x[i][6] = 'xxx'
					break
				elif x[j][5] == 'black' and x[i][5] == 'white':
					flag = 1
					x[j][6] = 'xxx'
					x[i][6] = 'xxx'
					break
		if flag == 0:
			print('wait')
		else:
			print(j + 1)


for _ in range(int(input())):
	n = int(input())
	a = []
	l = []
	for i in range(n):
		f = 0
		b = list(map(str, input().split()))
		a.append(b)
		for j in range(0, i):
			if f == 0 and int(a[i][0]) >= int(a[j][1]) and (int(a[i][0]) <= int(a[j][2])) and (int(a[j][0]) >= int(a[i][1])) and (int(a[j][0]) <= int(a[i][2])) and (int(a[j][3]) == int(a[i][3])) and (a[j][4] == a[i][4]):
				if a[i][5] == 'random' and a[j][5] == 'random' or (a[i][5] == 'white' and a[j][5] == 'black') or (a[i][5] == 'black' and a[j][5] == 'white'):
					if not int(j) in l:
						f = 1
						print(int(j) + 1)
						l.append(int(j))
						l.append(int(i))
		if f == 0:
			print('wait')


t = int(input())
for i in range(t):
	n = int(input())
	a = [None for j in range(n)]
	for j in range(n):
		a[j] = input().split()
		a[j][0] = int(a[j][0])
		a[j][1] = int(a[j][1])
		a[j][2] = int(a[j][2])
		a[j][3] = int(a[j][3])
		a[j].append(j + 1)
	b = list()
	b.append(a[0])
	print('wait')
	for j in range(1, n):
		c = 2
		for k in range(len(b)):
			c = 1
			if a[j][0] >= b[k][1] and a[j][0] <= b[k][2] and (b[k][0] >= a[j][1]) and (b[k][0] <= a[j][2]) and (a[j][3] == b[k][3]) and (a[j][4] == b[k][4]):
				if a[j][5] == b[k][5] == 'random' or (a[j][5] == 'white' and b[k][5] == 'black') or (a[j][5] == 'black' and b[k][5] == 'white'):
					print(b[k][6])
					b.pop(k)
					c = 0
					break
		if c == 1 or c == 2:
			b.append(a[j])
			print('wait')


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = []
	aa = []
	for ans in range(1, n + 1):
		found = False
		x = list(input().split())
		a.append([int(x[0]), int(x[1]), int(x[2]), int(x[3]), x[4], x[5]])
		aa.append([int(x[0]), int(x[1]), int(x[2]), int(x[3]), x[4], x[5]])
		for i in a[:-1]:
			j = a[-1]
			if i[4] == j[4] and i[3] == j[3]:
				if i[5] == j[5] == 'random':
					if i[0] >= j[1] and i[0] <= j[2] and (j[0] >= i[1]) and (j[0] <= i[2]):
						found = True
						print(aa.index(i) + 1)
						a.remove(i)
						a.remove(j)
						break
				elif i[5] != j[5] and i[5] in ['black', 'white'] and (j[5] in ['black', 'white']):
					if i[0] >= j[1] and i[0] <= j[2] and (j[0] >= i[1]) and (j[0] <= i[2]):
						found = True
						print(aa.index(i) + 1)
						a.remove(i)
						a.remove(j)
						break
				else:
					continue
			else:
				continue
		if not found:
			print('wait')


t = int(input())
for i in range(t):
	n = int(input())
	Ans = []
	for i in range(n):
		l = input().split()
		R = int(l[0])
		Min = int(l[1])
		Max = int(l[2])
		T = int(l[3])
		Ra = l[4]
		Co = l[5]
		l.append(0)
		flg = False
		for j in range(len(Ans)):
			if R >= int(Ans[j][1]) and R <= int(Ans[j][2]) and (int(Ans[j][0]) >= Min) and (int(Ans[j][0]) <= Max) and (Ra == Ans[j][4]) and (T == int(Ans[j][3])) and (Co == Ans[j][5] and Co == 'random' or (Co == 'white' and Ans[j][5] == 'black') or (Co == 'black' and Ans[j][5] == 'white')) and (not Ans[j][6]):
				flg = True
				Ans[j][6] = 1
				l[6] = 1
				print(j + 1)
				break
		if not flg:
			print('wait')
		else:
			pass
		Ans.append(l)


for j in range(int(input())):
	(n, arr, done) = (int(input()), [], [])
	for k in range(0, n):
		p = -1
		arr.append(input().split())
		for i in range(k):
			if i not in done:
				if int(arr[i][3]) != int(arr[k][3]) or arr[i][4] != arr[k][4]:
					continue
				if int(arr[i][0]) not in range(int(arr[k][1]), int(arr[k][2]) + 1) or int(arr[k][0]) not in range(int(arr[i][1]), int(arr[i][2]) + 1):
					continue
				if arr[i][-1] == 'random':
					if arr[k][-1] != 'random':
						continue
				elif arr[i][-1] == 'white':
					if arr[k][-1] != 'black':
						continue
				elif arr[i][-1] == 'black':
					if arr[k][-1] != 'white':
						continue
				p = i
				done.append(i)
				done.append(k)
				break
		if p == -1:
			print('wait')
		else:
			print(p + 1)


def match(a, b):
	if a[0] >= b[1] and a[0] <= b[2] and (b[0] >= a[1]) and (b[0] <= a[2]):
		if a[3] == b[3] and a[4] == b[4]:
			if a[5] == 'white' and b[5] == 'black' or (a[5] == 'black' and b[5] == 'white') or (a[5] == 'random' and b[5] == 'random'):
				return 1
	return 0
t = int(input())
a = []
left = []
for z in range(t):
	left.clear()
	a.clear()
	n = int(input())
	for y in range(n):
		left.append(y)
		b = input().split(' ')
		a.append([int(b[0]), int(b[1]), int(b[2]), int(b[3]), b[4], b[5]])
		found = 0
		for x in left[:-1]:
			if match(a[x], a[y]):
				found = 1
				print(x + 1)
				left.remove(x)
				left.remove(y)
				break
		if not found:
			print('wait')


class player:

	def __init__(self):
		self.rat = 0
		self.minrate = 0
		self.maxrate = 0
		self.tim = 0
		self.rated = -3
		self.color = -1
		self.num = 0
		self.done = -1
		self.match = -2

	def __repr__(self):
		return str(self.rat) + ' ' + str(self.minrate) + ' ' + str(self.maxrate) + ' ' + str(self.tim) + ' ' + str(self.rated) + ' ' + str(self.color) + ' ' + str(self.done)

def func():
	n = int(input())
	pla_lst = []
	for i in range(n):
		da = input().split()
		z = player()
		z.rat = int(da[0])
		z.minrate = int(da[1])
		z.maxrate = int(da[2])
		z.tim = int(da[3])
		if da[4] == 'rated':
			z.rated = 1
		else:
			z.rated = 0
		if da[5] == 'random':
			z.color = 1
		elif da[5] == 'white':
			z.color = 2
		else:
			z.color = 3
		z.num = i + 1
		pla_lst.append(z)
	for i in range(n):
		if pla_lst[i].done == 1:
			continue
		for j in range(i):
			if i == j or pla_lst[j].done == 1:
				continue
			if pla_lst[i].rated != pla_lst[j].rated:
				continue
			if (pla_lst[i].color == 1 and pla_lst[j].color == 1 or (pla_lst[i].color == 2 and pla_lst[j].color == 3) or (pla_lst[i].color == 3 and pla_lst[j].color == 2)) == False:
				continue
			if pla_lst[i].tim != pla_lst[j].tim:
				continue
			if pla_lst[i].rat >= pla_lst[j].minrate and pla_lst[i].rat <= pla_lst[j].maxrate and (pla_lst[j].rat >= pla_lst[i].minrate) and (pla_lst[j].rat <= pla_lst[i].maxrate):
				pla_lst[i].done = 1
				pla_lst[i].match = j + 1
				pla_lst[j].done = 1
				break
		if pla_lst[i].match != -2:
			print(pla_lst[i].match)
		else:
			print('wait')
t = int(input())
for i in range(t):
	func()


tc = int(input())
while tc > 0:
	l = []
	occupied = {}
	n = int(input())
	for i in range(n):
		inp = input()
		inp_list = inp.split()
		l.append(inp_list)
	if n == 1:
		print('wait')
	else:
		i = 0
		while i < n:
			j = 0
			flag = 0
			while j < i:
				if j not in occupied.keys():
					tlist = [5, 15, 30, 60]
					(r1, min1, max1, t1, rated1, color1) = (l[i][0], l[i][1], l[i][2], l[i][3], l[i][4], l[i][5])
					(r2, min2, max2, t2, rated2, color2) = (l[j][0], l[j][1], l[j][2], l[j][3], l[j][4], l[j][5])
					if (int(r2) >= int(min1) and int(r2) <= int(max1) and (int(r1) >= int(min2)) and (int(r1) <= int(max2))) and (rated1 == rated2 and int(t1) == int(t2)) and (color1 == color2 == 'random' or (color1 == 'white' and color2 == 'black') or (color2 == 'white' and color1 == 'black')):
						if int(t1) in tlist and int(t2) in tlist:
							print(j + 1)
							occupied[j] = 1
							occupied[i] = 1
							flag = 1
				if flag == 1:
					break
				j += 1
			i += 1
			if flag == 0:
				print('wait')
	tc -= 1


def foo(a, b):
	return b[-1] != -100 and b[1] <= a[0] <= b[2] and (a[3] == b[3]) and (a[1] <= b[0] <= a[2]) and (a[-3] + b[-3] != 0) and (a[-2] + b[-2] == 0)
r = {'rated': 1, 'unrated': -1, 'black': -1, 'random': 0, 'white': 1}
for _ in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		x = input().split()
		a.append(list(map(int, x[:4])) + [r[x[4]], r[x[5]], i])
	print('wait')
	if n == 1:
		continue
	for j in range(1, n):
		w = list(filter(lambda k: foo(a[j], k), a[:j]))
		if len(w):
			print(w[0][-1] + 1)
			a[w[0][-1]][-1] = -100
			a[j][-1] = -100
		else:
			print('wait')


from collections import OrderedDict
for _ in range(int(input())):
	n = int(input())
	a = [input().split() for i in range(n)]
	temp_a = {a[i][0]: i for i in range(n)}
	temp = OrderedDict()
	for i in a:
		result = False
		(r, minn, maxx, time, rate, col) = i
		for j in temp:
			(temp_r, temp_minn, temp_maxx, temp_time, temp_rate, temp_col) = temp[j]
			if int(minn) <= int(temp_r) <= int(maxx) and int(temp_minn) <= int(r) <= int(temp_maxx) and (time == temp_time) and (rate == temp_rate) and (col == 'white' and temp_col == 'black' or (temp_col == 'white' and col == 'black') or col == temp_col == 'random'):
				print(temp_a[temp_r] + 1)
				del temp[temp_r]
				result = True
				break
			else:
				result = False
		if result == False:
			temp[r] = i
			print('wait')


for t in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		(r, min1, max1, time, israted, color) = map(str, input().split())
		r = int(r)
		min1 = int(min1)
		max1 = int(max1)
		time = int(time)
		used = 0
		b = [r, min1, max1, time, israted, color, used]
		flag = 0
		for j in range(i):
			if b[0] >= a[j][1] and b[0] <= a[j][2] and (a[j][0] >= b[1]) and (a[j][0] <= b[2]) and (b[3] == a[j][3]) and (a[j][4] == b[4]) and (a[j][5] == b[5] or (a[j][5] == 'white' and b[5] == 'black') or (a[j][5] == 'black' and b[5] == 'white')) and (a[j][6] == 0):
				flag = j + 1
				a[j][6] = 1
				b[6] = 1
				break
		a.append(b)
		if flag != 0:
			print(flag)
		else:
			print('wait')


T = int(input())
for _ in range(T):
	N = int(input())
	player = []
	for n in range(N):
		s = input().strip().split()
		player.append([int(s[0]), int(s[1]), int(s[2]), int(s[3]), s[4], s[5], 0])
	for i in range(N):
		p = player[i]
		ans = 'wait'
		for j in range(i):
			opp = player[j]
			if opp[6] == 1:
				continue
			if opp[1] <= p[0] <= opp[2] and p[1] <= opp[0] <= p[2]:
				if p[4] == opp[4]:
					if p[3] == opp[3]:
						if p[5] == 'random' and opp[5] == 'random' or (p[5] == 'black' and opp[5] == 'white') or (p[5] == 'white' and opp[5] == 'black'):
							ans = j + 1
							opp[6] = 1
							p[6] = 1
							break
		print(ans)


import math
t = int(input())
for i in range(t):
	s1 = int(input())
	s = []
	c = 0
	for j in range(s1):
		s2 = input()
		s2 = s2.split()
		if len(s) == 0:
			print('wait')
			s.append(s2)
		else:
			c = 0
			for k in range(len(s)):
				if int(s2[0]) <= int(s[k][2]) and int(s2[0]) >= int(s[k][1]) and (int(s[k][0]) <= int(s2[2])) and (int(s[k][0]) >= int(s2[1])):
					if int(s2[3]) == int(s[k][3]) and s2[4] == s[k][4]:
						if s2[5] == s[k][5] and s2[5] == 'random' or (s2[5] == 'white' and s[k][5] == 'black') or (s[k][5] == 'white' and s2[5] == 'black'):
							c = c + 1
							print(k + 1)
							s[k] = [0, 0, 0, 0, 0]
							k = math.inf
							break
				else:
					c = c
			if c == 0:
				print('wait')
				s.append(s2)
			else:
				st = [0, 0, 0, 0, 0]
				s.append(st)


for _ in range(int(input())):
	n = int(input())
	a = []
	t = [0] * n
	for j in range(n):
		temp = input().split()
		a.append(temp)
		c = len(a) - 1
		s = 0
		f = 0
		for i in range(c):
			if t[i] == 0:
				if (int(a[i][0]) >= int(a[c][1]) and int(a[i][0]) <= int(a[c][2])) and int(a[c][0]) >= int(a[i][1]) and (int(a[c][0]) <= int(a[i][2])):
					s += 1
				if a[i][4] == a[c][4]:
					s += 1
				if a[i][3] == a[c][3]:
					s += 1
				if a[i][5] == a[c][5] or (a[i][5] == 'white' and a[c][5] == 'black') or (a[c][5] == 'white' and a[i][5] == 'black'):
					s += 1
				if s == 4:
					print(i + 1)
					t[c] = 1
					t[i] = 1
					f = 1
					break
				else:
					s = 0
		if f == 0:
			print('wait')


t = int(input())
while t:
	i = 1
	players = []
	t -= 1
	N = int(input())
	flag = 0
	wait = []
	while i <= N:
		flag = 0
		line = input()
		rating = int(line.split()[0])
		minrating = int(line.split()[1])
		maxrating = int(line.split()[2])
		time = int(line.split()[3])
		rate = line.split()[4]
		color = line.split()[5]
		if i == 1 or len(players) == 0:
			wait.append(i)
			flag = 0
		else:
			for p in players:
				if rating <= p[2] and rating >= p[1] and (p[0] >= minrating) and (p[0] <= maxrating) and (time == p[3]) and (rate == p[4]):
					if p[5] == 'random' and color == 'random':
						print(p[6])
						flag = 1
						players.remove(p)
						break
					elif p[5] == 'black' and color == 'white' or (p[5] == 'white' and color == 'black'):
						print(p[6])
						flag = 1
						players.remove(p)
						break
				else:
					flag = 0
		if flag == 0:
			players.append([rating, minrating, maxrating, time, rate, color, i])
			print('wait')
		i += 1


t = int(input())
for x in range(t):
	n = int(input())
	playerslist = [list(input().split())]
	playerslist[0][0] = int(playerslist[0][0])
	playerslist[0][1] = int(playerslist[0][1])
	playerslist[0][2] = int(playerslist[0][2])
	playerslist[0][3] = int(playerslist[0][3])
	print('wait')
	while len(playerslist) < n:
		l = list(input().split())
		l[0] = int(l[0])
		l[1] = int(l[1])
		l[2] = int(l[2])
		l[3] = int(l[3])
		playerslist.append(l)
		z = 0
		for i in range(len(playerslist) - 1):
			if l[0] >= playerslist[i][1] and l[0] <= playerslist[i][2] and (playerslist[i][0] >= l[1]) and (playerslist[i][0] <= l[2]) and (l[3] == playerslist[i][3]) and (l[4] == playerslist[i][4]):
				if l[5] == playerslist[i][5] == 'random' or (l[5] == 'white' and playerslist[i][5] == 'black') or (l[5] == 'black' and playerslist[i][5] == 'white'):
					print(i + 1)
					z = 1
					playerslist[i] = [0, 0, 0, 0, 0, 0]
					playerslist[len(playerslist) - 1] = [0, 0, 0, 0, 0, 0]
					break
		if z == 0:
			print('wait')


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	l = []
	while n > 0:
		n -= 1
		l1 = input().split()
		l1.append(1)
		l1[0] = int(l1[0])
		l1[1] = int(l1[1])
		l1[2] = int(l1[2])
		l1[3] = int(l1[3])
		l.append(l1)
		c = 0
		for i in range(len(l) - 1):
			if l[i][6] == 1:
				if l1[0] <= l[i][2] and l1[0] >= l[i][1] and (l1[2] >= l[i][0]) and (l1[1] <= l[i][0]):
					if l1[3] == l[i][3] and l1[4] == l[i][4]:
						if l1[5] == l[i][5] and l1[5] == 'random' or (l1[5] != 'random' != l[i][5] and l1[5] != l[i][5]):
							l1[6] = 0
							l[i][6] = 0
							print(i + 1)
							c = 1
							break
		if c == 0:
			print('wait')


for _ in range(int(input())):
	n = int(input())
	a = []
	for i in range(n):
		b = [i for i in input().split()]
		(b[0], b[1], b[2], b[3]) = (int(b[0]), int(b[1]), int(b[2]), int(b[3]))
		a.append(b)
	visit = [0 for i in range(n)]
	ans = ['wait']
	for i in range(1, n):
		for j in range(i):
			if visit[j] == 0 and a[j][0] >= a[i][1] and (a[j][0] <= a[i][2]) and (a[i][0] >= a[j][1]) and (a[i][0] <= a[j][2]) and (a[j][3] == a[i][3]) and (a[j][4] == a[i][4]):
				if a[j][5] == 'random' and a[i][5] == 'random' or (a[j][5] == 'white' and a[i][5] == 'black') or (a[j][5] == 'black' and a[i][5] == 'white'):
					visit[i] = 1
					visit[j] = 1
					ans.append(j + 1)
					break
		else:
			ans.append('wait')
	for i in ans:
		print(i)


def chk(a, b):
	if int(a[0]) < int(b[1]) or int(a[0]) > int(b[2]):
		return False
	if int(b[0]) < int(a[1]) or int(b[0]) > int(a[2]):
		return False
	if b[3] != a[3]:
		return False
	if b[4] != a[4]:
		return False
	if b[5] != a[5]:
		if b[5] == 'random' or a[5] == 'random':
			return False
	elif b[5] != 'random':
		return False
	return True
t = int(input())
while t > 0:
	n = int(input())
	a = [[i for i in input().split()] for j in range(n)]
	b = ['wait']
	c = [-1] * n
	for i in range(1, n):
		for j in range(i):
			if chk(a[i], a[j]) and c[j] == -1:
				c[j] = 1
				c[i] = 1
				b.append(j + 1)
				break
		if len(b) != i + 1:
			b.append('wait')
	for i in range(n):
		print(b[i])
	t -= 1


colour = {'random': 'random', 'black': 'white', 'white': 'black'}
for _ in range(int(input())):
	stack = []
	n = int(input())
	for k in range(n):
		desc = input().split()
		R = int(desc[0])
		low = int(desc[1])
		high = int(desc[2])
		times = int(desc[3])
		rate = desc[4]
		clr = desc[5]
		for i in range(len(stack)):
			if stack[i] == 0:
				continue
			if stack[i][0] < low or stack[i][0] > high or stack[i][1] > R or (stack[i][2] < R) or (stack[i][3] != times) or (stack[i][4] != rate) or (stack[i][5] != colour[clr]):
				continue
			print(i + 1)
			stack[i] = 0
			stack.append(0)
			break
		else:
			print('wait')
			stack.append([R, low, high, times, rate, clr])


import io
import sys

def test_data():
	return io.StringIO('1\n7\n5 1 10 15 rated random\n11 1 20 15 rated random\n10 3 30 15 rated random\n2 5 15 15 rated random\n30 20 60 60 unrated white\n50 20 40 60 unrated random\n50 20 40 60 unrated black')

def get_input(source):
	with source as lines:
		t = int(next(lines))
		for _ in range(t):
			p = []
			n = int(next(lines))
			for _ in range(n):
				(r, mi, ma, t, ranked, colour) = next(lines).strip().split(' ')
				p.append((int(r), int(mi), int(ma), int(t), ranked, colour))
			yield (n, p)

def calc(n, p):
	waiting = []
	for (i, person) in enumerate(p):
		found = False
		for (pos_j, j) in enumerate(waiting):
			other = p[j]
			if other[1] <= person[0] and person[0] <= other[2] and (person[1] <= other[0]) and (other[0] <= person[2]) and (other[3] == person[3]) and (other[4] == person[4]) and (other[5] == 'black' and person[5] == 'white' or (other[5] == 'white' and person[5] == 'black') or (other[5] == 'random' and person[5] == 'random')):
				yield (j + 1)
				del waiting[pos_j]
				found = True
				break
		if not found:
			yield 'wait'
			waiting.append(i)
inp = test_data() if '-test' in sys.argv else sys.stdin
for args in get_input(inp):
	for state in calc(*args):
		print(state)


for jjjj in range(int(input())):
	n = int(input())
	c = []
	rr = 'random'
	bb = 'black'
	cc = 'white'
	visited = [0 for i in range(n)]
	ans = []
	for i in range(n):
		a = list(map(str, input().strip().split(' ')))
		u = -1
		for j in range(i - 1, -1, -1):
			if visited[j] == 1:
				continue
			b = c[j]
			rating = int(b[0])
			rating1 = int(a[0])
			if rating >= int(a[1]) and rating <= int(a[2]) and (rating1 >= int(b[1])) and (rating1 <= int(b[2])) and (a[3] == b[3]) and (a[4] == b[4]):
				if a[5] == rr and b[5] == rr or (a[5] == bb and b[5] == cc) or (a[5] == cc and b[5] == bb):
					u = j
		if u == -1:
			ans.append('wait')
		else:
			visited[u] = 1
			visited[i] = 1
			ans.append(str(u + 1))
		c.append(a)
	for i in ans:
		print(i)


def match(a, b):
	if a == 'random' or b == 'random':
		return a == b
	return a != b

def solve(x):
	pp = []
	for (j, y) in enumerate(x):
		y = [j + 1] + list(map(int, y[:4])) + y[4:]
		for (i, p) in enumerate(pp):
			if not match(p[-1], y[-1]):
				continue
			if p[-2] != y[-2]:
				continue
			if p[-3] != y[-3]:
				continue
			if y[1] < p[2] or y[1] > p[3]:
				continue
			if p[1] < y[2] or p[1] > y[3]:
				continue
			print(p[0])
			pp = pp[:i] + pp[i + 1:]
			break
		else:
			print('wait')
			pp += [y]
import sys
f = sys.stdin
t = int(f.readline())
for i in range(t):
	n = int(f.readline())
	x = []
	for j in range(n):
		x += [f.readline().split()]
	solve(x)


t = int(input())
while t:
	t -= 1
	n = int(input())
	vis = [0] * n
	a = []
	for i in range(n):
		temp = input().split()
		(temp[0], temp[1], temp[2], temp[3]) = (int(temp[0]), int(temp[1]), int(temp[2]), int(temp[3]))
		f = 1
		for j in range(len(a)):
			if vis[j] == 0:
				matching = 1
				if not (temp[4] == a[j][4] and (temp[5] == a[j][5] and temp[5] == 'random' or (temp[5] == 'white' and a[j][5] == 'black') or (a[j][5] == 'white' and temp[5] == 'black'))):
					matching = 0
				if not (temp[3] == a[j][3] and temp[0] >= a[j][1] and (temp[0] <= a[j][2]) and (a[j][0] >= temp[1]) and (a[j][0] <= temp[2])):
					matching = 0
				if matching:
					vis[i] = vis[j] = 1
					print(j + 1)
					f = 0
					break
		if f:
			print('wait')
		a.append(temp)


class Game(object):

	def __init__(self, rating, minRating, maxRating, thinkingTime, isMatchRated, colour, index):
		self.rating = rating
		self.minRating = minRating
		self.maxRating = maxRating
		self.isMatchRated = isMatchRated
		self.thinkingTime = thinkingTime
		self.colour = colour
		self.index = index
T = int(input())
for _ in range(T):
	N = int(input())
	gamesReadyToStart = []
	for player in range(1, N + 1):
		(rating, minRating, maxRating, thinkingTime, isMatchRated, colour) = input().split()
		(rating, minRating, maxRating, thinkingTime) = (int(rating), int(minRating), int(maxRating), int(thinkingTime))
		playerMatched = False
		for gameI in range(len(gamesReadyToStart)):
			game = gamesReadyToStart[gameI]
			suitableMatch = True
			if game.rating < minRating:
				suitableMatch = False
			if game.rating > maxRating:
				suitableMatch = False
			if rating < game.minRating:
				suitableMatch = False
			if rating > game.maxRating:
				suitableMatch = False
			if game.isMatchRated != isMatchRated:
				suitableMatch = False
			if game.thinkingTime != thinkingTime:
				suitableMatch = False
			if game.colour == 'black' and colour == 'black':
				suitableMatch = False
			if game.colour == 'black' and colour == 'random':
				suitableMatch = False
			if game.colour == 'white' and colour == 'white':
				suitableMatch = False
			if game.colour == 'white' and colour == 'random':
				suitableMatch = False
			if game.colour == 'random' and colour == 'black':
				suitableMatch = False
			if game.colour == 'random' and colour == 'white':
				suitableMatch = False
			if suitableMatch == True:
				playerMatched = True
				break
		if playerMatched:
			print(game.index)
			gamesReadyToStart.pop(gameI)
		if not playerMatched:
			index = player
			newGame = Game(rating, minRating, maxRating, thinkingTime, isMatchRated, colour, index)
			gamesReadyToStart.append(newGame)
			print('wait')


for t in range(int(input())):
	n = int(input())
	li = []
	for i in range(n):
		l = input().split()
		l[0] = int(l[0])
		l[1] = int(l[1])
		l[2] = int(l[2])
		l[3] = int(l[3])
		li.append(l)
	ans = [] * n
	ans.append('wait')
	table = [0] * n
	for i in range(1, n):
		f = 0
		for j in range(0, i):
			if i == j:
				continue
			if table[j] == 0 and li[i][0] in range(li[j][1], li[j][2] + 1) and (li[j][0] in range(li[i][1], li[i][2] + 1)):
				if li[i][3] == li[j][3] and li[i][4] == li[j][4]:
					if li[i][5] == 'random' and li[j][5] == 'random' or (li[i][5] != 'random' and li[i][5] != li[j][5] and (li[j][5] != 'random')):
						ans.append(j + 1)
						table[i] = 1
						table[j] = 1
						f = 0
						break
					else:
						f = 1
				else:
					f = 1
			else:
				f = 1
		if f == 1:
			ans.append('wait')
	for i in ans:
		print(i)


t = int(input())
for i in range(t):
	n = int(input())
	(R, Minn, Maxx, Time, isRated, colour) = ([], [], [], [], [], [])
	flag = []
	for j in range(n):
		arr = list(input().strip().split(' '))
		R.append(int(arr[0]))
		Minn.append(int(arr[1]))
		Maxx.append(int(arr[2]))
		Time.append(int(arr[3]))
		isRated.append(arr[4])
		colour.append(arr[5])
		flag.append(1)
	ans = ['wait']
	for j in range(n):
		for k in range(j):
			if isRated[j] == isRated[k]:
				if Time[j] == Time[k]:
					if R[j] >= Minn[k] and R[j] <= Maxx[k] and (R[k] <= Maxx[j]) and (R[k] >= Minn[j]):
						if colour[j] == colour[k] and colour[j] == 'random' or (colour[j] == 'white' and colour[k] == 'black') or (colour[k] == 'white' and colour[j] == 'black'):
							if flag[k]:
								ans.append(k + 1)
								flag[k] = 0
								flag[j] = 0
								break
			if k == j - 1:
				ans.append('wait')
	for j in ans:
		print(j)


class game:

	def __init__(self, pos, rating, minrating, maxrating, timethink, israted, color):
		self.pos = pos
		self.rating = int(rating)
		self.minrating = int(minrating)
		self.maxrating = int(maxrating)
		self.timethink = int(timethink)
		self.israted = israted
		self.color = color

def compat(game1, game2):
	cons = []
	cons.append(game2.minrating <= game1.rating <= game2.maxrating)
	cons.append(game1.minrating <= game2.rating <= game1.maxrating)
	cons.append(game1.timethink == game2.timethink)
	cons.append(game1.israted == game2.israted)
	cons.append(game1.color == 'random' and game2.color == 'random' or (game1.color == 'black' and game2.color == 'white') or (game1.color == 'white' and game2.color == 'black'))
	if False in cons:
		return False
	else:
		return True
for _ in range(int(input())):
	n = int(input())
	queue = []
	for currpos in range(n):
		currgame = game(currpos + 1, *input().strip().split(' '))
		for (i, g) in enumerate(queue):
			if compat(currgame, g):
				print(g.pos)
				queue.remove(queue[i])
				break
		else:
			queue.append(currgame)
			print('wait')


test = int(input())
for _ in range(test):
	r = []
	mi = []
	ma = []
	ti = []
	ir = []
	co = []
	wa = []
	n = int(input())
	for i in range(n):
		temp = input().split()
		r.append(int(temp[0]))
		mi.append(int(temp[1]))
		ma.append(int(temp[2]))
		ti.append(int(temp[3]))
		ir.append(temp[4])
		co.append(temp[5])
		wa.append(-1)
	for i in range(n):
		for j in range(i):
			if wa[j] != -1:
				continue
			if r[i] >= mi[j] and r[i] <= ma[j] and (r[j] >= mi[i]) and (r[j] <= ma[i]) and (ti[i] == ti[j]) and (ir[i] == ir[j]):
				if co[i] == 'random' and co[j] == 'random' or (co[i] == 'black' and co[j] == 'white') or (co[i] == 'white' and co[j] == 'black'):
					wa[j] = -2
					wa[i] = j + 1
					break
		if wa[i] == -1:
			print('wait')
		else:
			print(wa[i])


t = int(input())
for tc in range(t):
	n = int(input())
	arr = []
	for i in range(n):
		l = [num for num in input().strip().split()]
		arr.append(l)
	booked = [False] * n
	ans = ['wait'] * n
	for i in range(n):
		for j in range(i):
			if not booked[j]:
				if arr[i][3] == arr[j][3] and arr[i][4] == arr[j][4]:
					if int(arr[j][1]) <= int(arr[i][0]) <= int(arr[j][2]) and int(arr[i][1]) <= int(arr[j][0]) <= int(arr[i][2]):
						if arr[i][5] == arr[j][5] == 'random' or (arr[i][5] == 'white' and arr[j][5] == 'black') or (arr[j][5] == 'white' and arr[i][5] == 'black'):
							booked[i] = booked[j] = True
							ans[i] = str(j + 1)
							break
	for val in ans:
		print(val)


t = int(input())
while t > 0:
	n = int(input())
	r = []
	rmin = []
	rmax = []
	tim = []
	rated = []
	color = []
	played = [False for i in range(n)]
	for i in range(n):
		a = input().split()
		r.append(int(a[0]))
		rmin.append(int(a[1]))
		rmax.append(int(a[2]))
		tim.append(int(a[3]))
		rated.append(a[4])
		color.append(a[5])
		flag = 0
		for j in range(i):
			a1 = False
			a2 = False
			a3 = False
			a4 = False
			if played[j] == False:
				if rmin[j] <= r[i] <= rmax[j] and rmin[i] <= r[j] <= rmax[i]:
					a1 = True
				if rated[i] == rated[j]:
					a2 = True
				if tim[i] == tim[j]:
					a3 = True
				if color[i] == color[j] == 'random' or (color[i] == 'black' and color[j] == 'white') or (color[j] == 'black' and color[i] == 'white'):
					a4 = True
				if a1 and a2 and a3 and a4:
					print(j + 1)
					played[i] = True
					played[j] = True
					flag = 1
					break
		if flag == 0:
			print('wait')
	t -= 1