#include <bits/stdc++.h>   using namespace std;   const int fx[] = {+1, -1, +0, +0}, fy[] = {+0, +0, +1, -1};   const long long mod = 1e9 + 7, INF = 1e18, mx = 1e6;   int Case = 1, cas = 0;   string S;   struct exo {     int Open, Close, SubLen;     exo() {}     exo(int a, int b, int c) { Open = a, Close = b, SubLen = c; }   } tree[3 * mx + 5];   exo Combine(exo p, exo q) {     int mn = min(p.Open, q.Close);     int a = p.Open + q.Open - mn;     int b = p.Close + q.Close - mn;     int c = p.SubLen + q.SubLen + 2 * mn;     return exo(a, b, c);   }   void build(int node, int L, int R) {     if (L > R) return;     if (L == R) {       if (S[L] ==  ( )         tree[node] = exo(1, 0, 0);       else         tree[node] = exo(0, 1, 0);       return;     }     int mid = (L + R) >> 1;     int Left = node << 1;     int Right = Left | 1;     build(Left, L, mid);     build(Right, mid + 1, R);     tree[node] = Combine(tree[Left], tree[Right]);   }   exo query(int node, int L, int R, int i, int j) {     if (i > R || j < L) return exo(0, 0, 0);     if (i <= L && R <= j) return tree[node];     int mid = (L + R) >> 1;     int Left = node << 1;     int Right = Left | 1;     exo p = query(Left, L, mid, i, j);     exo q = query(Right, mid + 1, R, i, j);     return Combine(p, q);   }   int main() {     ios_base ::sync_with_stdio(0), cin.tie(0);     ;     cin >> S;     int n = S.size();     build(1, 0, n - 1);     int Q;     cin >> Q;     while (Q--) {       int L, R;       cin >> L >> R;       L--, R--;       cout << query(1, 0, n - 1, L, R).SubLen << endl;     }     return 0;   }   