#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 * 1000 + 3;
int m, par[MAXN], l[MAXN], ans[MAXN];
string s;
vector<int> r[MAXN];
struct node {
  int px = 0, sx = 0, sz = 0;
  node operator+(node x) {
    node res;
    res.sz = sz + x.sz + min(sx, x.px);
    res.px = px + x.px - min(sx, x.px);
    res.sx = sx + x.sx - min(sx, x.px);
    return res;
  }
  node(char c) {
    if (c == '(')
      sx = 1, px = 0, sz = 0;
    else
      sx = 0, px = 1, sz = 0;
  }
  node() {}
} a[MAXN];
int gp(int v) {
  if (par[v] < 0) return v;
  int p = par[v];
  par[v] = gp(par[v]);
  if (~par[p]) a[v] = a[v] + a[p];
  return par[v];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  cin >> s >> m;
  memset(par, -1, sizeof par);
  for (int i = 0; i < ((int)s.size()); i++) a[i] = node(s[i]);
  for (int i = 0; i < m; i++) {
    int x;
    cin >> l[i] >> x;
    l[i]--;
    r[x].push_back(i);
  }
  for (int i = 1; i <= ((int)s.size()); i++) {
    par[i - 1] = i;
    for (auto j : r[i]) gp(l[j]), ans[j] = a[l[j]].sz;
  }
  for (int i = 0; i < m; i++) cout << (ans[i] << 1) << '\n';
  return 0;
}
