#include namespace { #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic pop using namespace std; using namespace atcoder; #define rep(i,n)for (int i = 0; i < int(n); ++i) #define rrep(i,n)for (int i = int(n)-1; i >= 0; --i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template void chmax(T& a, const T& b) { a = max(a, b); } template void chmin(T& a, const T& b) { a = min(a, b); } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; struct doubling { const int n; int k; vector> to; doubling(int n) : n(n), k(-1), to(1, vector(n + 1, n)) {} void set_dest(int u, int v) { // u -> v to[0][u] = v; } void build() { build(n); } template void build(T d) { // move by d will be accepted later // k := floor_lg(d) k = 1; while ((T(1) << (k + 1)) <= d) k++; // prepare data area to.reserve(k); for (int i = to.size(); i < k; i++) { to.emplace_back(n + 1); } // construct doubling table for (int i = 1; i < k; i++) { for (int u = 0; u <= n; u++) { to[i][u] = to[i-1][to[i-1][u]]; } } } template int move(int v, T d) { assert(k > 0); // returns -1 if move from v reaches endpoint before d step assert(d >= 0); for (int l = 0; l < k && d > 0; l++, d >>= 1) { if (d & 1) v = to[l][v]; } assert(d == 0); if (v == n) v = -1; return v; } template int find_first_true(int v, F f) { assert(k > 0); // returns -1 if not found if (f(v)) return v; // f(v) keeps false below int res = -1; for (int i = k - 1; i >= 0; i--) { int w = to[i][v]; if (w == n) continue; if (f(w)) res = w; else v = w; } return res; } template int find_last_true(int v, F f) { assert(k > 0); // returns -1 if f is already false at v if (!f(v)) return -1; // f(v) keeps true below for (int i = k - 1; i >= 0; i--) { int w = to[i][v]; if (w == n) continue; if (f(w)) v = w; } return v; } }; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; string s; cin >> s; VI paired_pos(n, -1); VI st; doubling d(n); rep(i, n) { if (s[i] == '(') { if (!st.empty()) { d.set_dest(i, st.back()); } st.push_back(i); } else { int j = st.back(); st.pop_back(); paired_pos[j] = i; paired_pos[i] = j; } } d.build(); while(q--) { int x, y; cin >> x >> y; x--, y--; chmin(x, paired_pos[x]); chmin(y, paired_pos[y]); int rmx = max(paired_pos[x], paired_pos[y]); int lmn = min(x, y); int i = d.find_first_true(lmn, [&](int i) { return paired_pos[i] >= rmx; }); if (i == -1) { cout << -1 << '\n'; } else { cout << i + 1 << ' ' << paired_pos[i] + 1 << '\n'; } } }