## GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

# [Solution] Coprime Codeforces Solution | Solution Codeforces

D. Coprime
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Given an array of $n$ positive integers ${a}_{1},{a}_{2},\dots ,{a}_{n}$ ($1\le {a}_{i}\le 1000$). Find the maximum value of $i+j$ such that ${a}_{i}$ and ${a}_{j}$ are coprime,${}^{†}$ or $-1$ if no such $i$$j$ exist.

For example consider the array $\left[1,3,5,2,4,7,7\right]$. The maximum value of $i+j$ that can be obtained is $5+7$, since ${a}_{5}=4$ and ${a}_{7}=7$ are coprime.

${}^{†}$ Two integers $p$ and $q$ are coprime if the only positive integer that is a divisor of both of them is $1$ (that is, their greatest common divisor is $1$).

Input

The input consists of multiple test cases. The first line contains an integer $t$ ($1\le t\le 10$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer $n$ ($2\le n\le 2\cdot {10}^{5}$) — the length of the array.

The following line contains $n$ space-separated positive integers ${a}_{1}$${a}_{2}$,..., ${a}_{n}$ ($1\le {a}_{i}\le 1000$) — the elements of the array.

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 a single integer  — the maximum value of $i+j$ such that $i$ and $j$ satisfy the condition that ${a}_{i}$ and ${a}_{j}$ are coprime, or output $-1$ in case no $i$$j$ satisfy the condition.

Note

For the first test case, we can choose $i=j=3$, with sum of indices equal to $6$, since $1$ and $1$ are coprime.

For the second test case, we can choose $i=7$ and $j=5$, with sum of indices equal to $7+5=12$, since $7$ and $4$ are coprime.