#P1987G1. Spinning Round (Easy Version)

    ID: 9321 Type: RemoteJudge 7000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 10 Uploaded By: Tags>divide and conquerdptrees*2900

Spinning Round (Easy Version)

No submission language available for this problem.

Description

This is the easy version of the problem. The only difference between the two versions are the allowed characters in ss. In the easy version, ss only contains the character ?. You can make hacks only if both versions of the problem are solved.

You are given a permutation pp of length nn. You are also given a string ss of length nn, consisting only of the character ?.

For each ii from 11 to nn:

  • Define lil_i as the largest index j<ij < i such that pj>pip_j > p_i. If there is no such index, li:=il_i := i.
  • Define rir_i as the smallest index j>ij > i such that pj>pip_j > p_i. If there is no such index, ri:=ir_i := i.

Initially, you have an undirected graph with nn vertices (numbered from 11 to nn) and no edges. Then, for each ii from 11 to nn, add one edge to the graph:

  • If si=s_i = L, add the edge (i,li)(i, l_i) to the graph.
  • If si=s_i = R, add the edge (i,ri)(i, r_i) to the graph.
  • If si=s_i = ?, either add the edge (i,li)(i, l_i) or the edge (i,ri)(i, r_i) to the graph at your choice.

Find the maximum possible diameter^{\text{∗}} over all connected graphs that you can form. Output 1-1 if it is not possible to form any connected graphs.

^{\text{∗}} Let d(s,t)d(s, t) denote the smallest number of edges on any path between ss and tt.

The diameter of the graph is defined as the maximum value of d(s,t)d(s, t) over all pairs of vertices ss and tt.

Each test contains multiple test cases. The first line of input contains a single integer tt (1t21041 \le t \le 2 \cdot 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (2n41052 \le n \le 4 \cdot 10^5) — the length of the permutation pp.

The second line of each test case contains nn integers p1,p2,,pnp_1,p_2,\ldots, p_n (1pin1 \le p_i \le n) — the elements of pp, which are guaranteed to form a permutation.

The third line of each test case contains a string ss of length nn. It is guaranteed that it consists only of the character ?.

It is guaranteed that the sum of nn over all test cases does not exceed 41054 \cdot 10^5.

For each test case, output the maximum possible diameter over all connected graphs that you form, or 1-1 if it is not possible to form any connected graphs.

Input

Each test contains multiple test cases. The first line of input contains a single integer tt (1t21041 \le t \le 2 \cdot 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (2n41052 \le n \le 4 \cdot 10^5) — the length of the permutation pp.

The second line of each test case contains nn integers p1,p2,,pnp_1,p_2,\ldots, p_n (1pin1 \le p_i \le n) — the elements of pp, which are guaranteed to form a permutation.

The third line of each test case contains a string ss of length nn. It is guaranteed that it consists only of the character ?.

It is guaranteed that the sum of nn over all test cases does not exceed 41054 \cdot 10^5.

Output

For each test case, output the maximum possible diameter over all connected graphs that you form, or 1-1 if it is not possible to form any connected graphs.

Sample Input 1

8
5
2 1 4 3 5
?????
2
1 2
??
3
3 1 2
???
7
5 3 1 6 4 2 7
???????
5
5 2 1 3 4
?????
6
6 2 3 4 5 1
??????
8
1 7 5 6 2 8 4 3
????????
12
6 10 7 1 8 5 12 2 11 3 4 9
????????????

Sample Output 1

4
1
2
6
4
5
5
8

Note

In the first test case, here are some possible connected graphs that you can form (the labels are indices):

In the second test case, the only connected graph has a diameter of 11.