#include <bits/stdc++.h>
using namespace std;
struct node {
  int length;
  int open;
  int close;
};
node emptyNode;
node nodes[4 * 1000007];
string s;
node combineNode(node x, node y) {
  node z;
  int minBracket = min(x.open, y.close);
  z.length = x.length + y.length + 2 * minBracket;
  z.open = x.open + y.open - minBracket;
  z.close = x.close + y.close - minBracket;
  return z;
}
void initIT(int root, int left, int right) {
  if (left == right) {
    if (s[left] == '(')
      nodes[root].open = 1;
    else
      nodes[root].close = 1;
    return;
  }
  int mid = (left + right) / 2;
  initIT(2 * root, left, mid);
  initIT(2 * root + 1, mid + 1, right);
  nodes[root] = combineNode(nodes[2 * root], nodes[2 * root + 1]);
  return;
}
node queryIT(int root, int left, int right, int i, int j) {
  if (i > right || j < left) return emptyNode;
  if (i <= left && right <= j) return nodes[root];
  int mid = (left + right) / 2;
  return combineNode(queryIT(2 * root, left, mid, i, j),
                     queryIT(2 * root + 1, mid + 1, right, i, j));
}
int main() {
  cin >> s;
  s = '0' + s;
  initIT(1, 1, (int)s.size() - 1);
  int n;
  scanf("%d", &n);
  for (int i = (1); i <= (n); ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    node ans = queryIT(1, 1, (int)s.size() - 1, u, v);
    printf("%d\n", ans.length);
  }
  return 0;
}
