#include <bits/stdc++.h>
using namespace std;
pair<long long int, long long int> tree[5 * 1000006];
string st;
void go(int node, int low, int high) {
  if (low == high) {
    if (st[low] == '(') {
      tree[node].second = 1;
      tree[node].first = 0;
    } else {
      tree[node].second = 0;
      tree[node].first = 1;
    }
    return;
  }
  int mid = (low + high) / 2;
  go(2 * node, low, mid);
  go(2 * node + 1, mid + 1, high);
  int mn = min(tree[2 * node].second, tree[2 * node + 1].first);
  tree[node].second = tree[2 * node].second + tree[2 * node + 1].second;
  tree[node].first = tree[2 * node + 1].first + tree[2 * node].first;
  tree[node].second -= mn;
  tree[node].first -= mn;
}
pair<long long int, long long int> ask(int node, int low, int high, int l,
                                       int r) {
  if (low > r or high < l) return make_pair(0, 0);
  if (low >= l and high <= r) {
    return tree[node];
  }
  int mid = (low + high) / 2;
  pair<long long int, long long int> a = ask(2 * node, low, mid, l, r);
  pair<long long int, long long int> b = ask(2 * node + 1, mid + 1, high, l, r);
  int mn = min(a.second, b.first);
  a.second -= mn, b.first -= mn;
  pair<long long int, long long int> ans =
      make_pair(a.first + b.first, a.second + b.second);
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> st;
  int n = st.size() - 1;
  go(1, 0, n);
  int q;
  cin >> q;
  while (q--) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    pair<long long int, long long int> ans = ask(1, 0, n, l, r);
    cout << (r - l) + 1 - (ans.first + ans.second) << '\n';
  }
  return 0;
}
