#include <bits/stdc++.h>
using namespace std;
const int MAX = (int)1e6 + 10;
const int INF = (int)1e9 + 10;
int n;
string s;
int q;
int sum[MAX];
int T[2 * MAX];
void init() {
  cin >> s;
  n = s.size();
  cin >> q;
  for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + (s[i - 1] == '(' ? 1 : -1);
  for (int i = 1; i <= n; i++) T[i + n - 1] = sum[i];
}
void build() {
  for (int i = n - 1; i >= 1; i--) T[i] = min(T[i << 1], T[i << 1 | 1]);
}
int query(int l, int r) {
  int res = INF;
  for (l += n - 1, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1) res = min(res, T[l++]);
    if (r & 1) res = min(res, T[--r]);
  }
  return res;
}
void process() {
  build();
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    int res = r - l + 1;
    int total = sum[r] - sum[l - 1];
    int vmin = query(l, r) - sum[l - 1];
    if (vmin < 0) {
      res -= abs(vmin);
      total -= vmin;
    }
    res -= total;
    cout << res << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  init();
  process();
}
