#include <bits/stdc++.h>
using namespace std;
template <class C>
void mini(C& a4, C b4) {
  a4 = min(a4, b4);
}
template <class C>
void maxi(C& a4, C b4) {
  a4 = max(a4, b4);
}
template <class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2> pair) {
  return out << "(" << pair.X << ", " << pair.Y << ")";
}
const int N = 1e6 + 20;
const int M = 1 << 20;
int drz[2 * M + 5];
string input;
int pref[N];
int czyt(int a, int b) {
  a += M;
  b += M;
  int wyn = min(drz[a], drz[b]);
  while (a / 2 != b / 2) {
    if (a % 2 == 0) {
      mini(wyn, drz[a + 1]);
    }
    if (b % 2 == 1) {
      mini(wyn, drz[b - 1]);
    }
    a /= 2;
    b /= 2;
  }
  return wyn;
}
int main() {
  cout << fixed << setprecision(10);
  getline(cin, input);
  int n = input.length();
  int ind = M + 1;
  for (int i = (1); i <= (n); ++i) {
    if (input[i - 1] == '(') {
      drz[ind] = drz[ind - 1] + 1;
      pref[i] = pref[i - 1];
    } else {
      drz[ind] = drz[ind - 1] - 1;
      pref[i] = pref[i - 1] + 1;
    }
    ind++;
    ;
  }
  for (int i = (M - 1); i >= (1); --i) {
    drz[i] = min(drz[2 * i], drz[2 * i + 1]);
  }
  int m;
  cin >> m;
  ;
  for (int i = (1); i <= (m); ++i) {
    int l, r;
    cin >> l >> r;
    ;
    cout << 2 * (pref[r] - pref[l - 1] - drz[l + M - 1] + czyt(l - 1, r))
         << "\n";
  }
  return 0;
}
