#include <bits/stdc++.h>
using namespace std;
int n;
int ans[4 * 1024 * 1024];
int lc[4 * 1024 * 1024], rc[4 * 1024 * 1024];
string str;
int lget(int a, int b) { return (a <= b) ? lc[b] - lc[a - 1] : 0; }
int rget(int a, int b) { return (a <= b) ? rc[b] - rc[a - 1] : 0; }
void build(int at, int l, int r) {
  if (l == r) {
    ans[at] = 0;
  } else {
    int mid = (l + r) / 2;
    build(at * 2, l, mid);
    build(at * 2 + 1, mid + 1, r);
    int al = ans[at * 2], ar = ans[at * 2 + 1];
    int bl = lget(l, mid), br = rget(mid + 1, r);
    bl -= al / 2, br -= ar / 2;
    ans[at] = al + ar + 2 * min(bl, br);
  }
}
int query(int at, int l, int r, int first, int second) {
  if (first > second) return 0;
  if (l == first && r == second) return ans[at];
  int mid = (l + r) / 2;
  int al = query(at * 2, l, mid, first, min(second, mid));
  int ar = query(at * 2 + 1, mid + 1, r, max(first, mid + 1), second);
  int bl = lget(first, min(second, mid));
  int br = rget(max(first, mid + 1), second);
  bl -= al / 2, br -= ar / 2;
  return al + ar + 2 * min(bl, br);
}
int a, b, q;
int main(int argc, char const* argv[]) {
  ios::sync_with_stdio(false);
  cin >> str;
  n = (int)(str.size());
  for (int i = 1; i <= n; i += 1) {
    lc[i] = lc[i - 1], rc[i] = rc[i - 1];
    if (str[i - 1] == '(')
      lc[i]++;
    else
      rc[i]++;
  }
  build(1, 1, n);
  cin >> q;
  for (int i = 0; i < q; i += 1) {
    cin >> a >> b;
    cout << query(1, 1, n, a, b) << '\n';
  }
  return 0;
}
