import java.io.BufferedReader import scala.annotation.tailrec import scala.collection.{immutable, mutable} import scala.io.StdIn.readLine import scala.math.* import scala.annotation.tailrec import scala.io.{BufferedSource, Source, StdIn} def readLine()(using reader: BufferedReader): String = reader.readLine() class Bit(val size: Int): private val array = Array.fill(size){Int.MinValue} def get(position: Int): Int = var pos = position var result = Int.MinValue while pos>= 0 do result = max(result, array(pos)) pos -= ~pos & (pos + 1) result def changeMax(position: Int, value: Int) = var pos = position while pos < size do array(pos) = max(array(pos), value) pos += ~pos & (pos + 1) @main def main = given reader: BufferedReader = Source.stdin.bufferedReader() val Array(n, q) = readLine().split(' ').map(_.toInt) val s = readLine() val queries = Array.fill(q){ val Array(x, y) = readLine().split(' ').map(_.toInt - 1) x -> y } val stack = mutable.Stack[Int]() val pair = Array.fill(n){-1} for (c, i) <- s.zipWithIndex do c match case '(' => stack.push(i) case ')' => val h = stack.pop() pair(i) = h pair(h) = i val bit = Bit(n) var last = n - 1 val result = Array.fill(q){"-1"} for ((x, y), i) <- queries.map((x, y) => min(min(x, pair(x)), min(y, pair(y))) -> max(max(x, pair(x)), max(y, pair(y)))).zipWithIndex.sortBy(-_._1._2) do while 0 <= last && y <= last do if s(last) == ')' then bit.changeMax(pair(last), pair(last)) last -= 1 val left = bit.get(x) if left >= 0 then result(i) = s"${left + 1} ${pair(left) + 1}" println(result.mkString("\n"))