#include <bits/stdc++.h>
using namespace std;
const int oo = 1e9 + 1;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 5;
int n, m, u, v;
string s;
struct Node {
  int optimal, close, open;
};
Node St[4 * maxn], res;
Node Join(const Node &x, const Node &y) {
  Node kq;
  kq.optimal = x.optimal + y.optimal + min(x.open, y.close) * 2;
  kq.close = x.close;
  kq.open = y.open;
  if (x.open > y.close)
    kq.open += x.open - y.close;
  else
    kq.close += y.close - x.open;
  return kq;
}
void Init() {
  cin >> s;
  n = s.length();
  s.insert(0, " ");
}
void Build(int l, int r, int id) {
  if (l == r) {
    St[id].optimal = 0;
    if (s[l] == ')')
      St[id].close = 1;
    else
      St[id].open = 1;
    return;
  }
  int mid = (l + r) / 2;
  Build(l, mid, id * 2);
  Build(mid + 1, r, id * 2 + 1);
  St[id] = Join(St[id * 2], St[id * 2 + 1]);
}
void Query(int l, int r, int id, int u, int v) {
  if (u > r || l > v) return;
  if (u <= l && r <= v) {
    res = Join(res, St[id]);
    return;
  }
  int mid = (l + r) / 2;
  Query(l, mid, id * 2, u, v);
  Query(mid + 1, r, id * 2 + 1, u, v);
}
void Solve() {
  cin >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> u >> v;
    res.optimal = res.open = res.close = 0;
    Query(1, n, 1, u, v);
    cout << res.optimal << endl;
  }
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  Init();
  Build(1, n, 1);
  Solve();
}
