Polycarp has $n$ different binary words. A word called binary if it contains only characters '0' and '1'. For example, these words are binary: "0001", "11", "0" and "0011100".

Polycarp wants to offer his set of $n$ binary words to play a game "words". In this game, players name words and each next word (starting from the second) must start with the last character of the previous word. The first word can be any. For example, these sequence of words can be named during the game: "0101", "1", "10", "00", "00001".

Word reversal is the operation of reversing the order of the characters. For example, the word "0111" after the reversal becomes "1110", the word "11010" after the reversal becomes "01011".

Probably, Polycarp has such a set of words that there is no way to put them in the order correspondent to the game rules. In this situation, he wants to reverse some words from his set so that:  the final set of $n$ words still contains different words (i.e. all words are unique);  there is a way to put all words of the final set of words in the order so that the final sequence of $n$ words is consistent with the game rules. 

Polycarp wants to reverse minimal number of words. Please, help him.


-----Input-----

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

The first line of a test case contains one integer $n$ ($1 \le n \le 2\cdot10^5$) — the number of words in the Polycarp's set. Next $n$ lines contain these words. All of $n$ words aren't empty and contains only characters '0' and '1'. The sum of word lengths doesn't exceed $4\cdot10^6$. All words are different.

Guaranteed, that the sum of $n$ for all test cases in the input doesn't exceed $2\cdot10^5$. Also, guaranteed that the sum of word lengths for all test cases in the input doesn't exceed $4\cdot10^6$.


-----Output-----

Print answer for all of $t$ test cases in the order they appear.

If there is no answer for the test case, print -1. Otherwise, the first line of the output should contain $k$ ($0 \le k \le n$) — the minimal number of words in the set which should be reversed. The second line of the output should contain $k$ distinct integers — the indexes of the words in the set which should be reversed. Words are numerated from $1$ to $n$ in the order they appear. If $k=0$ you can skip this line (or you can print an empty line). If there are many answers you can print any of them.


-----Example-----
Input
4
4
0001
1000
0011
0111
3
010
101
0
2
00000
00001
4
01
001
0001
00001

Output
1
3 
-1
0

2
1 2

Difficulty: HARD
Tags: Mathematics

Implementation

Starter code:

Solutions:
for _ in range(int(input())):
	n = int(input())
	mass = []
	zo = 0
	oz = 0
	zz = 0
	oo = 0
	ozs = []
	zos = []
	ozss = set()
	zoss = set()
	for j in range(n):
		k = input()
		mass.append(k)
		if k[0] == '0' and k[-1] == '1':
			zoss.add(k)
			zos.append(j + 1)
			zo += 1
		elif k[0] == '1' and k[-1] == '0':
			ozss.add(k)
			ozs.append(j + 1)
			oz += 1
		elif k[0] == '0' and k[-1] == '0':
			zz += 1
		else:
			oo += 1
	if zz and oo and (not oz) and (not zo):
		print(-1)
		continue
	elif zo > oz:
		print((zo - oz) // 2)
		ans = []
		need = (zo - oz) // 2
		i = 0
		while need:
			zzz = mass[zos[i] - 1][len(mass[zos[i] - 1]) - 1::-1]
			if zzz not in ozss:
				ans.append(zos[i])
				need -= 1
			i += 1
		print(*ans)
	else:
		print((oz - zo) // 2)
		ans = []
		need = (oz - zo) // 2
		i = 0
		while need:
			zzz = mass[ozs[i] - 1][len(mass[ozs[i] - 1]) - 1::-1]
			if zzz not in zoss:
				ans.append(ozs[i])
				need -= 1
			i += 1
		print(*ans)


k = int(input())
for i in range(k):
	is_t = set()
	a = dict()
	a['00'] = []
	a['11'] = []
	a['01'] = []
	a['10'] = []
	n = int(input())
	s = []
	for i in range(n):
		b = input()
		a[b[0] + b[-1]].append(i)
		s.append(b)
		is_t.add(b)
	c = len(a['10'])
	d = len(a['01'])
	if c + d == 0:
		if len(a['00']) == 0 or len(a['11']) == 0:
			print(0)
		else:
			print(-1)
	elif c > d:
		ans = []
		i = 0
		m = (d + c) // 2
		while d != m and i < len(a['10']):
			s1 = s[a['10'][i]]
			if s1[::-1] not in is_t:
				d += 1
				ans.append(a['10'][i] + 1)
			i += 1
		if d != m:
			print(-1)
		else:
			print(len(ans))
			print(*ans)
	else:
		ans = []
		i = 0
		m = (d + c) // 2
		while c != m and i < len(a['01']):
			s1 = s[a['01'][i]]
			if s1[::-1] not in is_t:
				c += 1
				ans.append(a['01'][i] + 1)
			i += 1
		if c != m:
			print(-1)
		else:
			print(len(ans))
			print(*ans)


N = int(input())

def ceildiv(x, y):
	if x % y == 0:
		return x // y
	else:
		return x // y + 1
for _ in range(N):
	doms = []
	(oc, zc) = (0, 0)
	n = int(input())
	used = set()
	fulls = dict()
	for i in range(n):
		d = input()
		used.add(d)
		if d[0] != d[-1]:
			fulls[i] = d
			doms.append((i, (d[0], d[-1])))
		elif d[0] == '0':
			zc = 1
		else:
			oc = 1
	if len(doms) == 0:
		if zc == 1 and oc == 1:
			print(-1)
		else:
			print(0)
	else:
		_01 = 0
		_10 = 0
		_01_indexes = []
		_10_indexes = []
		for dom in doms:
			if dom[1] == ('0', '1'):
				_01 += 1
				_01_indexes.append(dom[0])
			else:
				_10 += 1
				_10_indexes.append(dom[0])
		if _10 < _01:
			(_01, _10) = (_10, _01)
			(_01_indexes, _10_indexes) = (_10_indexes, _01_indexes)
		_10_indexes = [x for x in _10_indexes if fulls[x][::-1] not in used]
		need = ceildiv(_10 - _01 - 1, 2)
		if len(_10_indexes) >= need:
			print(need)
			print(' '.join(list([str(x + 1) for x in _10_indexes[:need]])))
		else:
			print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	k = {'01': 0, '00': 0, '11': 0, '10': 0}
	ab = []
	ba = []
	a = []
	ra = set()
	rb = set()
	for i in range(n):
		s = input()
		ts = s[0] + s[-1]
		k[ts] += 1
		if ts == '01':
			ab.append([str(i + 1), s])
			ra.add(s)
		if ts == '10':
			ba.append([str(i + 1), s])
			rb.add(s)
	if k['01'] == 0 and k['10'] == 0 and (k['00'] > 0) and (k['11'] > 0):
		ans = -1
	elif k['01'] == k['10'] or k['01'] == k['10'] + 1 or k['01'] == k['10'] - 1:
		ans = 0
	else:
		m = (k['01'] + k['10']) // 2 if (k['01'] + k['10']) % 2 == 0 else (k['01'] + k['10']) // 2 + 1
		if k['01'] > m:
			ans = k['01'] - m
			for i in range(len(ab)):
				psp = ab[i][1]
				nn = list(psp)
				nn.reverse()
				psp = ''.join(nn)
				c1 = len(rb)
				rb.add(psp)
				c2 = len(rb)
				if c1 != c2:
					a.append(ab[i][0])
			if len(a) >= ans:
				a = a[:ans]
			else:
				ans = -1
		else:
			ans = k['10'] - m
			for i in range(len(ba)):
				psp = ba[i][1]
				nn = list(psp)
				nn.reverse()
				psp = ''.join(nn)
				c1 = len(ra)
				ra.add(psp)
				c2 = len(ra)
				if c1 != c2:
					a.append(ba[i][0])
			if len(a) >= ans:
				a = a[:ans]
			else:
				ans = -1
	print(ans)
	if ans > 0:
		print(' '.join(a))


t = int(input())
for i in range(t):
	n = int(input())
	(i0, i1) = ([], [])
	(l0, l1) = ([], [])
	(h0, h1) = (False, False)
	for i in range(n):
		t = input()
		if t[0] == '0' and t[-1] == '1':
			i0.append(i)
			l0.append(t)
		elif t[0] == '1' and t[-1] == '0':
			i1.append(i)
			l1.append(t)
		elif t[0] == t[-1] == '1':
			h1 = True
		elif t[0] == t[-1] == '0':
			h0 = True
	(c0, c1) = (len(l0), len(l1))
	(req, sl) = (0, [])
	s0 = set(l0)
	s1 = set(l1)
	if c0 > 0 or c1 > 0:
		if c0 - c1 > 1:
			req = (c0 - c1) // 2
			sel = 0
			sl = []
			for tt in range(len(l0)):
				t = l0[tt]
				if not t[::-1] in s1:
					req -= 1
					sl.append(i0[tt] + 1)
				if req == 0:
					break
		elif c1 - c0 > 1:
			req = (c1 - c0) // 2
			sel = 0
			sl = []
			for tt in range(len(l1)):
				t = l1[tt]
				if not t[::-1] in s0:
					req -= 1
					sl.append(i1[tt] + 1)
				if req == 0:
					break
		if req > 0:
			print(-1)
		else:
			print(len(sl))
			print(*sl)
	elif h0 and h1:
		print(-1)
	else:
		print(0)
		print(*[])