Problem2450--CF1744c - Traffic Light

2450: CF1744c - Traffic Light

[Creator : ]
Time Limit : 1.000 sec  Memory Limit : 128 MB

Description

题意:

给定红黄绿灯的循环节,以及你初始看到的灯,求下一个绿灯等待的最长时间。


You find yourself on an unusual crossroad with a weird traffic light. That traffic light has three possible colors: red (r), yellow (y), green (g). It is known that the traffic light repeats its colors every nn seconds and at the ii-th second the color sisi is on.

That way, the order of the colors is described by a string. For example, if s=s="rggry", then the traffic light works as the following: red-green-green-red-yellow-red-green-green-red-yellow- ... and so on.

More formally, you are given a string s1,s2,…,sns1,s2,…,sn of length nn. At the first second the color s1s1 is on, at the second — s2s2, ..., at the nn-th second the color snsn is on, at the n+1n+1-st second the color s1s1 is on and so on.

You need to cross the road and that can only be done when the green color is on.

You know which color is on the traffic light at the moment, but you don't know the current moment of time. You need to find the minimum amount of time in which you are guaranteed to cross the road.

You can assume that you cross the road immediately.

For example, with s=s="rggry" and the current color r there are two options: either the green color will be on after 11 second, or after 33. That way, the answer is equal to 33 — that is the number of seconds that we are guaranteed to cross the road, if the current color is r.

Input

The first line contains a single integer tt (1≤t≤104(1≤t≤104) — the number of test cases.

Then the description of the test cases follows.

The first line of each test case contains an integer nn and a symbol cc (1≤n≤2⋅1051≤n≤2⋅105cc is one of allowed traffic light colors ry or g)— the length of the string ss and the current color of the traffic light.

The second line of each test case contains a string ss of the length nn, consisting of the letters ry and g.

It is guaranteed that the symbol g is in the string ss and the symbol cc is in the string ss.

It is guaranteed, that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case output the minimal number of second in which you are guaranteed to cross the road.

Sample Input Copy

6
5 r
rggry
1 g
g
3 r
rrg
5 y
yrrgy
7 r
rgrgyrg
9 y
rrrgyyygy

Sample Output Copy

3
0
2
4
1
4

HINT

Note

The first test case is explained in the statement.

In the second test case the green color is on so you can cross the road immediately.

In the third test case, if the red color was on at the second second, then we would wait for the green color for one second, and if the red light was on at the first second, then we would wait for the green light for two seconds.

In the fourth test case the longest we would wait for the green color is if we wait for it starting from the fifth second.

Source/Category