#include <bits/stdc++.h>   using namespace std;   const double eps(1e-8);   char s[1000010];   int n, m, a, b;   vector<int> vec[1000010];   vector<int> p[1000010];   int x[1000010], y[1000010], sum[1000010], ans[1000010];   int Lowbit(int x) { return x & (-x); }   void Update(int pos, int val) {     while (pos > 0) {       sum[pos] += val;       pos -= Lowbit(pos);     }   }   int Query(int pos) {     int ret = 0;     while (pos <= n) {       ret += sum[pos];       pos += Lowbit(pos);     }     return ret;   }   void Add(int l, int r, int val) {     Update(r, val);     Update(l - 1, -val);   }   int main() {     scanf( %s , s + 1);     for (n = 1; s[n]; n++)       ;     n--;     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       scanf( %d%d , &a, &b);       vec[b].push_back(a);       p[b].push_back(i);     }     int len = 0;     for (int i = 1; i <= n; i++) {       len++;       if (s[i] ==  ( )         x[len] = 0;       else         x[len] = 1;       y[len] = i;       if (len >= 2 && x[len] == 1 && x[len - 1] == 0) {         Add(1, y[len - 1], 2);         len -= 2;       }       for (int j = 0; j < (int)vec[i].size(); j++) {         int tmp = vec[i][j];         ans[p[i][j]] = Query(tmp);       }     }     for (int i = 1; i <= m; i++) printf( %d n , ans[i]);     return 0;   }   