fun main() {
val t = readLine()!!.toInt()
repeat(t) {
val (n, c) = readLine()!!.split(" ")
val s = readLine()!!
val len = n.toInt()
var maxWait = 0
var nextGreen = -1
// Find the index of all green lights in the sequence
val greenIndices = mutableListOf<Int>()
for (i in 0 until len) {
if (s[i] == 'g') {
greenIndices.add(i)
}
}
// Calculate the minimal distance from any given point to the next green light
for (i in 0 until len) {
if (s[i] == c[0]) {
// Find the first green after or at current index
val nextGreenIndex = greenIndices.firstOrNull { it >= i } ?: (greenIndices[0] + len)
val distance = nextGreenIndex - i
maxWait = maxOf(maxWait, distance)
}
}
println(maxWait)
}
}
ZnVuIG1haW4oKSB7CiAgICB2YWwgdCA9IHJlYWRMaW5lKCkhIS50b0ludCgpCgogICAgcmVwZWF0KHQpIHsKICAgICAgICB2YWwgKG4sIGMpID0gcmVhZExpbmUoKSEhLnNwbGl0KCIgIikKICAgICAgICB2YWwgcyA9IHJlYWRMaW5lKCkhIQogICAgICAgIHZhbCBsZW4gPSBuLnRvSW50KCkKCiAgICAgICAgdmFyIG1heFdhaXQgPSAwCiAgICAgICAgdmFyIG5leHRHcmVlbiA9IC0xCgogICAgICAgIC8vIEZpbmQgdGhlIGluZGV4IG9mIGFsbCBncmVlbiBsaWdodHMgaW4gdGhlIHNlcXVlbmNlCiAgICAgICAgdmFsIGdyZWVuSW5kaWNlcyA9IG11dGFibGVMaXN0T2Y8SW50PigpCiAgICAgICAgZm9yIChpIGluIDAgdW50aWwgbGVuKSB7CiAgICAgICAgICAgIGlmIChzW2ldID09ICdnJykgewogICAgICAgICAgICAgICAgZ3JlZW5JbmRpY2VzLmFkZChpKQogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICAvLyBDYWxjdWxhdGUgdGhlIG1pbmltYWwgZGlzdGFuY2UgZnJvbSBhbnkgZ2l2ZW4gcG9pbnQgdG8gdGhlIG5leHQgZ3JlZW4gbGlnaHQKICAgICAgICBmb3IgKGkgaW4gMCB1bnRpbCBsZW4pIHsKICAgICAgICAgICAgaWYgKHNbaV0gPT0gY1swXSkgewogICAgICAgICAgICAgICAgLy8gRmluZCB0aGUgZmlyc3QgZ3JlZW4gYWZ0ZXIgb3IgYXQgY3VycmVudCBpbmRleAogICAgICAgICAgICAgICAgdmFsIG5leHRHcmVlbkluZGV4ID0gZ3JlZW5JbmRpY2VzLmZpcnN0T3JOdWxsIHsgaXQgPj0gaSB9ID86IChncmVlbkluZGljZXNbMF0gKyBsZW4pCiAgICAgICAgICAgICAgICB2YWwgZGlzdGFuY2UgPSBuZXh0R3JlZW5JbmRleCAtIGkKICAgICAgICAgICAgICAgIG1heFdhaXQgPSBtYXhPZihtYXhXYWl0LCBkaXN0YW5jZSkKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgcHJpbnRsbihtYXhXYWl0KQogICAgfQp9Cg==