#include <bits/stdc++.h>
using namespace std;
struct State {
  int pair, open, close;
};
vector<State> precalc;
const int BLOCK = 1000;
State compute(const string &s) {
  State result = {0, 0, 0};
  for (int i = 0; i < (int)(s.length()); i++) {
    if (s[i] == '(') {
      result.open++;
    } else {
      if (result.open > 0) {
        result.open--;
        result.pair++;
      } else {
        result.close++;
      }
    }
  }
  return result;
}
void doPrecalc(const string &s) {
  precalc.clear();
  for (int i = 0; i < s.length(); i += BLOCK) {
    string t = s.substr(i, BLOCK);
    precalc.push_back(compute(t));
  }
}
int merge(const vector<State> &s) {
  State result = {0, 0, 0};
  for (int i = 0; i < (int)(s.size()); i++) {
    result.pair += s[i].pair;
    int p = min(result.open, s[i].close);
    result.pair += p;
    result.open -= p;
    result.open += s[i].open;
  }
  return result.pair * 2;
}
int main() {
  static char buf[1 << 20];
  while (scanf("%s", buf) != -1) {
    string s(buf);
    doPrecalc(s);
    int n;
    scanf("%d", &n);
    while (n--) {
      int src, dst;
      scanf("%d%d", &src, &dst);
      src--;
      vector<State> v;
      while (src < dst) {
        int srcBlockBegin = src / BLOCK * BLOCK;
        int srcBlockEnd = srcBlockBegin + BLOCK;
        int block = src / BLOCK;
        if (src == srcBlockBegin && srcBlockEnd <= dst) {
          v.push_back(precalc[block]);
          src = srcBlockEnd;
        } else {
          int end = min(srcBlockEnd, dst);
          v.push_back(compute(s.substr(src, end - src)));
          src = end;
        }
      }
      printf("%d\n", merge(v));
    }
  }
}
