## GUPTA MECHANICAL

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

## [Solution] Shifting String Codeforces Solution

F. Shifting String
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp found the string $s$ and the permutation $p$. Their lengths turned out to be the same and equal to $n$.

A permutation of $n$ elements — is an array of length $n$, in which every integer from $1$ to $n$ occurs exactly once. For example, $\left[1,2,3\right]$ and $\left[4,3,5,1,2\right]$ are permutations, but $\left[1,2,4\right]$$\left[4,3,2,1,2\right]$ and $\left[0,1,2\right]$ are not.

In one operation he can

multiply $s$ by $p$, so he replaces $s$ with string $new$, in which for any $i$ from $1$ to $n$ it is true that $ne{w}_{i}={s}_{{p}_{i}}$. For example, with $s=wmbe$ and $p=\left[3,1,4,2\right]$, after operation the string will turn to $s={s}_{3}{s}_{1}{s}_{4}{s}_{2}=bwem$.

Polycarp wondered after how many operations the string would become equal to its initial value for the first time. Since it may take too long, he asks for your help in this matter.

It can be proved that the required number of operations always exists. It can be very large, so use a 64-bit integer type.

Input

The first line of input contains one integer $t$ ($1\le t\le 5000$) — the number of test cases in input.

The first line of each case contains single integer $n$ ($1\le n\le 200$) — the length of string and permutation.

The second line of each case contains a string $s$ of length $n$, containing lowercase Latin letters.

The third line of each case contains $n$ integers — permutation $p$ ($1\le {p}_{i}\le n$), all ${p}_{i}$ are different.

Output

Output $t$ lines, each of which contains the answer to the corresponding test case of input. As an answer output single integer — the minimum number of operations, after which the string $s$ will become the same as it was before operations.