#include <bits/stdc++.h>
using std::max;
using std::min;
using std::vector;
vector<bool> p;
int main() {
  {
    char c;
    do {
      scanf("%c", &c);
      if (c == '(')
        p.push_back(true);
      else if (c == ')')
        p.push_back(false);
    } while (c == '(' || c == ')');
  }
  int n = p.size();
  vector<int> psum;
  {
    int s = 0;
    psum.push_back(s);
    for (int i = 0; i < n; ++i) {
      if (p[i])
        ++s;
      else
        --s;
      psum.push_back(s);
    }
  }
  vector<vector<int>> mins;
  {
    int len = 1;
    mins.push_back(psum);
    for (len = 2 * len; len <= n + 1; len = 2 * len) {
      vector<int> m;
      const vector<int>& prev = mins.back();
      for (int i = 0; i + len <= n + 1; ++i)
        m.push_back(min(prev[i], prev[i + len / 2]));
      mins.push_back(m);
    }
  }
  int tests;
  scanf("%d", &tests);
  while (tests--) {
    int l, r;
    scanf("%d %d", &l, &r);
    int m;
    {
      int len;
      int k;
      for (k = 0, len = 1; 2 * len <= r - l + 2; len *= 2, ++k)
        ;
      m = min(mins[k][l - 1], mins[k][r - len + 1]);
    }
    if (0) printf("XXX %d %d %d\n", psum[l - 1], m, psum[r]);
    printf("%d\n",
           r - l + 1 - 2 * max(0, psum[l - 1] - m) - psum[r] + psum[l - 1]);
  }
}
