CF1690F.Shifting String

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Polycarp found the string ss and the permutation pp . Their lengths turned out to be the same and equal to nn .

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

In one operation he can multiply ss by pp , so he replaces ss with string newnew , in which for any ii from 11 to nn it is true that newi=spinew_i = s_{p_i} . For example, with s=wmbes=wmbe and p=[3,1,4,2]p = [3, 1, 4, 2] , after operation the string will turn to s=s3s1s4s2=bwems=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.

输入格式

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

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

The second line of each case contains a string ss of length nn , containing lowercase Latin letters.

The third line of each case contains nn integers — permutation pp ( 1pin1 \le p_i \le n ), all pip_i are different.

输出格式

Output tt 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 ss will become the same as it was before operations.

输入输出样例

  • 输入#1

    3
    5
    ababa
    3 4 5 2 1
    5
    ababa
    2 1 4 5 3
    10
    codeforces
    8 6 1 7 5 2 9 3 10 4

    输出#1

    1
    6
    12

说明/提示

In the first sample operation doesn't change the string, so it will become the same as it was after 11 operations.

In the second sample the string will change as follows:

  • ss = babaa
  • ss = abaab
  • ss = baaba
  • ss = abbaa
  • ss = baaab
  • ss = ababa
首页