Arkady has a playlist that initially consists of $n$ songs, numerated from $1$ to $n$ in the order they appear in the playlist. Arkady starts listening to the songs in the playlist one by one, starting from song $1$. The playlist is cycled, i. e. after listening to the last song, Arkady will continue listening from the beginning.

Each song has a genre $a_i$, which is a positive integer. Let Arkady finish listening to a song with genre $y$, and the genre of the next-to-last listened song be $x$. If $\operatorname{gcd}(x, y) = 1$, he deletes the last listened song (with genre $y$) from the playlist. After that he continues listening normally, skipping the deleted songs, and forgetting about songs he listened to before. In other words, after he deletes a song, he can't delete the next song immediately.

Here $\operatorname{gcd}(x, y)$ denotes the greatest common divisor (GCD) of integers $x$ and $y$.

For example, if the initial songs' genres were $[5, 9, 2, 10, 15]$, then the playlist is converted as follows: [5, 9, 2, 10, 15] $\to$ [5, 9, 2, 10, 15] $\to$ [5, 2, 10, 15] (because $\operatorname{gcd}(5, 9) = 1$) $\to$ [5, 2, 10, 15] $\to$ [5, 2, 10, 15] $\to$ [5, 2, 10, 15] $\to$ [5, 2, 10, 15] $\to$ [5, 2, 10, 15] $\to$ [5, 10, 15] (because $\operatorname{gcd}(5, 2) = 1$) $\to$ [5, 10, 15] $\to$ [5, 10, 15] $\to$ ... The bold numbers represent the two last played songs. Note that after a song is deleted, Arkady forgets that he listened to that and the previous songs.

Given the initial playlist, please determine which songs are eventually deleted and the order these songs are deleted.


-----Input-----

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10000$). Description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of songs.

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the genres of the songs.

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


-----Output-----

For each test case, print a single line. First, print a single integer $k$ — the number of deleted songs. After that print $k$ distinct integers: deleted songs in the order of their deletion.


-----Examples-----

Input
5
5
5 9 2 10 15
6
1 2 4 2 4 2
2
1 2
1
1
1
2
Output
2 2 3 
2 2 1 
2 2 1 
1 1 
0


-----Note-----

Explanation of the first test case is given in the statement.

In the second test case, the playlist is converted as follows: [1, 2, 4, 2, 4, 2] $\to$ [1, 2, 4, 2, 4, 2] $\to$ [1, 4, 2, 4, 2] (because $\operatorname{gcd}(1, 2) = 1$) $\to$ [1, 4, 2, 4, 2] $\to$ [1, 4, 2, 4, 2] $\to$ [1, 4, 2, 4, 2] $\to$ [1, 4, 2, 4, 2] $\to$ [1, 4, 2, 4, 2] $\to$ [4, 2, 4, 2] (because $\operatorname{gcd}(2, 1) = 1$) $\to$ [4, 2, 4, 2] $\to$ ...

In the third test case, the playlist is converted as follows: [1, 2] $\to$ [1, 2] $\to$ [1] (because $\operatorname{gcd}(1, 2) = 1$) $\to$ [1] $\to$ [1] (Arkady listened to the same song twice in a row) $\to$ [] (because $\operatorname{gcd}(1, 1) = 1$).

The fourth test case is same as the third after deletion of the second song.

In the fifth test case, the same song is listened to over and over again, but since $\operatorname{gcd}(2, 2) \ne 1$, it is not deleted.

Difficulty: HARD
Tags: Spanning trees

Data structures

Implementation

Shortest paths
Skill types: Data structures

Starter code:

Solutions:
t = int(input())

def euclidean(a, b):
	(a, b) = (max(a, b), min(a, b))
	while b:
		(a, b) = (b, a % b)
	return a

def main():
	n = int(input())
	l = [int(s) for s in input().split()]
	l_next = list(range(1, n)) + [0]
	l_forw = list(range(1, n)) + [0]
	prev = n - 1
	i = 0
	res = []
	while True:
		if l_forw[i] == i:
			if l[i] == 1:
				res.append(i + 1)
			break
		if euclidean(l[i], l[l_forw[i]]) == 1:
			if l_forw[i] == l_next[i]:
				l_next[i] = l_next[l_next[i]]
			res.append(l_forw[i] + 1)
			l_forw[i] = l_forw[l_forw[i]]
			prev = i
		else:
			if prev == i:
				break
			l_next[prev] = l_next[i]
		i = l_next[prev]
	res = [len(res)] + res
	print(' '.join([str(r) for r in res]))
for _ in range(t):
	main()


from math import gcd
from collections import deque
for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	q = deque()
	next = [None] * n
	for i in range(n):
		next[i] = (i + 1) % n
		q.append(i)
	temp = 0
	ans = []
	while q:
		i = q.popleft()
		j = next[i]
		if i == j:
			if a[i] == 1:
				ans.append(i + 1)
			continue
		if len(q) and next[q[-1]] == j:
			pass
		elif gcd(a[i], a[j]) == 1:
			ans.append(j + 1)
			next[i] = next[j]
			q.append(i)
	print(len(ans), *ans)


from math import gcd
for _ in [*open(0)][2::2]:
	a = (*map(int, _.split()),)
	n = len(a)
	s = []
	f = [*range(1, n)] + [0]
	q = [(i, f[i]) for i in range(n) if gcd(a[i], a[f[i]]) < 2]
	while len(q) != 0:
		(x, y) = q[0]
		q.pop(0)
		if f[x] == y:
			s.append(y + 1)
			f[x] = f[y]
			f[y] = -1
			if f[x] < 0 or x < 0:
				continue
			if gcd(a[x], a[f[x]]) < 2:
				q.append((x, f[x]))
	print(len(s), *s)


import queue
import math
import sys
global pre, suf
suf = [0 for i in range(int(1000000.0 + 10))]
cases = int(input())
while cases:
	cases -= 1
	n = int(input())
	a = list(map(int, input().split()))
	que = []
	for i in range(n):
		suf[i] = (i + 1) % n
		if math.gcd(a[i], a[suf[i]]) == 1:
			que.append((i, suf[i]))
	ans = []
	while len(que) != 0:
		tp = que[0]
		que.pop(0)
		(x, y) = (tp[0], tp[1])
		if suf[x] == y:
			ans.append(y)
			suf[x] = suf[y]
			suf[y] = -1
			if suf[x] == -1 or x == -1:
				continue
			if math.gcd(a[x], a[suf[x]]) == 1:
				que.append((x, suf[x]))
	print(len(ans), end=' ')
	for i in ans:
		print(i + 1, end=' ')
	print()