#include <bits/stdc++.h>
using namespace std;
string s;
int T[2345678], A[2345678], B[2345678], C[2345678];
void build(int v, int tl, int tr) {
  if (tl == tr) {
    if (s[tl] == '(') {
      A[v] = 1;
    } else {
      B[v] = 1;
    }
    return;
  }
  int tm = (tl + tr) >> 1;
  build(v << 1, tl, tm);
  build((v << 1) | 1, tm + 1, tr);
  int tmp = min(A[v << 1], B[(v << 1) | 1]);
  T[v] = T[v << 1] + T[(v << 1) | 1] + 2 * tmp;
  A[v] = A[v << 1] + A[(v << 1) | 1] - tmp;
  B[v] = B[v << 1] + B[(v << 1) | 1] - tmp;
}
pair<int, pair<int, int> > query(int v, int tl, int tr, int l, int r) {
  if (l > tr || r < tl) {
    return pair<int, pair<int, int> >(0, pair<int, int>(0, 0));
  }
  if (tl >= l && tr <= r) {
    return pair<int, pair<int, int> >(T[v], pair<int, int>(A[v], B[v]));
  }
  int tmt = (tl + tr) >> 1;
  pair<int, pair<int, int> > f = query((v << 1), tl, tmt, l, r);
  pair<int, pair<int, int> > s = query(((v << 1) | 1), tmt + 1, tr, l, r);
  int t = min(f.second.first, s.second.second);
  int tree = f.first + s.first + 2 * t;
  int a = f.second.first + s.second.first - t;
  int b = f.second.second + s.second.second - t;
  return pair<int, pair<int, int> >(tree, pair<int, int>(a, b));
}
int l, r, m;
int main() {
  cin >> s;
  build(1, 0, s.size() - 1);
  scanf("%d", &m);
  while (m--) {
    scanf("%d %d", &l, &r);
    l--;
    r--;
    printf("%d\n", query(1, 0, s.size() - 1, l, r).first);
  }
  return 0;
}
