#include <bits/stdc++.h>   using namespace std;   pair<long long, pair<long long, long long> > stree[4000005];   char sa[1000005];   void build(long long node, long long start, long long end) {     if (start == end) {       stree[node].first = 0;       stree[node].second.first = 0;       stree[node].second.second = 0;       if (sa[start] ==  ( )         stree[node].second.first = 1;       else if (sa[start] ==  ) )         stree[node].second.second = 1;     } else {       long long mid = (start + end) / 2;       build(2 * node, start, mid);       build(2 * node + 1, mid + 1, end);       long long com =           min(stree[2 * node].second.first, stree[2 * node + 1].second.second);       long long left =           stree[2 * node].second.first + stree[2 * node + 1].second.first - com;       long long right =           stree[2 * node].second.second + stree[2 * node + 1].second.second - com;       stree[node].first = stree[2 * node].first + stree[2 * node + 1].first + com;       stree[node].second.first = left;       stree[node].second.second = right;     }   }   pair<long long, pair<long long, long long> > query(long long node,                                                      long long start,                                                      long long end, long long l,                                                      long long r) {     if (l > end || start > r) return make_pair(0, make_pair(0, 0));     if (l <= start && r >= end) return stree[node];     long long mid = (start + end) / 2;     pair<long long, pair<long long, long long> > p1 =         query(2 * node, start, mid, l, r);     pair<long long, pair<long long, long long> > p2 =         query(2 * node + 1, mid + 1, end, l, r);     long long a = min(p1.second.first, p2.second.second);     long long b = p1.second.first + p2.second.first - a;     long long c = p1.second.second + p2.second.second - a;     return make_pair(p1.first + p2.first + a, make_pair(b, c));   }   int main() {     string str;     cin >> str;     long long n, q;     n = str.length();     cin >> q;     for (long long i = 1; i <= n; i++) sa[i] = str[i - 1];     build(1, 1, n);     while (q-- != 0) {       long long x, y;       cin >> x >> y;       long long ans = 2 * query(1, 1, n, x, y).first;       cout << ans << endl;     }     return 0;   }   