#include using namespace std; struct lowest_common_ancestor{ vector p, sz, in, next; void dfs1(vector> &c, int v = 0){ sz[v] = 1; for (int &w : c[v]){ dfs1(c, w); sz[v] += sz[w]; if (sz[w] > sz[c[v][0]]){ swap(w, c[v][0]); } } } void dfs2(vector> &c, int &t, int v = 0){ in[v] = t; t++; for (int w : c[v]){ if (w == c[v][0]){ next[w] = next[v]; } else { next[w] = w; } dfs2(c, t, w); } } lowest_common_ancestor(vector &p, vector> &c): p(p){ int N = p.size(); sz = vector(N); dfs1(c); in = vector(N); next = vector(N, 0); int t = 0; dfs2(c, t); } int lca(int u, int v){ while (1){ if (in[u] > in[v]){ swap(u, v); } if (next[u] == next[v]){ return u; } v = p[next[v]]; } } }; int main(){ int N, Q; cin >> N >> Q; string S; cin >> S; vector a; vector b(N / 2); vector> c(N / 2 + 1); stack> st; st.push(make_pair(-1, -1)); for (int i = 0; i < N; i++){ if (S[i] == '('){ if (!st.empty()){ c[st.top().first + 1].push_back(a.size() + 1); } st.push(make_pair(a.size(), i)); a.push_back(i); } if (S[i] == ')'){ b[st.top().first] = i; st.pop(); } } vector pos(N); for (int i = 0; i < N / 2; i++){ pos[a[i]] = i + 1; pos[b[i]] = i + 1; } vector p(N / 2 + 1, -1); for (int i = 0; i <= N / 2; i++){ for (int j : c[i]){ p[j] = i; } } lowest_common_ancestor T(p, c); for (int i = 0; i < Q; i++){ int x, y; cin >> x >> y; x--; y--; int r = T.lca(pos[x], pos[y]); if (r == 0){ cout << -1 << endl; } else { cout << a[r - 1] + 1 << ' ' << b[r - 1] + 1 << endl; } } }