#include <bits/stdc++.h>
using namespace std;
void MOD(long long &x) {
  if (x >= 1000000007) x -= 1000000007;
  if (x < 0) x += 1000000007;
}
typedef struct segtree {
  int maxcorrseq;
  int opennum;
  int closenum;
} segtree;
char arr[1000005 + 5];
segtree tree[4 * 1000005 + 5];
int n, q;
void build(int node, int start, int end) {
  if (start > end) return;
  if (start == end) {
    tree[node].maxcorrseq = 0;
    tree[node].opennum = (arr[start] == '(') ? 1 : 0;
    tree[node].closenum = 1 - tree[node].opennum;
  } else {
    int mid = (start + end) / 2;
    build(2 * node, start, mid);
    build(2 * node + 1, mid + 1, end);
    int temp = min(tree[2 * node].opennum, tree[2 * node + 1].closenum);
    tree[node].maxcorrseq =
        tree[2 * node].maxcorrseq + tree[2 * node + 1].maxcorrseq + 2 * temp;
    tree[node].opennum =
        tree[2 * node].opennum + tree[2 * node + 1].opennum - temp;
    tree[node].closenum =
        tree[2 * node].closenum + tree[2 * node + 1].closenum - temp;
  }
}
segtree query(int node, int start, int end, int l, int r) {
  segtree ans;
  ans.maxcorrseq = 0;
  ans.opennum = 0;
  ans.closenum = 0;
  if (r < start || end < l) return ans;
  if (l <= start && end <= r) return tree[node];
  int mid = (start + end) / 2;
  segtree p1 = query(2 * node, start, mid, l, r);
  segtree p2 = query(2 * node + 1, mid + 1, end, l, r);
  int temp = min(p1.opennum, p2.closenum);
  ans.maxcorrseq = p1.maxcorrseq + p2.maxcorrseq + 2 * temp;
  ans.opennum = p1.opennum + p2.opennum - temp;
  ans.closenum = p1.closenum + p2.closenum - temp;
  return ans;
}
void solve() {
  string s;
  cin >> s;
  n = s.length();
  for (int i = 1; i < n + 1; i++) arr[i] = s[i - 1];
  build(1, 1, n);
  cin >> q;
  for (int i = 1; i < q + 1; i++) {
    int x, y;
    cin >> x >> y;
    segtree ans = query(1, 1, n, x, y);
    cout << ans.maxcorrseq << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
