CF BUDDY
← Problems·

1259D · Let's Play the Words?

1900 · data structures, hashing, implementation

Problem: Polycarp has nn 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 nn 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 nn 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 nn words is consistent with the game rules.

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

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

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

Guaranteed, that the sum of nn for all test cases in the input doesn't exceed 21052\cdot10^5. Also, guaranteed that the sum of word lengths for all test cases in the input doesn't exceed 41064\cdot10^6.

Output Format: Print answer for all of tt 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 kk (0kn0 \le k \le n) — the minimal number of words in the set which should be reversed. The second line of the output should contain kk distinct integers — the indexes of the words in the set which should be reversed. Words are numerated from 11 to nn in the order they appear. If k=0k=0 you can skip this line (or you can print an empty line). If there are many answers you can print any of them.

Sample Cases

Case 1

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

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback