#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;

#define MAXN 1000006

int dp[21][MAXN];

int main()
{
    freopen(".15.in","r",stdin);
    freopen(".15.out","w",stdout);
    string str;
    cin>>str;
    int n = str.size();
    dp[0][0] = 0;
    for(int i = 0; i < n; ++i)
        dp[0][i+1] = dp[0][i] + (str[i] == '(' ? 1 : -1);

    for(int i = 1; (1<<i) <= n+1; ++i)
        for(int j = 0; j + (1<<i)-1 <= n; ++j)
            dp[i][j] = min(dp[i-1][j], dp[i-1][j + (1<<(i-1))]);

    int q;
    cin>>q;
    for(int i = 0; i < q; ++i)
    {
        int l, r;
        cin>>l>>r;
        l--;
        int k = log2(r-l+1);
        int aa = min(dp[k][l], dp[k][r-(1<<k)+1]);
        cout<<(r-l) - (dp[0][l]+dp[0][r]-aa*2)<<endl;
    }
    return 0;
}
