#include <bits/stdc++.h>
using namespace std;
const int N = 1000006;
char s[N];
pair<int, int> st[4 * N];
pair<int, int> process(const pair<int, int> &x, const pair<int, int> &y) {
  int matched = min(x.second, y.first);
  return {y.first - matched + x.first, x.second - matched + y.second};
}
void build(int l, int r, int i = 1) {
  if (l == r) {
    st[i] = {s[l] == ')', s[l] == '('};
    return;
  }
  int mid = (l + r) >> 1, li = i << 1, ri = li + 1;
  build(l, mid, li);
  build(mid + 1, r, ri);
  st[i] = process(st[li], st[ri]);
}
pair<int, int> query(int l, int r, int x, int y, int i = 1) {
  if (r < x || l > y) {
    return {0, 0};
  }
  if (x <= l && r <= y) {
    return st[i];
  }
  int mid = (l + r) >> 1, li = i << 1, ri = li + 1;
  auto ql = query(l, mid, x, y, li);
  auto qr = query(mid + 1, r, x, y, ri);
  return process(ql, qr);
}
int main() {
  int m;
  scanf("%s %d", s, &m);
  int n = strlen(s);
  build(0, n - 1);
  while (m--) {
    int l, r;
    scanf("%d %d", &l, &r);
    auto res = query(0, n - 1, l - 1, r - 1);
    printf("%d\n", r - l + 1 - res.first - res.second);
  }
}
