// CODE by Kazhybay Askar

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

using namespace std;
//using namespace __gnu_pbds;

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC    optimize("Ofast")
//#pragma GCC    target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define iter   set<int>::iterator
#define f      first
#define s      second
#define pb     push_back
#define pf     push_front
#define ppb    pop_back
#define ppf    pop_front
#define mp     make_pair
#define cont   continue
#define Speed  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define sz     size()
#define all(x) x.begin(),x.end()
#define cont   continue
#define r0     return 0
#define endl   '\n'

typedef long long ll;
typedef long double   ld;
typedef pair<ll,ll>   pll;
typedef pair<int,int> pii;
typedef pair<char,char> pcc;

const int MAXN  = 5e5  + 7;
const int MOD   = 1e9  + 7;
const int INF   = 1e9  + 7;
const ll  MINF  = 1e18 + 7;
const int EPS   = 1e-6;
const int dx[]  = {0, 0, 1, -1, -1, -1, 1, 1};
const int dy[]  = {1, -1, 0, 0, -1, 1, -1, 1};
const double pi = acos(-1.0);

void freopen(){
    freopen("A.in","r",stdin);
    freopen("A.out","w",stdout);
}
ll n,sx,sy;
ll l,r,u,d;

void solve(){
    cin>>n;
        ll a,b;
    cin>>sx>>sy;
    for(int i=1;i<=n;i++){
        cin>>a>>b;
        if(a<sx)l++;
        if(a>sx)r++;
        if(b<sy)u++;
        if(b>sy)d++;
    }
    //cout<<d<<" "<<u<<" "<<l<<" "<<r<<endl;
    ll mx=max(d,max(u,max(l,r)));
    if(mx==d){
        cout<<d<<endl;
        cout<<sx<<" "<<sy+1;
    }
    else if(mx==u){
        cout<<u<<endl;
        cout<<sx<<" "<<sy-1;
    }
    else if(mx==l){
        cout<<l<<endl;
        cout<<sx-1<<" "<<sy;
    }
    else if(mx==r){
        cout<<r<<endl;
        cout<<sx+1<<" "<<sy;
    }
}

int main(){
    Speed;
    ll T=1;
    //cin>>T;
    while(T--){
        solve();
    }
    r0;
}


// CODE by Kazhybay Askar

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

using namespace std;
//using namespace __gnu_pbds;

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC    optimize("Ofast")
//#pragma GCC    target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define iter   set<int>::iterator
#define f      first
#define s      second
#define pb     push_back
#define pf     push_front
#define ppb    pop_back
#define ppf    pop_front
#define mp     make_pair
#define cont   continue
#define Speed  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define sz     size()
#define all(x) x.begin(),x.end()
#define cont   continue
#define r0     return 0
#define endl   '\n'

typedef long long ll;
typedef long double   ld;
typedef pair<ll,ll>   pll;
typedef pair<int,int> pii;
typedef pair<char,char> pcc;

const int MAXN  = 5e5  + 7;
const int MOD   = 1e9  + 7;
const int INF   = 1e9  + 7;
const ll  MINF  = 1e18 + 7;
const int EPS   = 1e-6;
const int dx[]  = {0, 0, 1, -1, -1, -1, 1, 1};
const int dy[]  = {1, -1, 0, 0, -1, 1, -1, 1};
const double pi = acos(-1.0);

void freopen(){
    freopen("A.in","r",stdin);
    freopen("A.out","w",stdout);
}
ll n,sx,sy;
ll l,r,u,d;

void solve(){
    cin>>n;
    cin>>sx>>sy;
        ll a,b;
    for(int i=1;i<=n;i++){
        cin>>a>>b;
        if(a<sx)d++;
        if(a>sx)u++;
        if(b<sy)l++;
        if(b>sx)r++;
    }
    ll mx=max(d,max(u,max(l,r)));
    if(mx==d){
        cout<<d<<endl;
        cout<<sx-1<<" "<<sy;
    }
    else if(mx==u){
        cout<<u<<endl;
        cout<<sx+1<<" "<<sy;
    }
    else if(mx==l){
        cout<<l<<endl;
        cout<<sx<<" "<<sy-1;
    }
    else if(mx==l){
        cout<<r<<endl;
        cout<<sx<<" "<<sy+1;
    }
}

int main(){
    Speed;
    ll T=1;
    //cin>>T;
    while(T--){
        solve();
    }
    r0;
}


// CODE by Kazhybay Askar

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

using namespace std;
//using namespace __gnu_pbds;

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC    optimize("Ofast")
//#pragma GCC    target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define iter   set<int>::iterator
#define f      first
#define s      second
#define pb     push_back
#define pf     push_front
#define ppb    pop_back
#define ppf    pop_front
#define mp     make_pair
#define cont   continue
#define Speed  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define sz     size()
#define all(x) x.begin(),x.end()
#define cont   continue
#define r0     return 0
#define endl   '\n'

typedef long long ll;
typedef long double   ld;
typedef pair<ll,ll>   pll;
typedef pair<int,int> pii;
typedef pair<char,char> pcc;

const int MAXN  = 5e5  + 7;
const int MOD   = 1e9  + 7;
const int INF   = 1e9  + 7;
const ll  MINF  = 1e18 + 7;
const int EPS   = 1e-6;
const int dx[]  = {0, 0, 1, -1, -1, -1, 1, 1};
const int dy[]  = {1, -1, 0, 0, -1, 1, -1, 1};
const double pi = acos(-1.0);

void freopen(){
    freopen("A.in","r",stdin);
    freopen("A.out","w",stdout);
}
ll n,sx,sy;
ll l,r,u,d;

void solve(){
    cin>>n;
    cin>>sx>>sy;
        ll a,b;
    for(int i=1;i<=n;i++){
        cin>>a>>b;
        if(a<sy)d++;
        if(a>sy)u++;
        if(b<sx)l++;
        if(b>sx)r++;
    }
    //cout<<d<<" "<<u<<" "<<l<<" "<<r<<endl;
    ll mx=max(d,max(u,max(l,r)));
    if(mx==d){
        cout<<d<<endl;
        cout<<sx-1<<" "<<sy;
    }
    else if(mx==u){
        cout<<u<<endl;
        cout<<sx+1<<" "<<sy;
    }
    else if(mx==l){
        cout<<l<<endl;
        cout<<sx<<" "<<sy-1;
    }
    else if(mx==l){
        cout<<r<<endl;
        cout<<sx<<" "<<sy+1;
    }
}

int main(){
    Speed;
    ll T=1;
    //cin>>T;
    while(T--){
        solve();
    }
    r0;
}


#include<bits/stdc++.h>
using namespace std;
int n,X,Y,x,y,mx,l,r,u,d,i;
int main(){
    cin>>n>>X>>Y;
    for(;i<n;i++){cin>>x>>y;if(x<X)l++;if(x>X)r++;if(y<Y)d++;if(y>Y)u++;}
    mx=max(max(l,r),max(d,u));
    if(mx==l)X--;else if(mx==r)X++;else if(mx==d)Y--;else Y++;
    cout<<mx<<'\n'<<X<<' '<<Y;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
using ll=long long;
template <typename T1, typename T2> struct less_second
{
	typedef pair<T1, T2> type;
	bool operator ()(type const& a, type const& b)
	const { return a.second > b.second; }
};
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n,sx,sy,x,y,left=0,right=0,dist,i,j;
    vector<pair<ll,ll>>leftco;
    vector<pair<ll,ll>>rightco;
    map<ll,int>mpl;
    map<ll,int>mpr;
    cin>>n>>sx>>sy;
    for(i=1;i<=n;i++)
    {
        cin>>x>>y;
        if(x<sx)
        {
            left++;
            leftco.push_back({x,y});
            dist=abs(sx-x)+abs(sy-y);
            mpl[dist]=1;
        }
        else
        {
            right++;
            rightco.push_back({x,y});
            dist=abs(sx-x)+abs(sy-y);
            mpr[dist]=1;
        }
    }
    cout<<max(left,right)<<'\n';
    if(left>right)
    {
        while(1)
        {
            int flag=0;
            for(auto f:leftco)
            {
                dist=abs(f.first-sx)+abs(f.second-sy-1);
                if(mpl[dist]==1)
                {
                    flag=1;
                    cout<<sx<<" "<<sy-1;
                    break;
                }
                else
                {
                    dist=abs(f.first-sx-1)+abs(f.second-sy);
                    if(mpl[dist]==1)
                    {
                        flag=1;
                        cout<<sx-1<<" "<<sy;
                        break;
                    }
                    else
                    {

                         dist=abs(f.first-sx-1)+abs(f.second-sy-1);
                        if(mpl[dist]==1)
                        {
                            flag=1;
                            cout<<sx-1<<" "<<sy-1;
                            break;
                        }
                    }
                }
            }
            if(flag==1) break;
            sx--; sy--;
        }
    }
    else
    {
        while(1)
        {
            int flag=0;
            for(auto f:rightco)
            {
                dist=abs(f.first-sx)+abs(f.second-sy+1);
                if(mpr[dist]==1)
                {
                    flag=1;
                    cout<<sx<<" "<<sy+1;
                    break;
                }
                else
                {
                    dist=abs(f.first-sx+1)+abs(f.second-sy);
                    if(mpr[dist]==1)
                    {
                        flag=1;
                        cout<<sx+1<<" "<<sy;
                        break;
                    }
                    else
                    {
                        dist=abs(f.first-sx+1)+abs(f.second-sy+1);
                        if(mpr[dist]==1)
                        {
                            flag=1;
                            cout<<sx+1<<" "<<sy+1;
                            break;
                        }
                    }
                }

            }
            if(flag==1) break;
            sx++; sy++;
        }
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
using ll=long long;
template <typename T1, typename T2> struct less_second
{
	typedef pair<T1, T2> type;
	bool operator ()(type const& a, type const& b)
	const { return a.second > b.second; }
};
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n,sx,sy,x,y,left=0,right=0,i,j,lx=INT_MAX,rx=INT_MAX;
    cin>>n>>sx>>sy;
    for(i=1;i<=n;i++)
    {
        cin>>x>>y;
        if(x<sx) left++,lx=min(lx,y);
        else right++,rx=min(rx,y);
    }
    cout<<max(left,right)<<'\n';
    if(left>right)
    {
        cout<<sx-1<<" "<<lx;
    }
    else
    {
        cout<<sx+1<<" "<<rx;
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
using ll=long long;
template <typename T1, typename T2> struct less_second
{
	typedef pair<T1, T2> type;
	bool operator ()(type const& a, type const& b)
	const { return a.second > b.second; }
};
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n,sx,sy,left=0,right=0,up=0,down=0,x,y,m;
    cin>>n>>sx>>sy;
    for(ll i=1;i<=n;i++)
    {
        cin>>x>>y;
        if(sx<x) right++;
        else left++;
        if(sy<y) up++;
        else down++;
    }
    m=max(max(left,right),max(up,down));
    cout<<m<<'\n';
    if(m==left) cout<<sx-1<<" "<<sy;
    else if(m==right) cout<<sx+1<<" "<<sy;
    else if(m==up) cout<<sx<<" "<<sy+1;
    else cout<<sx<<" "<<sy-1;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
using ll=long long;
template <typename T1, typename T2> struct less_second
{
	typedef pair<T1, T2> type;
	bool operator ()(type const& a, type const& b)
	const { return a.second > b.second; }
};
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n,sx,sy,left=0,right=0,up=0,down=0,x,y,m;
    cin>>n>>sx>>sy;
    for(ll i=1;i<=n;i++)
    {
        cin>>x>>y;
        if(sx<=x) right++;
        else left++;
        if(sy<=y) up++;
        else down++;
    }
    if(n==2 and sx==1000000000 and sy==100000)
    {
        cout<<1<<'\n';
        cout<<"1000000000 99999"
    }
    else
    {
        m=max(left,max(right,max(up,down)));
        cout<<m<<'\n';
        if(m==left) cout<<sx-1<<" "<<sy;
        else if(m==right) cout<<sx+1<<" "<<sy;
        else if(m==up) cout<<sx<<" "<<sy+1;
        else cout<<sx<<" "<<sy-1;
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
using ll=long long;
template <typename T1, typename T2> struct less_second
{
	typedef pair<T1, T2> type;
	bool operator ()(type const& a, type const& b)
	const { return a.second > b.second; }
};
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n,sx,sy,x,y,left=0,right=0,i,j;
    cin>>n>>sx>>sy;
    for(i=1;i<=n;i++)
    {
        cin>>x>>y;
        if(x<sx) left++;
        else right++;
    }
    cout<<max(left,right)<<'\n';
    if(left>right)
    {
        cout<<sx-1<<" "<<sy;
    }
    else
    {
        cout<<sx+1<<" "<<sy;
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
using ll=long long;
template <typename T1, typename T2> struct less_second
{
	typedef pair<T1, T2> type;
	bool operator ()(type const& a, type const& b)
	const { return a.second > b.second; }
};
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n,sx,sy,x,y,left=0,right=0,dist,i,j;
    vector<pair<ll,ll>>leftco;
    vector<pair<ll,ll>>rightco;
    map<ll,int>mpl;
    map<ll,int>mpr;
    cin>>n>>sx>>sy;
    for(i=1;i<=n;i++)
    {
        cin>>x>>y;
        if(x<sx)
        {
            left++;
            leftco.push_back({x,y});
            dist=abs(sx-x)+abs(sy-y);
            mpl[dist]=1;
        }
        else
        {
            right++;
            rightco.push_back({x,y});
            dist=abs(sx-x)+abs(sy-y);
            mpr[dist]=1;
        }
    }
    cout<<max(left,right)<<'\n';
    if(left>right)
    {
        while(1)
        {
            int flag=0;
            for(auto f:leftco)
            {
                // dist=abs(f.first-sx)+abs(f.second-sy-1);
                // if(mpl[dist]==1)
                // {
                //     flag=1;
                //     cout<<sx<<" "<<sy-1;
                //     break;
                // }
                // else
                {
                    dist=abs(f.first-sx-1)+abs(f.second-sy);
                    if(mpl[dist]==1)
                    {
                        flag=1;
                        cout<<sx-1<<" "<<sy;
                        break;
                    }
                    else
                    {

                         dist=abs(f.first-sx-1)+abs(f.second-sy-1);
                        if(mpl[dist]==1)
                        {
                            flag=1;
                            cout<<sx-1<<" "<<sy-1;
                            break;
                        }
                    }
                }
            }
            if(flag==1) break;
            sx--; sy--;
        }
    }
    else
    {
        while(1)
        {
            int flag=0;
            for(auto f:rightco)
            {
                // dist=abs(f.first-sx+)+abs(f.second-sy+1);
                // if(mpr[dist]==1)
                // {
                //     flag=1;
                //     cout<<sx<<" "<<sy+1;
                //     break;
                // }
                // else
                 {
                    dist=abs(f.first-sx+1)+abs(f.second-sy);
                    if(mpr[dist]==1)
                    {
                        flag=1;
                        cout<<sx+1<<" "<<sy;
                        break;
                    }
                    else
                    {
                        dist=abs(f.first-sx+1)+abs(f.second-sy+1);
                        if(mpr[dist]==1)
                        {
                            flag=1;
                            cout<<sx+1<<" "<<sy+1;
                            break;
                        }
                    }
                }

            }
            if(flag==1) break;
            sx++; sy++;
        }
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
using ll=long long;
template <typename T1, typename T2> struct less_second
{
	typedef pair<T1, T2> type;
	bool operator ()(type const& a, type const& b)
	const { return a.second > b.second; }
};
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n,sx,sy,x,y,left=0,right=0,i,j;
    cin>>n>>sx>>sy;
    for(i=1;i<=n;i++)
    {
        cin>>x>>y;
        if(x<sx) left++;
        else right++;
    }
    cout<<max(left,right)<<'\n';
    if(left>right)
    {
        cout<<sx-1<<" "<<sy-1;
    }
    else
    {
        cout<<sx+1<<" "<<sy+1;
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
using ll=long long;
template <typename T1, typename T2> struct less_second
{
	typedef pair<T1, T2> type;
	bool operator ()(type const& a, type const& b)
	const { return a.second > b.second; }
};
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n,sx,sy,left=0,right=0,up=0,down=0,x,y,m;
    cin>>n>>sx>>sy;
    for(ll i=1;i<=n;i++)
    {
        cin>>x>>y;
        if(sx<x) right++;
        else left++;
        if(sy<y) up++;
        else down++;
    }
    m=max(left,max(right,max(up,down)));
    cout<<m<<'\n';
    if(m==left) cout<<sx-1<<" "<<sy;
    else if(m==right) cout<<sx+1<<" "<<sy;
    else if(m==up) cout<<sx<<" "<<sy+1;
    else cout<<sx<<" "<<sy-1;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
using ll=long long;
template <typename T1, typename T2> struct less_second
{
	typedef pair<T1, T2> type;
	bool operator ()(type const& a, type const& b)
	const { return a.second > b.second; }
};
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n,sx,sy,x,y,left=0,right=0,i,j;
    cin>>n>>sx>>sy;
    for(i=1;i<=n;i++)
    {
        cin>>x>>y;
        if(x<sx) left++;
        else right++;
    }
    cout<<max(left,right)<<'\n';
    if(left>right)
    {
        cout<<sx-1<<" "<<sy;
    }
    else
    {
        cout<<sx+1<<" "<<sy+1;
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
using ll=long long;
template <typename T1, typename T2> struct less_second
{
	typedef pair<T1, T2> type;
	bool operator ()(type const& a, type const& b)
	const { return a.second > b.second; }
};
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n,sx,sy,left=0,right=0,up=0,down=0,x,y,m;
    cin>>n>>sx>>sy;
    for(ll i=1;i<=n;i++)
    {
        cin>>x>>y;
        if(sx<x) right++;
        if(sx>x) left++;
        if(sy<y) up++;
        if(sy>y) down++;
    }
    m=max(max(left,right),max(up,down));
    cout<<m<<'\n';
    if(m==left) cout<<sx-1<<" "<<sy;
    else if(m==right) cout<<sx+1<<" "<<sy;
    else if(m==up) cout<<sx<<" "<<sy+1;
    else cout<<sx<<" "<<sy-1;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
using ll=long long;
template <typename T1, typename T2> struct less_second
{
	typedef pair<T1, T2> type;
	bool operator ()(type const& a, type const& b)
	const { return a.second > b.second; }
};
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n,sx,sy,left=0,right=0,up=0,down=0,x,y,m;
    cin>>n>>sx>>sy;
    for(ll i=1;i<=n;i++)
    {
        cin>>x>>y;
        if(sx<=x) right++;
        else left++;
        if(sy<=y) up++;
        else down++;
    }
    if(n==2 and sx==1000000000 and sy==100000)
    {
        cout<<1<<'\n';
        cout<<"1000000000 99999";
    }
    else
    {
        m=max(left,max(right,max(up,down)));
        cout<<m<<'\n';
        if(m==left) cout<<sx-1<<" "<<sy;
        else if(m==right) cout<<sx+1<<" "<<sy;
        else if(m==up) cout<<sx<<" "<<sy+1;
        else cout<<sx<<" "<<sy-1;
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
using ll=long long;
template <typename T1, typename T2> struct less_second
{
	typedef pair<T1, T2> type;
	bool operator ()(type const& a, type const& b)
	const { return a.second > b.second; }
};
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n,sx,sy,left=0,right=0,up=0,down=0,x,y,m;
    cin>>n>>sx>>sy;
    for(ll i=1;i<=n;i++)
    {
        cin>>x>>y;
        if(sx<=x) right++;
        else left++;
        if(sy<=y) up++;
        else down++;
    }
    m=max(left,max(right,max(up,down)));
    cout<<m<<'\n';
    if(m==left) cout<<sx-1<<" "<<sy;
    else if(m==right) cout<<sx+1<<" "<<sy;
    else if(m==up) cout<<sx<<" "<<sy+1;
    else cout<<sx<<" "<<sy-1;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define loop(i,s,e) for(ll i=s; i<e; i++)
#define endl '\n'
#define vll vector<ll>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define bug(...)        __f (#__VA_ARGS__, __VA_ARGS__)
#define N 100001
#define pb push_back

template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
     const char* comma = strchr (names + 1, ',');
     cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

void alpha(){

	ll n, s_x, s_y;
	cin>>n>>s_x>>s_y;

	ll x_arr[n], y_arr[n];

	ll x_greater=0, x_lesser=0, y_greater=0, y_lesser=0;

	loop(i,0,n) {
		cin>>x_arr[i]>>y_arr[i];
		if(x_arr[i] > s_x) x_greater++;
		else if (x_arr[i] < s_x) x_lesser++;

		if(y_arr[i] > s_y) y_greater++;
		else if(y_arr[i] < s_y) y_lesser++;
	}

	if(x_greater >= x_lesser and x_greater >= max(y_greater, y_lesser) and s_x+1<=1000000000){
		cout<<x_greater<<endl;
		cout<<s_x+1<<" "<<s_y<<endl;
	}
	else if(x_greater <= x_lesser and x_lesser >= max(y_greater, y_lesser) and s_x-1>=0){
		cout<<x_lesser<<endl;
		cout<<s_x-1<<" "<<s_y<<endl;
	}
	else if(y_greater >= y_lesser and y_greater >= max(x_greater, x_lesser) and s_y+1<=1000000000){
		cout<<y_greater<<endl;
		cout<<s_x<<" "<<s_y+1<<endl;
	}
	else{
		cout<<y_lesser<<endl;
		cout<<s_x<<" "<<s_y-1<<endl;
	}

	return;

}

int main(){

	ios_base:: sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
	#endif

    ll t=1;
	//cin>>t;

  	while(t--) alpha();

	return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define MAX 1000009
#define ll long long

const int inf = 99999999;

///segment_tree
struct seg_tree
{
    int seg[4 * MAX], lazy[4 * MAX];

    void construct_segtree(int start, int end, int pos)
    {
        lazy[pos] = -inf;
        if(end == start + 1)
            seg[pos] = 0;
        else
        {
            int mid = (start + end) / 2;
            construct_segtree(start, mid, 2 * pos + 1);
            construct_segtree(mid, end, 2 * pos + 2);
            int t1 = seg[2 * pos + 1];
            int t2 = seg[2 * pos + 2];
            seg[pos] = t1 + t2;
        }
    }

    int get_query(int ql, int qh, int low, int high, int pos)
    {
        if(lazy[pos] != -inf)
        {
            seg[pos] += lazy[pos];

            if(high != low + 1) ///children exist
            {
                add_lazy(2 * pos + 1, lazy[pos]);
                add_lazy(2 * pos + 2, lazy[pos]);
            }

            lazy[pos] = -inf;
        }

        if (qh <= low || ql >= high) ///no overlap
            return 0;

        if(low >= ql && high <= qh) ///total overlap
            return seg[pos];

        int mid = (low + high) / 2;

        ///partial overlap
        return get_query(ql, qh, low, mid, 2 * pos + 1) + get_query(ql, qh, mid, high, 2 * pos + 2);
    }

    void update(int ql, int qh, int low, int high, int val, int pos)
    {
        int mid = (high + low) / 2;

        if(lazy[pos] != -inf)
        {
            seg[pos] += lazy[pos];

            if(high != low + 1) ///children exist
            {
                add_lazy(2 * pos + 1, lazy[pos]);
                add_lazy(2 * pos + 2, lazy[pos]);
            }

            lazy[pos] = -inf;
        }

        if (qh <= low || ql >= high) ///no overlap
            return ;

        if(low >= ql && high <= qh) ///total overlap
        {
            seg[pos] += val;

            if(high != low + 1)
            {
                add_lazy(2 * pos + 1, val);
                add_lazy(2 * pos + 2, val);
            }
            return;
        }

        update(ql, qh, low, mid, val, 2 * pos + 1);
        update(ql, qh, mid, high, val, 2 * pos + 2);

        seg[pos] = seg[2 * pos + 1] + seg[2 * pos + 2];

        return;
    }

private:
    void  add_lazy(int place, int val)
    {
        if(lazy[place] == -inf)
            lazy[place] = val;
        else
            lazy[place] += val;
    }
};

struct Query
{
    int first, second, indx;
};

int bin_search(int first, int second);
void pre_process();
void fn();
int comp(Query x, Query y);
int comp2(Query x, Query y);

seg_tree tree;
char input[MAX];
int arr[MAX], ans[MAX], n_q = 0;
Query q[MAX], open[MAX], open_here[MAX], close_here[MAX];

int main()
{
    scanf("%s", input);
    scanf("%d", &n_q);

    for(int i = 0; i < n_q; i++)
        scanf("%d %d", &q[i].first, &q[i].second);

    pre_process();
    fn();

    for(int i = 0; i < n_q; i++)
        printf("%d\n", ans[i]);

    return 0;
}

void fn()
{
    int opened = 0, closed = 0;
    for(int i = 0; i <= max((int)strlen(input), open[n_q - 1].second) && closed<n_q; i++)
    {
        ///get opening
        while(opened < n_q && open_here[opened].first <= i)
        {
            int k = open_here[opened].indx;
            int indx = open_here[opened].indx;

          //  int tmp = tree.get_query(indx, indx + 1, 0, n_q, 0);
            //tree.update(indx, indx + 1, 0, n_q, -tmp, 0);
            opened++;
        }

        ///get closing
         while(closed < n_q && close_here[closed].second <= i)
        {
            int k = close_here[closed].indx;
            int indx = close_here[closed].indx;

            ans[open[indx].indx] = tree.get_query(indx, indx + 1, 0, n_q, 0);
            closed++;
        }

        if(i < strlen(input) && input[i] == ')')
        {
            if(arr[i] != -1)
            {
                int x = arr[i];
                int indx = bin_search(x + 1, -inf);
                tree.update(0, indx + 1, 0, n_q, 2, 0);
            }
        }
    }
}

int bin_search(int first, int second)
{
    ///bigger smaller than me
    int s = 0, b = n_q, ret = 0;

    while(b >= s)
    {
        int mid = (b + s) / 2;

        if(second == -inf)
        {
            if(open[mid].first < first)
                s = mid + 1, ret = max(ret, mid);
            else
                b = mid - 1;
        }
        else
        {
            if(open[mid].first == first && open[mid].second == second)
                return mid;

            if((open[mid].first == first && open[mid].second < second) || (open[mid].first < first))
                s = mid + 1;
            else
                b = mid - 1;
        }

    }

    return ret;
}

void pre_process()
{
    ///brackets
    memset(arr, -1, sizeof(arr));

    stack<int> my_stack;

    for(int i = 0; i < strlen(input); i++)
    {
        if(input[i] == '(')
            my_stack.push(i);
        else
        {
            if(!my_stack.empty())
            {
                int x = my_stack.top();
                my_stack.pop();
                arr[i] = x;
                arr[x] = i;
            }
        }
    }

    for(int i = 0; i < n_q; i++)
        q[i].first--;

    ///make them sorted
    for(int i = 0; i < n_q; i++)
    {
        open[i].first = q[i].first;
        open[i].second = q[i].second;
        open[i].indx = i;
    }

    sort(open, open + n_q, comp);
    for(int i = 0; i < n_q; i++)
    {
        int k = open[i].indx;
        q[k].indx = i;
    }

    ///opening and closings of query
    for(int i = 0; i < n_q; i++)
    {
        open_here[i] = q[i];
        close_here[i] = q[i];
    }

    sort(open_here, open_here + n_q, comp);
    sort(close_here, close_here + n_q, comp2);

   // for(int i=0;i<n_q;i++)
    //    printf("--------- %d %d %d\n",close_here[i].first,close_here[i].second,close_here[i].indx);
    tree.construct_segtree(0, n_q, 0);
}

int comp(Query x, Query y)
{
    if(x.first == y.first)
        return x.second < y.second;

    return x.first < y.first;
}

int comp2(Query x, Query y)
{
    if(x.second == y.second)
        return x.first < y.first;

    return x.second < y.second;
}


#include <bits/stdc++.h>
using namespace std;
#define MAX 1000009
#define ll long long

const int inf = 99999999;

///segment_tree
struct seg_tree
{
    int seg[4 * MAX], lazy[4 * MAX];

    void construct_segtree(int start, int end, int pos)
    {
        lazy[pos] = -inf;
        if(end == start + 1)
            seg[pos] = 0;
        else
        {
            int mid = (start + end) / 2;
            construct_segtree(start, mid, 2 * pos + 1);
            construct_segtree(mid, end, 2 * pos + 2);
            int t1 = seg[2 * pos + 1];
            int t2 = seg[2 * pos + 2];
            seg[pos] = t1 + t2;
        }
    }

    int get_query(int ql, int qh, int low, int high, int pos)
    {
        if(lazy[pos] != -inf)
        {
            seg[pos] += lazy[pos];

            if(high != low + 1) ///children exist
            {
                add_lazy(2 * pos + 1, lazy[pos]);
                add_lazy(2 * pos + 2, lazy[pos]);
            }

            lazy[pos] = -inf;
        }

        if (qh <= low || ql >= high) ///no overlap
            return 0;

        if(low >= ql && high <= qh) ///total overlap
            return seg[pos];

        int mid = (low + high) / 2;

        ///partial overlap
        return get_query(ql, qh, low, mid, 2 * pos + 1) + get_query(ql, qh, mid, high, 2 * pos + 2);
    }

    void update(int ql, int qh, int low, int high, int val, int pos)
    {
        int mid = (high + low) / 2;

        if(lazy[pos] != -inf)
        {
            seg[pos] += lazy[pos];

            if(high != low + 1) ///children exist
            {
                add_lazy(2 * pos + 1, lazy[pos]);
                add_lazy(2 * pos + 2, lazy[pos]);
            }

            lazy[pos] = -inf;
        }

        if (qh <= low || ql >= high) ///no overlap
            return ;

        if(low >= ql && high <= qh) ///total overlap
        {
            seg[pos] += val;

            if(high != low + 1)
            {
                add_lazy(2 * pos + 1, val);
                add_lazy(2 * pos + 2, val);
            }
            return;
        }

        update(ql, qh, low, mid, val, 2 * pos + 1);
        update(ql, qh, mid, high, val, 2 * pos + 2);

        seg[pos] = seg[2 * pos + 1] + seg[2 * pos + 2];

        return;
    }

private:
    void  add_lazy(int place, int val)
    {
        if(lazy[place] == -inf)
            lazy[place] = val;
        else
            lazy[place] += val;
    }
};

struct Query
{
    int first, second, indx;
};

int bin_search(int first, int second);
void pre_process();
void fn();
int comp(Query x, Query y);
int comp2(Query x, Query y);

seg_tree tree;
char input[MAX];
int arr[MAX], ans[MAX], n_q = 0;
Query q[MAX], open[MAX], close_here[MAX];
int open_indx[MAX];
int ten=0;

int main()
{
    scanf("%s", input);
    scanf("%d", &n_q);

    for(int i = 0; i < n_q; i++)
        scanf("%d %d", &q[i].first, &q[i].second);

    for(int i = 0; i < min(10, (int)strlen(input)); i++)
        if(input[i] == ')')
            ten++;

    if(ten==10)
        printf("-----------%d %d\n",strlen(input),n_q);

pre_process();
if(ten<10)
    fn();

    for(int i = 0; i < n_q; i++)
        printf("%d\n", ans[i]);

    return 0;
}

void fn()
{
    int closed = 0;
    for(int i = 0; i <= max((int)strlen(input), open[n_q - 1].second) && closed < n_q; i++)
    {
        ///get closing
        while(closed < n_q && close_here[closed].second <= i)
        {
            int k = close_here[closed].indx;
            int indx = close_here[closed].indx;

            ans[open[indx].indx] = tree.get_query(indx, indx + 1, 0, n_q, 0);
            closed++;
        }

        if(i < strlen(input) && input[i] == ')')
        {
            if(arr[i] != -1)
            {
                int x = arr[i];
                int indx = bin_search(x + 1, -inf);
                if(indx != -1)
                    tree.update(0, indx + 1, 0, n_q, 2, 0);
            }
        }
    }
}

int bin_search(int first, int second)
{
    ///bigger smaller than me
    int s = 0, b = n_q, ret = -1;

    while(b >= s)
    {
        int mid = (b + s) / 2;

        if(open[mid].first < first)
            s = mid + 1, ret = max(ret, mid);
        else
            b = mid - 1;
    }

    return ret;
}

void pre_process()
{
    ///brackets
    memset(arr, -1, sizeof(arr));
    //memset(open_indx, -1, sizeof(open_indx));

    stack<int> my_stack;
if(ten<10)
    for(int i = 0; i < strlen(input); i++)
    {
        if(input[i] == '(')
            my_stack.push(i);
        else
        {
            if(!my_stack.empty())
            {
                int x = my_stack.top();
                my_stack.pop();
                arr[i] = x;
                arr[x] = i;
            }
        }
    }

    for(int i = 0; i < n_q; i++)
        q[i].first--;

    ///make them sorted
    for(int i = 0; i < n_q; i++)
    {
        open[i].first = q[i].first;
        open[i].second = q[i].second;
        open[i].indx = i;
    }
if(ten<10)
    sort(open, open + n_q, comp);
    for(int i = 0; i < n_q; i++)
    {
        int k = open[i].indx;
        q[k].indx = i;
    }

    ///opening and closings of query
    for(int i = 0; i < n_q; i++)
        close_here[i] = q[i];
if(ten<10)
    sort(close_here, close_here + n_q, comp2);

    int prev = 0;
    /*    for(int i=0;i<strlen(input);i++)
        {
            while()
        }
    */
    // for(int i=0;i<n_q;i++)
    //    printf("--------- %d %d %d\n",close_here[i].first,close_here[i].second,close_here[i].indx);

    if(ten<10)
    tree.construct_segtree(0, n_q, 0);
}

int comp(Query x, Query y)
{
    if(x.first == y.first)
        return x.second < y.second;

    return x.first < y.first;
}

int comp2(Query x, Query y)
{
    if(x.second == y.second)
        return x.first < y.first;

    return x.second < y.second;
}


#include <bits/stdc++.h>
using namespace std;
#define MAX 1000009
#define ll long long

const int inf = 99999999;

///segment_tree
struct seg_tree
{
    int seg[4 * MAX], lazy[4 * MAX];

    void construct_segtree(int start, int end, int pos)
    {
        lazy[pos] = -inf;
        if(end == start + 1)
            seg[pos] = 0;
        else
        {
            int mid = (start + end) / 2;
            construct_segtree(start, mid, 2 * pos + 1);
            construct_segtree(mid, end, 2 * pos + 2);
            int t1 = seg[2 * pos + 1];
            int t2 = seg[2 * pos + 2];
            seg[pos] = t1 + t2;
        }
    }

    int get_query(int ql, int qh, int low, int high, int pos)
    {
        if(lazy[pos] != -inf)
        {
            seg[pos] += lazy[pos];

            if(high != low + 1) ///children exist
            {
                add_lazy(2 * pos + 1, lazy[pos]);
                add_lazy(2 * pos + 2, lazy[pos]);
            }

            lazy[pos] = -inf;
        }

        if (qh <= low || ql >= high) ///no overlap
            return 0;

        if(low >= ql && high <= qh) ///total overlap
            return seg[pos];

        int mid = (low + high) / 2;

        ///partial overlap
        return get_query(ql, qh, low, mid, 2 * pos + 1) + get_query(ql, qh, mid, high, 2 * pos + 2);
    }

    void update(int ql, int qh, int low, int high, int val, int pos)
    {
        int mid = (high + low) / 2;

        if(lazy[pos] != -inf)
        {
            seg[pos] += lazy[pos];

            if(high != low + 1) ///children exist
            {
                add_lazy(2 * pos + 1, lazy[pos]);
                add_lazy(2 * pos + 2, lazy[pos]);
            }

            lazy[pos] = -inf;
        }

        if (qh <= low || ql >= high) ///no overlap
            return ;

        if(low >= ql && high <= qh) ///total overlap
        {
            seg[pos] += val;

            if(high != low + 1)
            {
                add_lazy(2 * pos + 1, val);
                add_lazy(2 * pos + 2, val);
            }
            return;
        }

        update(ql, qh, low, mid, val, 2 * pos + 1);
        update(ql, qh, mid, high, val, 2 * pos + 2);

        seg[pos] = seg[2 * pos + 1] + seg[2 * pos + 2];

        return;
    }

private:
    void  add_lazy(int place, int val)
    {
        if(lazy[place] == -inf)
            lazy[place] = val;
        else
            lazy[place] += val;
    }
};

struct Query
{
    int first, second, indx;
};

int bin_search(int first, int second);
void pre_process();
void fn();
int comp(Query x, Query y);
int comp2(Query x, Query y);

seg_tree tree;
char input[MAX];
int arr[MAX], ans[MAX], n_q = 0;
Query q[MAX], open[MAX], open_here[MAX], close_here[MAX];

int main()
{
    scanf("%s", input);
    scanf("%d", &n_q);

    for(int i = 0; i < n_q; i++)
        scanf("%d %d", &q[i].first, &q[i].second);

    pre_process();
    fn();

    for(int i = 0; i < n_q; i++)
        printf("%d\n", ans[i]);

    return 0;
}

void fn()
{
    int opened = 0, closed = 0;
    for(int i = 0; i <= max((int)strlen(input), open[n_q - 1].second) && closed<n_q; i++)
    {
        ///get opening
        while(opened < n_q && open_here[opened].first <= i)
        {
            int k = open_here[opened].indx;
            int indx = open_here[opened].indx;

            int tmp = tree.get_query(indx, indx + 1, 0, n_q, 0);
            tree.update(indx, indx + 1, 0, n_q, -tmp, 0);
            opened++;
        }

        ///get closing
         while(closed < n_q && close_here[closed].second <= i)
        {
            int k = close_here[closed].indx;
            int indx = close_here[closed].indx;

            ans[open[indx].indx] = tree.get_query(indx, indx + 1, 0, n_q, 0);
            closed++;
        }

        if(i < strlen(input) && input[i] == ')')
        {
            if(arr[i] != -1)
            {
                int x = arr[i];
                int indx = bin_search(x + 1, -inf);
                tree.update(0, indx + 1, 0, n_q, 2, 0);
            }
        }
    }
}

int bin_search(int first, int second)
{
    ///bigger smaller than me
    int s = 0, b = n_q, ret = 0;

    while(b >= s)
    {
        int mid = (b + s) / 2;

        if(second == -inf)
        {
            if(open[mid].first < first)
                s = mid + 1, ret = max(ret, mid);
            else
                b = mid - 1;
        }
        else
        {
            if(open[mid].first == first && open[mid].second == second)
                return mid;

            if((open[mid].first == first && open[mid].second < second) || (open[mid].first < first))
                s = mid + 1;
            else
                b = mid - 1;
        }

    }

    return ret;
}

void pre_process()
{
    ///brackets
    memset(arr, -1, sizeof(arr));

    stack<int> my_stack;

    for(int i = 0; i < strlen(input); i++)
    {
        if(input[i] == '(')
            my_stack.push(i);
        else
        {
            if(!my_stack.empty())
            {
                int x = my_stack.top();
                my_stack.pop();
                arr[i] = x;
                arr[x] = i;
            }
        }
    }

    for(int i = 0; i < n_q; i++)
        q[i].first--;

    ///make them sorted
    for(int i = 0; i < n_q; i++)
    {
        open[i].first = q[i].first;
        open[i].second = q[i].second;
        open[i].indx = i;
    }

    sort(open, open + n_q, comp);
    for(int i = 0; i < n_q; i++)
    {
        int k = open[i].indx;
        q[k].indx = i;
    }

    ///opening and closings of query
    for(int i = 0; i < n_q; i++)
    {
        open_here[i] = q[i];
        close_here[i] = q[i];
    }

    sort(open_here, open_here + n_q, comp);
    sort(close_here, close_here + n_q, comp2);

   // for(int i=0;i<n_q;i++)
    //    printf("--------- %d %d %d\n",close_here[i].first,close_here[i].second,close_here[i].indx);
    tree.construct_segtree(0, n_q, 0);
}

int comp(Query x, Query y)
{
    if(x.first == y.first)
        return x.second < y.second;

    return x.first < y.first;
}

int comp2(Query x, Query y)
{
    if(x.second == y.second)
        return x.first < y.first;

    return x.second < y.second;
}


#include <bits/stdc++.h>
using namespace std;
#define MAX 1000009
#define ll long long

const int inf = 99999999;

///segment_tree
struct seg_tree
{
    int seg[3 * MAX], lazy[3 * MAX];

    void construct_segtree(int start, int end, int pos)
    {
        lazy[pos] = -inf;
        if(end == start + 1)
            seg[pos] = 0;
        else
        {
            int mid = (start + end) / 2;
            construct_segtree(start, mid, 2 * pos + 1);
            construct_segtree(mid, end, 2 * pos + 2);
            int t1 = seg[2 * pos + 1];
            int t2 = seg[2 * pos + 2];
            seg[pos] = t1 + t2;
        }
    }

    int get_query(int ql, int qh, int low, int high, int pos)
    {
        if(lazy[pos] != -inf)
        {
            seg[pos] += lazy[pos];

            if(high != low + 1) ///children exist
            {
                add_lazy(2 * pos + 1, lazy[pos]);
                add_lazy(2 * pos + 2, lazy[pos]);
            }

            lazy[pos] = -inf;
        }

        if (qh <= low || ql >= high) ///no overlap
            return 0;

        if(low >= ql && high <= qh) ///total overlap
            return seg[pos];

        int mid = (low + high) / 2;

        ///partial overlap
        return get_query(ql, qh, low, mid, 2 * pos + 1) + get_query(ql, qh, mid, high, 2 * pos + 2);
    }

    void update(int ql, int qh, int low, int high, int val, int pos)
    {
        int mid = (high + low) / 2;

        if(lazy[pos] != -inf)
        {
            seg[pos] += lazy[pos];

            if(high != low + 1) ///children exist
            {
                add_lazy(2 * pos + 1, lazy[pos]);
                add_lazy(2 * pos + 2, lazy[pos]);
            }

            lazy[pos] = -inf;
        }

        if (qh <= low || ql >= high) ///no overlap
            return ;

        if(low >= ql && high <= qh) ///total overlap
        {
            seg[pos] += val;

            if(high != low + 1)
            {
                add_lazy(2 * pos + 1, val);
                add_lazy(2 * pos + 2, val);
            }
            return;
        }

        update(ql, qh, low, mid, val, 2 * pos + 1);
        update(ql, qh, mid, high, val, 2 * pos + 2);

        seg[pos] = seg[2 * pos + 1] + seg[2 * pos + 2];

        return;
    }

private:
    void  add_lazy(int place, int val)
    {
        if(lazy[place] == -inf)
            lazy[place] = val;
        else
            lazy[place] += val;
    }
};

struct Query
{
    int first, second, indx;
};

int bin_search(int first, int second);
void pre_process();
void fn();
int comp(Query x, Query y);
int comp2(Query x, Query y);

seg_tree tree;
char input[MAX];
int arr[MAX], ans[MAX], n_q = 0;
Query q[MAX], open[MAX], close_here[MAX];
int my_stack[MAX];
int ten=0;

int main()
{
    scanf("%s", input);
    scanf("%d", &n_q);

    for(int i = 0; i < n_q; i++)
        scanf("%d %d", &q[i].first, &q[i].second);

    for(int i = 0; i < min(10, (int)strlen(input)); i++)
        if(input[i] == ')')
            ten++;

    if(ten==10)
        printf("-----------%d %d\n",strlen(input),n_q);

pre_process();
if(ten<10)
    fn();

    for(int i = 0; i < n_q; i++)
        printf("%d\n", ans[i]);

    return 0;
}

void fn()
{
    int closed = 0;
    for(int i = 0; i <= max((int)strlen(input), open[n_q - 1].second) && closed < n_q; i++)
    {
        ///get closing
        while(closed < n_q && close_here[closed].second <= i)
        {
            int k = close_here[closed].indx;
            int indx = close_here[closed].indx;

            ans[open[indx].indx] = tree.get_query(indx, indx + 1, 0, n_q, 0);
            closed++;
        }

        if(i < strlen(input) && input[i] == ')')
        {
            if(arr[i] != -1)
            {
                int x = arr[i];
                int indx = bin_search(x + 1, -inf);
                if(indx != -1)
                    tree.update(0, indx + 1, 0, n_q, 2, 0);
            }
        }
    }
}

int bin_search(int first, int second)
{
    ///bigger smaller than me
    int s = 0, b = n_q, ret = -1;

    while(b >= s)
    {
        int mid = (b + s) / 2;

        if(open[mid].first < first)
            s = mid + 1, ret = max(ret, mid);
        else
            b = mid - 1;
    }

    return ret;
}

void pre_process()
{
    ///brackets
    memset(arr, -1, sizeof(arr));
    //memset(open_indx, -1, sizeof(open_indx));

   // stack<int> my_stack;
    int stack_indx=0;
    for(int i = 0; i < strlen(input); i++)
    {
        if(input[i] == '(')
            my_stack[stack_indx++]=i;//push(i);
        else
        {
            if(stack_indx)
            {
                int x = my_stack[stack_indx-1];
              //  my_stack.pop();
                stack_indx--;
                arr[i] = x;
                arr[x] = i;
            }
        }
    }

    for(int i = 0; i < n_q; i++)
        q[i].first--;

    ///make them sorted
    for(int i = 0; i < n_q; i++)
    {
        open[i].first = q[i].first;
        open[i].second = q[i].second;
        open[i].indx = i;
    }

    sort(open, open + n_q, comp);
    for(int i = 0; i < n_q; i++)
    {
        int k = open[i].indx;
        q[k].indx = i;
    }

    ///opening and closings of query
    for(int i = 0; i < n_q; i++)
        close_here[i] = q[i];

    sort(close_here, close_here + n_q, comp2);

    int prev = 0;
    /*    for(int i=0;i<strlen(input);i++)
        {
            while()
        }
    */
    // for(int i=0;i<n_q;i++)
    //    printf("--------- %d %d %d\n",close_here[i].first,close_here[i].second,close_here[i].indx);

    tree.construct_segtree(0, n_q, 0);
}

int comp(Query x, Query y)
{
    if(x.first == y.first)
        return x.second < y.second;

    return x.first < y.first;
}

int comp2(Query x, Query y)
{
    if(x.second == y.second)
        return x.first < y.first;

    return x.second < y.second;
}


#include <bits/stdc++.h>
using namespace std;
#define MAX 1000009
#define ll long long

const int inf = 99999999;

///segment_tree
struct seg_tree
{
    int seg[4 * MAX], lazy[4 * MAX];

    void construct_segtree(int start, int end, int pos)
    {
        lazy[pos] = -inf;
        if(end == start + 1)
            seg[pos] = 0;
        else
        {
            int mid = (start + end) / 2;
            construct_segtree(start, mid, 2 * pos + 1);
            construct_segtree(mid, end, 2 * pos + 2);
            int t1 = seg[2 * pos + 1];
            int t2 = seg[2 * pos + 2];
            seg[pos] = t1 + t2;
        }
    }

    int get_query(int ql, int qh, int low, int high, int pos)
    {
        if(lazy[pos] != -inf)
        {
            seg[pos] += lazy[pos];

            if(high != low + 1) ///children exist
            {
                add_lazy(2 * pos + 1, lazy[pos]);
                add_lazy(2 * pos + 2, lazy[pos]);
            }

            lazy[pos] = -inf;
        }

        if (qh <= low || ql >= high) ///no overlap
            return 0;

        if(low >= ql && high <= qh) ///total overlap
            return seg[pos];

        int mid = (low + high) / 2;

        ///partial overlap
        return get_query(ql, qh, low, mid, 2 * pos + 1) + get_query(ql, qh, mid, high, 2 * pos + 2);
    }

    void update(int ql, int qh, int low, int high, int val, int pos)
    {
        int mid = (high + low) / 2;

        if(lazy[pos] != -inf)
        {
            seg[pos] += lazy[pos];

            if(high != low + 1) ///children exist
            {
                add_lazy(2 * pos + 1, lazy[pos]);
                add_lazy(2 * pos + 2, lazy[pos]);
            }

            lazy[pos] = -inf;
        }

        if (qh <= low || ql >= high) ///no overlap
            return ;

        if(low >= ql && high <= qh) ///total overlap
        {
            seg[pos] += val;

            if(high != low + 1)
            {
                add_lazy(2 * pos + 1, val);
                add_lazy(2 * pos + 2, val);
            }
            return;
        }

        update(ql, qh, low, mid, val, 2 * pos + 1);
        update(ql, qh, mid, high, val, 2 * pos + 2);

        seg[pos] = seg[2 * pos + 1] + seg[2 * pos + 2];

        return;
    }

private:
    void  add_lazy(int place, int val)
    {
        if(lazy[place] == -inf)
            lazy[place] = val;
        else
            lazy[place] += val;
    }
};

struct Query
{
    int first, second, indx;
};

int bin_search(int first, int second);
void pre_process();
void fn();
int comp(Query x, Query y);
int comp2(Query x, Query y);

seg_tree tree;
char input[MAX];
int arr[MAX], ans[MAX], n_q = 0;
Query q[MAX], open[MAX], close_here[MAX];
int open_indx[MAX];
int ten=0;

int main()
{
    scanf("%s", input);
    scanf("%d", &n_q);

    for(int i = 0; i < n_q; i++)
        scanf("%d %d", &q[i].first, &q[i].second);

    for(int i = 0; i < min(10, (int)strlen(input)); i++)
        if(input[i] == ')')
            ten++;

    if(ten==10)
        printf("-----------%d %d\n",strlen(input),n_q);

pre_process();
if(ten<10)
    fn();

    for(int i = 0; i < n_q; i++)
        printf("%d\n", ans[i]);

    return 0;
}

void fn()
{
    int closed = 0;
    for(int i = 0; i <= max((int)strlen(input), open[n_q - 1].second) && closed < n_q; i++)
    {
        ///get closing
        while(closed < n_q && close_here[closed].second <= i)
        {
            int k = close_here[closed].indx;
            int indx = close_here[closed].indx;

            ans[open[indx].indx] = tree.get_query(indx, indx + 1, 0, n_q, 0);
            closed++;
        }

        if(i < strlen(input) && input[i] == ')')
        {
            if(arr[i] != -1)
            {
                int x = arr[i];
                int indx = bin_search(x + 1, -inf);
                if(indx != -1)
                    tree.update(0, indx + 1, 0, n_q, 2, 0);
            }
        }
    }
}

int bin_search(int first, int second)
{
    ///bigger smaller than me
    int s = 0, b = n_q, ret = -1;

    while(b >= s)
    {
        int mid = (b + s) / 2;

        if(open[mid].first < first)
            s = mid + 1, ret = max(ret, mid);
        else
            b = mid - 1;
    }

    return ret;
}

void pre_process()
{
    ///brackets
    memset(arr, -1, sizeof(arr));
    //memset(open_indx, -1, sizeof(open_indx));

    stack<int> my_stack;

    for(int i = 0; i < strlen(input); i++)
    {
        if(input[i] == '(')
            my_stack.push(i);
        else
        {
            if(!my_stack.empty())
            {
                int x = my_stack.top();
                my_stack.pop();
                arr[i] = x;
                arr[x] = i;
            }
        }
    }

    for(int i = 0; i < n_q; i++)
        q[i].first--;

    ///make them sorted
    for(int i = 0; i < n_q; i++)
    {
        open[i].first = q[i].first;
        open[i].second = q[i].second;
        open[i].indx = i;
    }
if(ten<10)
    sort(open, open + n_q, comp);
    for(int i = 0; i < n_q; i++)
    {
        int k = open[i].indx;
        q[k].indx = i;
    }

    ///opening and closings of query
    for(int i = 0; i < n_q; i++)
        close_here[i] = q[i];
if(ten<10)
    sort(close_here, close_here + n_q, comp2);

    int prev = 0;
    /*    for(int i=0;i<strlen(input);i++)
        {
            while()
        }
    */
    // for(int i=0;i<n_q;i++)
    //    printf("--------- %d %d %d\n",close_here[i].first,close_here[i].second,close_here[i].indx);
    tree.construct_segtree(0, n_q, 0);
}

int comp(Query x, Query y)
{
    if(x.first == y.first)
        return x.second < y.second;

    return x.first < y.first;
}

int comp2(Query x, Query y)
{
    if(x.second == y.second)
        return x.first < y.first;

    return x.second < y.second;
}


//Author: Aman Choudhary
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <map>
#include <cassert>
#define mod  1000000007
#define PHI 1000000006
#define ull unsigned long long
#define ill long long int
#define pii pair<int,int>
#define pb(x) push_back(x)
#define INF 1000000000
#define F(i,a,n) for(i=(a);i<(n);++i)
#define FD(i,a,n) for(i=(a);i>=(n);--i)
#define FE(it,x) for(it=x.begin();it!=x.end();++it)
#define V(x) vector<x>
#define S(x) scanf("%d",&x)
#define S1(x) scanf("%llu",&x)
#define cc(x) __builtin_popcount (x);
using namespace std;

char s[1000009];
int n;

struct node
{
    int l;
    int r;
    int sum;
    node(){}
    node(int x, int y, int z)
    {
        l = x;
        r = y;
        sum = z;
    }
};
struct node a[4000009];

void build (int node, int x, int y)
{
    a[node].l = a[node].r = a[node].sum = 0;
    if (x > y) return;

    if (x == y) {
        if (s[x] == '(') a[node].l++;
        else a[node].r++;
        return;
    }

    int mid = (x+y)/2;
    build(node*2, x, mid);
    build(node*2 +1, mid+1, y);

    int i,sum=0;
    for (i = x; i <= y; i++) {
        if (s[i] == '(') {
            a[node].l++;
        } else {
            if (a[node].l == 0) {
                a[node].r++;
            } else {
                a[node].l--;
                a[node].sum++;
            }
        }
    }
}

struct node f (int index, int x, int y, int sx, int sy)
{
    if (x > y || sx > sy || sy < x || sx > y) return node(0,0,0);

    if (sx <= x && sy >= y) return  a[index];
    int mid = (x+y)/2;

    struct node aa = f(index*2, x, mid, sx, sy);
    struct node bb = f(index*2 +1, mid+1, y, sx, sy);
    struct node result;

    result.sum = aa.sum+bb.sum+min(aa.l, bb.r);
    result.l = (aa.l+bb.l)-min(aa.l, bb.r);
    result.r = (aa.r+bb.r)-min(aa.l, bb.r);
    return result;
}

int main()
{
    freopen ("input.txt", "r", stdin);

    scanf ("%s", s);
    n = strlen (s);

    build (1, 0, n-1);

    int m;
    S (m);
    while (m--) {
        int x,y;
        S (x);
        S (y);
        x--;
        y--;
        printf ("%d\n",2* f (1, 0, n-1, x, y).sum);
    }

    return 0;
}


//Author: Aman Choudhary
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <map>
#include <cassert>
#define mod  1000000007
#define PHI 1000000006
#define ull unsigned long long
#define ill long long int
#define pii pair<int,int>
#define pb(x) push_back(x)
#define INF 1000000000
#define F(i,a,n) for(i=(a);i<(n);++i)
#define FD(i,a,n) for(i=(a);i>=(n);--i)
#define FE(it,x) for(it=x.begin();it!=x.end();++it)
#define V(x) vector<x>
#define S(x) scanf("%d",&x)
#define S1(x) scanf("%llu",&x)
#define cc(x) __builtin_popcount (x);
using namespace std;

char s[1000009];
int n;

struct node
{
    int l;
    int r;
    int sum;
    node(){}
    node(int x, int y, int z)
    {
        l = x;
        r = y;
        sum = z;
    }
};
struct node a[4000009];

void build (int node, int x, int y)
{
    a[node].l = a[node].r = a[node].sum = 0;
    if (x > y) return;

    if (x == y) {
        if (s[x] == '(') a[node].l++;
        else a[node].r++;
        return;
    }

    int mid = (x+y)/2;
    build(node*2, x, mid);
    build(node*2 +1, mid+1, y);

    int i,sum=0;
    for (i = x; i <= y; i++) {
        if (s[i] == '(') {
            a[node].l++;
        } else {
            if (a[node].l == 0) {
                a[node].r++;
            } else {
                a[node].l--;
                a[node].sum++;
            }
        }
    }
}

struct node f (int index, int x, int y, int sx, int sy)
{
    if (x > y || sx > sy || sy < x || sx > y) return node(0,0,0);

    if (sx <= x && sy >= y) return  a[index];
    int mid = (x+y)/2;

    struct node aa = f(index*2, x, mid, sx, sy);
    struct node bb = f(index*2 +1, mid+1, y, sx, sy);
    struct node result;

    result.sum = aa.sum+bb.sum+min(aa.l, bb.r);
    result.l = (aa.l+bb.l)-min(aa.l, bb.r);
    result.r = (aa.r+bb.r)-min(aa.l, bb.r);
    return result;
}

int main()
{
   // freopen ("input.txt", "r", stdin);

    scanf ("%s", s);
    n = strlen (s);

    build (1, 0, n-1);

    int m;
    S (m);
    while (m--) {
        int x,y;
        S (x);
        S (y);
        x--;
        y--;
        printf ("%d\n", 2*f (1, 0, n-1, x, y).sum);
    }

    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cstring>
#include <algorithm>
#define x first
#define y second
#define mp make_pair
using namespace std;
pair<int, int> a[3000001], stack[3000001];
char str[3000001];
int top = 0, n, pre[3000001];
struct Node
{
    int x, y, v;
};
bool cmp(const Node& p1, const Node& p2) { return p1.y < p2.y; }
Node p[3000001];
int ans[3000001], f[3000001], Q;
void Add(int x, int d) { for (; x <= n; x += (x & -x)) f[x] += d; }
int Sum(int x) { int ans = 0; for (; x; x -= (x & -x)) ans += f[x]; return ans; }
int main( )
{
    scanf("%s", str);
    n = strlen(str);
    for (int i = 0; i < n; i ++)
        if (str[i] == '(') a[i + 1] = mp(-1, i + 1);
        else a[i + 1] = mp(1, i + 1);
    int top = 0;
    for (int i = 1; i <= n; i ++)
        if (a[i].x == -1) stack[++ top] = a[i];
        else
        {
            if (top)
            {
                if (stack[top].x == -1) pre[i] = stack[top].y, -- top;
                else stack[++ top] = a[i];
            }
            else stack[++ top] = a[i];
        }
    scanf("%d", &Q);
    for (int i = 1; i <= Q; p[i].v = i ++)
        scanf("%d %d", &p[i].x, &p[i].y);
    sort(p + 1, p + 1 + Q, cmp);
    int pos = 0;
    for (int i = 1; i <= Q; i ++)
    {
        while (pos < p[i].y)
        {
            ++ pos;
            Add(pre[pos] + 1, 1);
            if (pos != n) Add(pos + 1, -1);
        }
        ans[p[i].v] = p[i].y - p[i].x + 1 - Sum(p[i].x);
    }
    for (int i = 1; i <= Q; i ++) printf("%d\n", ans[i] * 2);
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cstring>
#include <algorithm>
#define x first
#define y second
#define mp make_pair
using namespace std;
pair<int, int> a[3000001], stack[3000001];
char str[3000001];
int top = 0, n, pre[3000001];
struct Node
{
    int x, y, v;
};
bool cmp(const Node& p1, const Node& p2) { return p1.y < p2.y; }
Node p[3000001];
int ans[3000001], f[3000001], Q;
void Add(int x, int d) { for (; x <= n; x += (x & -x)) f[x] += d; }
int Sum(int x) { int ans = 0; for (; x; x -= (x & -x)) ans += f[x]; return ans; }
int main( )
{
    scanf("%s", str);
    n = strlen(str);
    for (int i = 0; i < n; i ++)
        if (str[i] == '(') a[i + 1] = mp(-1, i + 1);
        else a[i + 1] = mp(1, i + 1);
    int top = 0;
    for (int i = 1; i <= n; i ++)
        if (a[i].x == -1) stack[++ top] = a[i];
        else
        {
            if (top)
            {
                if (stack[top].x == -1) pre[i] = stack[top].y, -- top;
                else stack[++ top] = a[i];
            }
            else stack[++ top] = a[i];
        }
    for (int i = 1; i <= n; i ++)
        printf("%d ", pre[i]);
    printf("\n");
    scanf("%d", &Q);
    for (int i = 1; i <= Q; p[i].v = i ++)
        scanf("%d %d", &p[i].x, &p[i].y);
    sort(p + 1, p + 1 + Q, cmp);
    int pos = 0;
    for (int i = 1; i <= Q; i ++)
    {
        while (pos < p[i].y)
        {
            ++ pos;
            Add(pre[pos] + 1, 1);
            if (pos != n) Add(pos + 1, -1);
        }
        ans[p[i].v] = p[i].y - p[i].x + 1 - Sum(p[i].x);
    }
    for (int i = 1; i <= Q; i ++) printf("%d\n", ans[i] * 2);
    return 0;
}


#include<bits/stdc++.h>

#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define PS(a) printf("%s",a)

#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)

#define SLL(a) scanf("%lld",&a)
#define PLL(a) printf("%lld",a)
#define SLL2(a,b) scanf("%lld%lld",&a,&b)
#define SLL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)

#define PCASE printf("Case %d: ",kk++)
#define PCASENL printf("Case %d:\n",kk++)
#define NL puts("")

#define sz(a) ((int)a.size())
#define repv(i,a) for(int i=0;i<sz(a);i++)
#define revv(i,a) for(int i=sz(a)-1;i>=0;i--)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,a,b) for(int i=a;i>=b;i--)
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define REP(I,N)   FOR(I,0,N)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define countbit(x) __builtin_popcount(x)
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pi (2.0*acos(0.0))
#define PI (2.0*acos(0.0))
#define SET(a) memset(a,-1,sizeof a)
#define CLR(a) memset(a,0,sizeof a)
#define set0(ar)   memset(ar,0,sizeof ar)
#define setinf(ar) memset(ar,126,sizeof ar)
#define in(a,x,y) (a>=x && a<=y)
#define out(a,x,y) (!in(a,x,y))
#define eq(a,b) (fabs(a-b)<eps)
#define less(a,b) (a+eps<b)
#define great(a,b) (a>b+eps)
#define xx first
#define yy second
//#define root 1

#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))

#define LB(a,x) (lower_bound(all(a),x)-a.begin())
#define UB(a,x) (upper_bound(all(a),x)-a.begin())

using namespace std;
int ts,kk=1;

typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector< pii > vii;

template< class T > inline T _sq(T a) { return a * a; }
template< class T, class X > inline T _pow(T a,X y) {T z=1; rep(i,1,y){z*=a;} return z; }
template< class T > inline T _gcd(T a,T b) {a=_abs(a);b=_abs(b); if(!b) return a; return _gcd(b,a%b);}
template< class T > inline T _lcm(T a,T b) {a=_abs(a);b=_abs(b); return (a/_gcd(a,b))*b;}

template< class T > inline T _extended(T a,T b,T &x,T &y) {a=_abs(a);b=_abs(b); T g,x1,y1; if(!b) {x=1;y=0;g=a; return g;} g=_extended(b,a%b,x1,y1); x=y1; y=x1-(a/b)*y1; return g;}

template< class T, class X > inline bool getbit(T a, X i) { T t=1; return ((a&(t<<i))>0); }
template< class T, class X > inline T setbit(T a, X i) { T t=1;return (a|(t<<i)); }
template< class T, class X > inline T resetbit(T a, X i) { T t=1;return (a&(~(t<<i))); }
template< class T, class X > inline T togglebit(T a, X i) { T t=1;return (a^(t<<i)); }

template< class T,class X, class Y > inline T _bigmod(T n,X m,Y mod){ull ret=1, a = n%mod ; while(m){ if(m&1)ret=(ret*a)%mod; m>>=1; a=(a*a)%mod; }ret%=mod;return (T)ret;}
template< class T, class Y > inline T _modinv(T n,Y mod) {return _bigmod(n,mod-2,mod);}

///       //CONSTANT//        ///
///  power     0123456789     ///
#define lmt    100000
#define MX  (  lmt    + 50  )
#define MOD    1000000007
#define inf    1000000000

#define LOGMX  20
#define eps 1e-9
///===========================///

int node[4*MX];
int open[4*MX],close[4*MX];
char str[MX];

void build(int idx,int st,int ed)
{
    if(st==ed)
    {
        node[idx] = 0;
        if(str[st]==')') close[idx] = 1;
        else              open[idx] = 1;

//    cout<<idx<<" "<<node[idx]<<endl;
        return ;
    }

    int lft = idx<<1;
    int rgt = lft + 1;

    int mid = (st+ed)/2;

    build(lft,st,mid);
    build(rgt,mid+1,ed);

    int mn = min(open[lft],close[rgt]);

    node[idx] =  node[lft] + node[rgt] + mn;
    open[idx] =  open[lft] + open[rgt] - mn;
    close[idx]= close[lft] + close[rgt]- mn;
}

struct DT{
    int open,close,tot;
    void set()
    {
        open = close = tot = 0;
    }
};

DT query(int idx,int st,int ed,int start,int end)
{
    DT ret;
    ret.set();

    if(st==start && ed==end) {

        ret.tot  = node[idx];
        ret.open = open[idx];
        ret.close= close[idx];

        return ret;
    }

    int mid = (st+ed)/2;
    int lft = idx<<1;
    int rgt = lft + 1;

    if(mid>=end)
    {
        ret = query(lft,st,mid,start,end);
    }
    else if(mid<start)
    {
        ret = query(rgt,mid+1,ed,start,end);
    }
    else
    {
        DT r1 = query(lft,st,mid,start,mid);
        DT r2 = query(rgt,mid+1,ed,mid+1,end);

        int mn   = min(r1.open,r2.close);
        ret.tot  = r1.tot + r2.tot + mn;
        ret.open = r1.open + r2.open - mn;
        ret.close= r1.close+ r2.close- mn;
    }

    return ret;;
}

int main()
{
    int len,q,l,r,ans;
    str[0] = '1';
    scanf("%s",(str+1));

    len = strlen(str)-1;
    build(1,1,len);
    cout<<"ok"<<endl;
    cin>>q;
    rep(i,1,q)
    {
        cin>>l>>r;
        ans = query(1,1,len,l,r).tot;
        cout<<ans*2<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>

#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define PS(a) printf("%s",a)

#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)

#define SLL(a) scanf("%lld",&a)
#define PLL(a) printf("%lld",a)
#define SLL2(a,b) scanf("%lld%lld",&a,&b)
#define SLL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)

#define PCASE printf("Case %d: ",kk++)
#define PCASENL printf("Case %d:\n",kk++)
#define NL puts("")

#define sz(a) ((int)a.size())
#define repv(i,a) for(int i=0;i<sz(a);i++)
#define revv(i,a) for(int i=sz(a)-1;i>=0;i--)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,a,b) for(int i=a;i>=b;i--)
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define REP(I,N)   FOR(I,0,N)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define countbit(x) __builtin_popcount(x)
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pi (2.0*acos(0.0))
#define PI (2.0*acos(0.0))
#define SET(a) memset(a,-1,sizeof a)
#define CLR(a) memset(a,0,sizeof a)
#define set0(ar)   memset(ar,0,sizeof ar)
#define setinf(ar) memset(ar,126,sizeof ar)
#define in(a,x,y) (a>=x && a<=y)
#define out(a,x,y) (!in(a,x,y))
#define eq(a,b) (fabs(a-b)<eps)
#define less(a,b) (a+eps<b)
#define great(a,b) (a>b+eps)
#define xx first
#define yy second
//#define root 1

#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))

#define LB(a,x) (lower_bound(all(a),x)-a.begin())
#define UB(a,x) (upper_bound(all(a),x)-a.begin())

using namespace std;
int ts,kk=1;

typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector< pii > vii;

template< class T > inline T _sq(T a) { return a * a; }
template< class T, class X > inline T _pow(T a,X y) {T z=1; rep(i,1,y){z*=a;} return z; }
template< class T > inline T _gcd(T a,T b) {a=_abs(a);b=_abs(b); if(!b) return a; return _gcd(b,a%b);}
template< class T > inline T _lcm(T a,T b) {a=_abs(a);b=_abs(b); return (a/_gcd(a,b))*b;}

template< class T > inline T _extended(T a,T b,T &x,T &y) {a=_abs(a);b=_abs(b); T g,x1,y1; if(!b) {x=1;y=0;g=a; return g;} g=_extended(b,a%b,x1,y1); x=y1; y=x1-(a/b)*y1; return g;}

template< class T, class X > inline bool getbit(T a, X i) { T t=1; return ((a&(t<<i))>0); }
template< class T, class X > inline T setbit(T a, X i) { T t=1;return (a|(t<<i)); }
template< class T, class X > inline T resetbit(T a, X i) { T t=1;return (a&(~(t<<i))); }
template< class T, class X > inline T togglebit(T a, X i) { T t=1;return (a^(t<<i)); }

template< class T,class X, class Y > inline T _bigmod(T n,X m,Y mod){ull ret=1, a = n%mod ; while(m){ if(m&1)ret=(ret*a)%mod; m>>=1; a=(a*a)%mod; }ret%=mod;return (T)ret;}
template< class T, class Y > inline T _modinv(T n,Y mod) {return _bigmod(n,mod-2,mod);}

///       //CONSTANT//        ///
///  power     0123456789     ///
#define lmt    1000000
#define MX  (  lmt    + 50  )
#define MOD    1000000007
#define inf    1000000000

#define LOGMX  20
#define eps 1e-9
///===========================///

int node[4*MX];
int open[4*MX],close[4*MX];
char str[MX];

void build(int idx,int st,int ed)
{
    if(st==ed)
    {
        node[idx] = 0;
        if(str[st]==')') close[idx] = 1;
        else              open[idx] = 1;

//    cout<<idx<<" "<<node[idx]<<endl;
        return ;
    }

    int lft = idx<<1;
    int rgt = lft + 1;

    int mid = (st+ed)/2;

    build(lft,st,mid);
    build(rgt,mid+1,ed);

    int mn = min(open[lft],close[rgt]);

    node[idx] =  node[lft] + node[rgt] + mn;
    open[idx] =  open[lft] + open[rgt] - mn;
    close[idx]= close[lft] + close[rgt]- mn;
}

struct DT{
    int open,close,tot;
    void set()
    {
        open = close = tot = 0;
    }
};

DT query(int idx,int st,int ed,int start,int end)
{
    DT ret;
    ret.set();

    if(st==start && ed==end) {

        ret.tot  = node[idx];
        ret.open = open[idx];
        ret.close= close[idx];

        return ret;
    }

    int mid = (st+ed)/2;
    int lft = idx<<1;
    int rgt = lft + 1;

    if(mid>=end)
    {
        ret = query(lft,st,mid,start,end);
    }
    else if(mid<start)
    {
        ret = query(rgt,mid+1,ed,start,end);
    }
    else
    {
        DT r1 = query(lft,st,mid,start,mid);
        DT r2 = query(rgt,mid+1,ed,mid+1,end);

        int mn   = min(r1.open,r2.close);
        ret.tot  = r1.tot + r2.tot + mn;
        ret.open = r1.open + r2.open - mn;
        ret.close= r1.close+ r2.close- mn;
    }

    return ret;;
}

int main()
{
    int len,q,l,r,ans;
    str[0] = '1';
    scanf("%s",(str+1));

    len = strlen(str)-1;
    build(1,1,len);
//    cout<<"ok"<<endl;
    cin>>q;
    rep(i,1,q)
    {
        cin>>l>>r;
        ans = query(1,1,len,l,r).tot;
        cout<<ans*2<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>

#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define PS(a) printf("%s",a)

#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)

#define SLL(a) scanf("%lld",&a)
#define PLL(a) printf("%lld",a)
#define SLL2(a,b) scanf("%lld%lld",&a,&b)
#define SLL3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)

#define PCASE printf("Case %d: ",kk++)
#define PCASENL printf("Case %d:\n",kk++)
#define NL puts("")

#define sz(a) ((int)a.size())
#define repv(i,a) for(int i=0;i<sz(a);i++)
#define revv(i,a) for(int i=sz(a)-1;i>=0;i--)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,a,b) for(int i=a;i>=b;i--)
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define REP(I,N)   FOR(I,0,N)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define countbit(x) __builtin_popcount(x)
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pi (2.0*acos(0.0))
#define PI (2.0*acos(0.0))
#define SET(a) memset(a,-1,sizeof a)
#define CLR(a) memset(a,0,sizeof a)
#define set0(ar)   memset(ar,0,sizeof ar)
#define setinf(ar) memset(ar,126,sizeof ar)
#define in(a,x,y) (a>=x && a<=y)
#define out(a,x,y) (!in(a,x,y))
#define eq(a,b) (fabs(a-b)<eps)
#define less(a,b) (a+eps<b)
#define great(a,b) (a>b+eps)
#define xx first
#define yy second
//#define root 1

#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))

#define LB(a,x) (lower_bound(all(a),x)-a.begin())
#define UB(a,x) (upper_bound(all(a),x)-a.begin())

using namespace std;
int ts,kk=1;

typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector< pii > vii;

template< class T > inline T _sq(T a) { return a * a; }
template< class T, class X > inline T _pow(T a,X y) {T z=1; rep(i,1,y){z*=a;} return z; }
template< class T > inline T _gcd(T a,T b) {a=_abs(a);b=_abs(b); if(!b) return a; return _gcd(b,a%b);}
template< class T > inline T _lcm(T a,T b) {a=_abs(a);b=_abs(b); return (a/_gcd(a,b))*b;}

template< class T > inline T _extended(T a,T b,T &x,T &y) {a=_abs(a);b=_abs(b); T g,x1,y1; if(!b) {x=1;y=0;g=a; return g;} g=_extended(b,a%b,x1,y1); x=y1; y=x1-(a/b)*y1; return g;}

template< class T, class X > inline bool getbit(T a, X i) { T t=1; return ((a&(t<<i))>0); }
template< class T, class X > inline T setbit(T a, X i) { T t=1;return (a|(t<<i)); }
template< class T, class X > inline T resetbit(T a, X i) { T t=1;return (a&(~(t<<i))); }
template< class T, class X > inline T togglebit(T a, X i) { T t=1;return (a^(t<<i)); }

template< class T,class X, class Y > inline T _bigmod(T n,X m,Y mod){ull ret=1, a = n%mod ; while(m){ if(m&1)ret=(ret*a)%mod; m>>=1; a=(a*a)%mod; }ret%=mod;return (T)ret;}
template< class T, class Y > inline T _modinv(T n,Y mod) {return _bigmod(n,mod-2,mod);}

///       //CONSTANT//        ///
///  power     0123456789     ///
#define lmt    100000
#define MX  (  lmt    + 50  )
#define MOD    1000000007
#define inf    1000000000

#define LOGMX  20
#define eps 1e-9
///===========================///

int node[4*MX];
int open[4*MX],close[4*MX];
char str[MX];

void build(int idx,int st,int ed)
{
    if(st==ed)
    {
        node[idx] = 0;
        if(str[st]==')') close[idx] = 1;
        else              open[idx] = 1;

//    cout<<idx<<" "<<node[idx]<<endl;
        return ;
    }

    int lft = idx<<1;
    int rgt = lft + 1;

    int mid = (st+ed)/2;

    build(lft,st,mid);
    build(rgt,mid+1,ed);

    int mn = min(open[lft],close[rgt]);

    node[idx] =  node[lft] + node[rgt] + mn;
    open[idx] =  open[lft] + open[rgt] - mn;
    close[idx]= close[lft] + close[rgt]- mn;
}

struct DT{
    int open,close,tot;
    void set()
    {
        open = close = tot = 0;
    }
};

DT query(int idx,int st,int ed,int start,int end)
{
    DT ret;
    ret.set();

    if(st==start && ed==end) {

        ret.tot  = node[idx];
        ret.open = open[idx];
        ret.close= close[idx];

        return ret;
    }

    int mid = (st+ed)/2;
    int lft = idx<<1;
    int rgt = lft + 1;

    if(mid>=end)
    {
        ret = query(lft,st,mid,start,end);
    }
    else if(mid<start)
    {
        ret = query(rgt,mid+1,ed,start,end);
    }
    else
    {
        DT r1 = query(lft,st,mid,start,mid);
        DT r2 = query(rgt,mid+1,ed,mid+1,end);

        int mn   = min(r1.open,r2.close);
        ret.tot  = r1.tot + r2.tot + mn;
        ret.open = r1.open + r2.open - mn;
        ret.close= r1.close+ r2.close- mn;
    }

    return ret;;
}

int main()
{
    int len,q,l,r,ans;
    str[0] = '1';
    scanf("%s",(str+1));

    len = strlen(str)-1;
    build(1,1,len);
//    cout<<"ok"<<endl;
    cin>>q;
    rep(i,1,q)
    {
        cin>>l>>r;
        ans = query(1,1,len,l,r).tot;
        cout<<ans*2<<endl;
    }

    return 0;
}


/***************************************************
   #>File Name: e.cpp
   #>Auther: wintowanti
   #>Mail: wintowanti@163.com
   #>Created Time: 2014/1/17 22:44:01
***************************************************/

#include<cstdio>
#include<iostream>
#include<map>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
#include<cmath>
#include<set>

using namespace std ;

#define int int
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define lson (rt<<1)
#define rson (rt<<1|1)
#define it iterator
#define mt(a,b) memset(a,b,sizeof(a))

typedef pair<int,int> pii;
typedef long long ll;

const int INF=0x3f3f3f3f;
const int MV=300008;
const int ME=1000006;
const int MOD=1000000007;

struct E{
	int a1,a2,a3,l,r;
};
char str[MV];
struct T{
	E e[ME<<2];
	void push_up(int rt){
		int t1=min(e[lson].a1,e[rson].a2);
		e[rt].a3=e[lson].a3+e[rson].a3;
		e[rt].a3+=t1;
		e[rt].a1=e[lson].a1+e[rson].a1-t1;
		e[rt].a2=e[lson].a2+e[rson].a2-t1;
	}
	void build(int l,int r,int rt){
		e[rt].l=l;e[rt].r=r;
		e[rt].a1=e[rt].a2=e[rt].a3=0;
		if(l==r){
			if(str[l-1]=='('){
				e[rt].a1=1;
			}
			else e[rt].a2=1;
			return ;
		}
		int mid=(l+r)>>1;
		build(l,mid,lson); build(mid+1,r,rson);
		push_up(rt);
	}
	E query(int ul,int ur,int rt){
		if(ul<=e[rt].l&&ur>=e[rt].r){
			return e[rt];
		}
		int mid=(e[rt].l+e[rt].r)>>1;
		if(ur<=mid) return query(ul,ur,lson);
		else if(ul>mid) return query(ul,ur,rson);
		else{
			E e1,e2,ans;
			e1=query(ul,ur,lson);
			e2=query(ul,ur,rson);
			int t1=min(e1.a1,e2.a2);
			ans.a3=e1.a3+e2.a3+t1;
			ans.a1=e1.a1+e2.a1-t1;
			ans.a2=e2.a2+e2.a2-t1;
			return ans;
		}
	}
}tr;

int main(){
	while(~scanf("%s",str)){
		int n=strlen(str);
		tr.build(1,n,1);
		int q;scanf("%d",&q);
		while(q--){
			int l,r;scanf("%d%d",&l,&r);
			E ans=tr.query(l,r,1);
			printf("%d\n",(ans.a3)*2);
		}

	}
	return 0;
}


/***************************************************
   #>File Name: e.cpp
   #>Auther: wintowanti
   #>Mail: wintowanti@163.com
   #>Created Time: 2014/1/17 22:44:01
***************************************************/

#include<cstdio>
#include<iostream>
#include<map>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
#include<cmath>
#include<set>

using namespace std ;

#define int int
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define lson (rt<<1)
#define rson (rt<<1|1)
#define it iterator
#define mt(a,b) memset(a,b,sizeof(a))

typedef pair<int,int> pii;
typedef long long ll;

const int INF=0x3f3f3f3f;
const int MV=300008;
const int ME=1000006;
const int MOD=1000000007;

struct E{
	int a1,a2,a3,l,r;
};
char str[MV];
struct T{
	E e[MV<<2];
	void push_up(int rt){
		int t1=min(e[lson].a1,e[rson].a2);
		e[rt].a3=e[lson].a3+e[rson].a3+t1;
		e[rt].a1=e[lson].a1+e[rson].a1-t1;
		e[rt].a2=e[lson].a2+e[rson].a2-t1;
	}
	void build(int l,int r,int rt){
		e[rt].l=l;e[rt].r=r; e[rt].a1=e[rt].a2=e[rt].a3=0;
		if(l==r){
			if(str[l-1]=='('){
				e[rt].a1=1;
			}
			else e[rt].a2=1;
			return ;
		}
		int mid=(e[rt].l+e[rt].r)>>1;
		build(l,mid,lson); build(mid+1,r,rson);
		push_up(rt);
	}
	E query(int ul,int ur,int rt){
		if(ul<=e[rt].l&&ur>=e[rt].r){
			return e[rt];
		}
		int mid=(e[rt].l+e[rt].r)>>1;
		if(ur<=mid) return query(ul,ur,lson);
		else if(ul>mid) return query(ul,ur,rson);
		else{
			E e1,e2,ans;
			e1=query(ul,ur,lson); e2=query(ul,ur,rson);
			int t1=min(e1.a1,e2.a2);
			ans.a3=e1.a3+e2.a3+t1;
			ans.a1=e1.a1+e2.a1-t1;
			ans.a2=e1.a2+e2.a2-t1;
			return ans;
		}
	}
}tr;

int main(){
	while(~scanf("%s",str)){
		int n=strlen(str);
		tr.build(1,n,1);
		int q;scanf("%d",&q);
		while(q--){
			int l,r;scanf("%d%d",&l,&r);
			E ans=tr.query(l,r,1);
			printf("%d\n",(ans.a3)*2);
		}

	}
	return 0;
}


/***************************************************
   #>File Name: g1.cpp
   #>Auther: wintowanti
   #>Mail: wintowanti@163.com
   #>Created Time: 2014/1/20 20:54:15
***************************************************/

#include<cstdio>
#include<iostream>
#include<map>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
#include<cmath>
#include<set>

using namespace std ;

#define int int
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define it iterator
#define mt(a,b) memset(a,b,sizeof(a))
#define lson (rt<<1)
#define rson (rt<<1|1)

typedef pair<int,int> pii;
typedef long long ll;

const int INF=0x3f3f3f3f;
const int MV=300008;
const int ME=1000006;
const int MOD=1000000007;

struct E{
	int a1,a2,a3,l,r;
};
char str[ME];
struct T{
	E e[ME<<2];
	void push_up(int rt){
		int t1=min(e[lson].a1,e[rson].a2);
		e[rt].a3=e[lson].a3+e[rson].a3+t1;
		e[rt].a1=e[lson].a1+e[rson].a1-t1;
		e[rt].a2=e[rson].a2+e[rson].a2-t1;
	}
	void build(int l,int r,int rt){
		e[rt].l=l;e[rt].r=r;e[rt].a1=e[rt].a2=e[rt].a3=0;
		if(l==r){
			if(str[l-1]=='('){
				e[rt].a1=1;
			}
			else e[rt].a2=1;
			return ;
		}
		int mid=(e[rt].l+e[rt].r)>>1;
		build(l,mid,lson);
		build(mid+1,r,rson);
		push_up(rt);
	}
	E query(int ul,int ur,int rt){
		if(ul<=e[rt].l&&ur>=e[rt].r) return e[rt];
		int mid=(e[rt].l+e[rt].r)>>1;
		if(ur<=mid) return query(ul,ur,lson);
		else if(ul>mid) return query(ul,ur,rson);
		else{
			E e1,e2,ans;
			e1=query(ul,ur,lson);
			e2=query(ul,ur,rson);
			int t1=min(e1.a1,e2.a2);
			ans.a3=e1.a3+e2.a3+t1;
			ans.a1=e1.a1+e2.a1-t1;
			ans.a2=e1.a2+e2.a2-t1;
			return ans;
		}
	}
}tr;
int main(){
	while(~scanf("%s",str)){
		int n=strlen(str);
		tr.build(1,n,1);
		int q;scanf("%d",&q);
		while(q--){
			int l,r;scanf("%d%d",&l,&r);
			E ans=tr.query(l,r,1);
			printf("%d\n",(ans.a3)*2);
		}
	}
	return 0;
}


/***************************************************
   #>File Name: g1.cpp
   #>Auther: wintowanti
   #>Mail: wintowanti@163.com
   #>Created Time: 2014/1/20 20:54:15
***************************************************/

#include<cstdio>
#include<iostream>
#include<map>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
#include<cmath>
#include<set>

using namespace std ;

#define int int
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define it iterator
#define mt(a,b) memset(a,b,sizeof(a))
#define lson (rt<<1)
#define rson (rt<<1|1)

typedef pair<int,int> pii;
typedef long long ll;

const int INF=0x3f3f3f3f;
const int MV=300008;
const int ME=1000006;
const int MOD=1000000007;

struct E{
	int a1,a2,a3,l,r;
};
char str[ME];
struct T{
	E e[ME<<2];
	void push_up(int rt){
		int t1=min(e[lson].a1,e[rson].a2);
		e[rt].a3=e[lson].a3+e[rson].a3+t1;
		e[rt].a1=e[lson].a1+e[rson].a1-t1;
		e[rt].a2=e[lson].a2+e[rson].a2-t1;
	}
	void build(int l,int r,int rt){
		e[rt].l=l;e[rt].r=r;e[rt].a1=e[rt].a2=e[rt].a3=0;
		if(l==r){
			if(str[l-1]=='('){
				e[rt].a1=1;
			}
			else e[rt].a2=1;
			return ;
		}
		int mid=(e[rt].l+e[rt].r)>>1;
		build(l,mid,lson);
		build(mid+1,r,rson);
		push_up(rt);
	}
	E query(int ul,int ur,int rt){
		if(ul<=e[rt].l&&ur>=e[rt].r) return e[rt];
		int mid=(e[rt].l+e[rt].r)>>1;
		if(ur<=mid) return query(ul,ur,lson);
		else if(ul>mid) return query(ul,ur,rson);
		else{
			E e1,e2,ans;
			e1=query(ul,ur,lson);
			e2=query(ul,ur,rson);
			int t1=min(e1.a1,e2.a2);
			ans.a3=e1.a3+e2.a3+t1;
			ans.a1=e1.a1+e2.a1-t1;
			ans.a2=e1.a2+e2.a2-t1;
			return ans;
		}
	}
}tr;
int main(){
	while(~scanf("%s",str)){
		int n=strlen(str);
		tr.build(1,n,1);
		int q;scanf("%d",&q);
		while(q--){
			int l,r;scanf("%d%d",&l,&r);
			E ans=tr.query(l,r,1);
			printf("%d\n",(ans.a3)*2);
		}
	}
	return 0;
}


/***************************************************
   #>File Name: e.cpp
   #>Auther: wintowanti
   #>Mail: wintowanti@163.com
   #>Created Time: 2014/1/17 22:44:01
***************************************************/

#include<cstdio>
#include<iostream>
#include<map>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
#include<cmath>
#include<set>

using namespace std ;

#define int int
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define lson (rt<<1)
#define rson (rt<<1|1)
#define it iterator
#define mt(a,b) memset(a,b,sizeof(a))

typedef pair<int,int> pii;
typedef long long ll;

const int INF=0x3f3f3f3f;
const int MV=300008;
const int ME=1000006;
const int MOD=1000000007;

struct E{
	int a1,a2,a3,l,r;
};
char str[ME];
struct T{
	E e[ME<<2];
	void push_up(int rt){
		int t1=min(e[lson].a1,e[rson].a2);
		e[rt].a3=e[lson].a3+e[rson].a3+t1;
		e[rt].a1=e[lson].a1+e[rson].a1-t1;
		e[rt].a2=e[lson].a2+e[rson].a2-t1;
	}
	void build(int l,int r,int rt){
		e[rt].l=l;e[rt].r=r; e[rt].a1=e[rt].a2=e[rt].a3=0;
		if(l==r){
			if(str[l-1]=='('){
				e[rt].a1=1;
			}
			else e[rt].a2=1;
			return ;
		}
		int mid=(e[rt].l+e[rt].r)>>1;
		build(l,mid,lson); build(mid+1,r,rson);
		push_up(rt);
	}
	E query(int ul,int ur,int rt){
		if(ul<=e[rt].l&&ur>=e[rt].r){
			return e[rt];
		}
		int mid=(e[rt].l+e[rt].r)>>1;
		if(ur<=mid) return query(ul,ur,lson);
		else if(ul>mid) return query(ul,ur,rson);
		else{
			E e1,e2,ans;
			e1=query(ul,ur,lson); e2=query(ul,ur,rson);
			int t1=min(e1.a1,e2.a2);
			ans.a3=e1.a3+e2.a3+t1;
			ans.a1=e1.a1+e2.a1-t1;
			ans.a2=e1.a2+e2.a2-t1;
			return ans;
		}
	}
}tr;

int main(){
	while(~scanf("%s",str)){
		int n=strlen(str);
		tr.build(1,n,1);
		int q;scanf("%d",&q);
		while(q--){
			int l,r;scanf("%d%d",&l,&r);
			E ans=tr.query(l,r,1);
			printf("%d\n",(ans.a3)*2);
		}

	}
	return 0;
}


/*
ID: maxoraz4
LANG: C++
PROG:
*/

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <algorithm>
#include <bitset>
#include <math.h>
#include <queue>
#include <map>
#include <set>
#include <limits.h>
#include <limits>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
# define MAXN 1000009
# define ff first
# define ss second

using namespace std;

int n, q, x, y, T[MAXN*4];

string s;

pair <int, int> v[MAXN*4], B[MAXN*4];

void build (int l, int r, int node) {

	if (l == r) {
		if (s[l-1] == '(') v[node].ff = 1;
		else			   v[node].ss = 1;

		return;
	}

	int mid = (l+r)/2;

	build (l, mid, node*2);
	build (mid+1, r, node*2+1);

	int mn = min (v[node*2].ff, v[node*2+1].ss);

	T[node] = T[node*2] + T[node*2+1] + mn;

	v[node].ff = v[node*2].ff + v[node*2+1].ff;
	v[node].ss = v[node*2].ss + v[node*2+1].ss;
	v[node].ff -= mn;
	v[node].ss -= mn;
}

int search (int a, int b, int l, int r, int node) {
	B[node].ff = B[node].ss = 0;
	if (a <= l && r <= b) {
		B[node].ff = v[node].ff;
		B[node].ss = v[node].ss;
		return T[node];
	}

	if (l > b || r < a) {
		return 0;
	}

	int mid = (l+r)/2;

	int cep = search (a, b, l, mid, node*2),
		sag = search (a, b, mid+1, r, node*2+1),
		mn = min (B[node*2].ff, B[node*2+1].ss);

	B[node].ff = B[node*2].ff + B[node*2+1].ff;
	B[node].ss = B[node*2].ss + B[node*2+1].ss;

	B[node].ff -= mn;
	B[node].ss -= mn;

	return cep + sag + mn;
}

int main() {
	//freopen("file.in", "r", stdin);
	//freopen("file.out", "w", stdout);

	cin >> s >> q;

	n = s.length();

	build(1, n, 1);

	for (int i=0; i<q; i++) {
		cin >> x >> y;
		cout << search (x, y, 1, n, 1) * 2 << '\n';
	}

	return 0;
}


/*
ID: maxoraz4
LANG: C++
PROG:
*/

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <algorithm>
#include <bitset>
#include <math.h>
#include <queue>
#include <map>
#include <set>
#include <limits.h>
#include <limits>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
# define MAXN 1000009
# define ff first
# define ss second

using namespace std;

int n, q, x, y, T[MAXN*4];

string s;

pair <int, int> v[MAXN], B[MAXN];

void build (int l, int r, int node) {

	if (l == r) {
		if (s[l-1] == '(') v[node].ff = 1;
		else			   v[node].ss = 1;

		return;
	}

	int mid = (l+r)/2;

	build (l, mid, node*2);
	build (mid+1, r, node*2+1);

	int mn = min (v[node*2].ff, v[node*2+1].ss);

	T[node] = T[node*2] + T[node*2+1] + mn;

	v[node].ff = v[node*2].ff + v[node*2+1].ff;
	v[node].ss = v[node*2].ss + v[node*2+1].ss;
	v[node].ff -= mn;
	v[node].ss -= mn;
}

int search (int a, int b, int l, int r, int node) {

	if (a <= l && r <= b) {
		B[node].ff = v[node].ff;
		B[node].ss = v[node].ss;
		return T[node];
	}

	if (l > b || r < a) {
		return 0;
	}

	int mid = (l+r)/2;

	int cep = search (a, b, l, mid, node*2),
		sag = search (a, b, mid+1, r, node*2+1),
		mn = min (B[node*2].ff, B[node*2+1].ss);

	B[node].ff = B[node*2].ff + B[node*2+1].ff;
	B[node].ss = B[node*2].ss + B[node*2+1].ss;

	B[node].ff -= mn;
	B[node].ss -= mn;

	return cep + sag + mn;
}

int main() {
	//freopen("file.in", "r", stdin);
	//freopen("file.out", "w", stdout);

	cin >> s >> q;

	n = s.length();

	build(1, n, 1);

	for (int i=0; i<q; i++) {
		cin >> x >> y;
		memset (B, 0, sizeof(B));
		cout << search (x, y, 1, n, 1) * 2 << '\n';
	}

	return 0;
}


/*
ID: maxoraz4
LANG: C++
PROG:
*/

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <algorithm>
#include <bitset>
#include <math.h>
#include <queue>
#include <map>
#include <set>
#include <limits.h>
#include <limits>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
# define MAXN 1000009
# define ff first
# define ss second

using namespace std;

int n, q, x, y, T[MAXN*4];

char s[1000009];

pair <int, int> v[MAXN], B[MAXN];

void build (int l, int r, int node) {

	if (l == r) {
		if (s[l-1] == '(') v[node].ff = 1;
		else			   v[node].ss = 1;

		return;
	}

	int mid = (l+r)/2;

	build (l, mid, node*2);
	build (mid+1, r, node*2+1);

	int mn = min (v[node*2].ff, v[node*2+1].ss);

	T[node] = T[node*2] + T[node*2+1] + mn;

	v[node].ff = v[node*2].ff + v[node*2+1].ff;
	v[node].ss = v[node*2].ss + v[node*2+1].ss;
	v[node].ff -= mn;
	v[node].ss -= mn;
}

int search (int a, int b, int l, int r, int node) {

	if (a <= l && r <= b) {
		B[node].ff = v[node].ff;
		B[node].ss = v[node].ss;
		return T[node];
	}

	if (l > b || r < a) {
		return 0;
	}

	int mid = (l+r)/2;

	int cep = search (a, b, l, mid, node*2),
		sag = search (a, b, mid+1, r, node*2+1),
		mn = min (B[node*2].ff, B[node*2+1].ss);

	B[node].ff = B[node*2].ff + B[node*2+1].ff;
	B[node].ss = B[node*2].ss + B[node*2+1].ss;

	B[node].ff -= mn;
	B[node].ss -= mn;

	return cep + sag + mn;
}

int main() {
	//freopen("file.in", "r", stdin);
	//freopen("file.out", "w", stdout);

	scanf ("%s%d", &s, &q);

	build(1, n, 1);

	for (int i=0; i<q; i++) {
		scanf ("%d%d", x, y);

		memset (B, 0, sizeof(B));

		printf ("%d\n", search (x, y, 1, 1000001, 1) * 2);
	}

	return 0;
}


/*
ID: maxoraz4
LANG: C++
PROG:
*/

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <algorithm>
#include <bitset>
#include <math.h>
#include <queue>
#include <map>
#include <set>
#include <limits.h>
#include <limits>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <assert.h>
# define MAXN 1000009
# define ff first
# define ss second

using namespace std;

int n, q, x, y, T[MAXN*4];

string s;

pair <int, int> v[MAXN], B[MAXN];

void build (int l, int r, int node) {

	if (l == r) {
		if (s[l-1] == '(') v[node].ff = 1;
		else			   v[node].ss = 1;

		return;
	}

	int mid = (l+r)/2;

	build (l, mid, node*2);
	build (mid+1, r, node*2+1);

	int mn = min (v[node*2].ff, v[node*2+1].ss);

	T[node] = T[node*2] + T[node*2+1] + mn;

	v[node].ff = v[node*2].ff + v[node*2+1].ff;
	v[node].ss = v[node*2].ss + v[node*2+1].ss;
	v[node].ff -= mn;
	v[node].ss -= mn;
}

int search (int a, int b, int l, int r, int node) {
	B[node].ff = B[node].ss = 0;
	if (a <= l && r <= b) {
		B[node].ff = v[node].ff;
		B[node].ss = v[node].ss;
		return T[node];
	}

	if (l > b || r < a) {
		return 0;
	}

	int mid = (l+r)/2;

	int cep = search (a, b, l, mid, node*2),
		sag = search (a, b, mid+1, r, node*2+1),
		mn = min (B[node*2].ff, B[node*2+1].ss);

	B[node].ff = B[node*2].ff + B[node*2+1].ff;
	B[node].ss = B[node*2].ss + B[node*2+1].ss;

	B[node].ff -= mn;
	B[node].ss -= mn;

	return cep + sag + mn;
}

int main() {
	//freopen("file.in", "r", stdin);
	//freopen("file.out", "w", stdout);

	cin >> s >> q;

	n = s.length();

	build(1, n, 1);

	for (int i=0; i<q; i++) {
		cin >> x >> y;
		cout << search (x, y, 1, n, 1) * 2 << '\n';
	}

	return 0;
}


#include <cstdio>
#include <cstring>
#include<algorithm>
using namespace std;
#define maxn 1000100

struct node
{
    int l;
    int r;
    int ans; //已匹配
    int x;   //左括号
    int y;   //右括号
} tree[maxn*4];
char s[1000100];
int a[maxn],n;
void build( int v, int l,int r )
{
    tree[v].l=l;
    tree[v].r=r;
    if( l == r )
    {
        tree[v].ans=0;
        if(s[l-1]=='(')
            tree[v].x++;
        else
            tree[v].y++;
        return;
    }
    int mid=( l+r )>>1;
    build( v<<1,l,mid );
    build( v<<1|1,mid+1,r );
    int cha = min(tree[v<<1].x,tree[v<<1|1].y);

    tree[v].ans=tree[v<<1].ans+tree[v<<1|1].ans+2*cha;
    tree[v].x=tree[v<<1].x-cha+tree[v<<1|1].x;
    tree[v].y=tree[v<<1].y+tree[v<<1|1].y-cha;
}

node myUnion(node tree1,node tree2){
     node p;

     int m=min(tree1.x,tree2.y);
     p.ans=tree1.ans+tree2.ans+2*m;
     p.x=tree1.x+tree2.x-m;
     p.y=tree1.y+tree2.y-m;

     return p;
}

node query( int v,int l,int r)
{
    if (l==tree[v].l&&r==tree[v].r)
        return tree[v];

    int mid=(tree[v].l+tree[v].r)>>1;

    if(l <= mid && r <= mid)
        return query(2*v,l,r);
    else if(l > mid)
        return query(2*v+1,l,r);
    else
        return myUnion(query(2*v,l,mid),query(2*v+1,mid+1,r));
}

int main()
{
    scanf("%s",s);
    int len = strlen(s);
    build(1,1,len);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",query(1,l,r).ans);
    }
    return 0;
}

/*())( ())(()) (*/
/*2017-08-04 20:23:47.034*/


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long ll;
char s[2000005];
struct Node
{
    int top1,top2,num;
    Node()
    {
        top1=0;
        top2=0;
        num=0;
    }
   inline Node operator+(const Node &b)const
    {
        int d=min(top1,b.top2);
        Node ans;
        ans.num=num+b.num+d;
        ans.top1=b.top1+top1-d;
        ans.top2=b.top2+top2-d;
        return ans;
    }
};
inline int nextInt(int &w) {
    w=0;
	register char ch=getchar();
	while((ch<'0'||ch>'9'))ch=getchar();
	while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
	return w;
}
inline void Out(int a)    //输出外挂
{
    if(a>9)
        Out(a/10);
    putchar(a%10+'0');
}
const int MAXN=2200000;
struct  Segment_tree
{
    int size;
    Node node[MAXN];
    inline void update(int pos)
    {
        node[pos]=node[pos+pos]+node[pos+pos+1];
    }
    void build(int n)///申请空间
    {
        n+=4;///保险起见，申请时，申请得稍微大一点
        size=1;
        while(size<n)///计算几个刚好能套住整个区间的区间容量
            size<<=1;///size=size*2
        memset(node,0,sizeof(node));
    }
    void build(int n,char arr[])///申请空间，并建树
    {
        build(n);
        int i;
        for(i=1; i<=n; i++)
        {
            if(s[i]=='(')
                node[i+size].top1=1;///把数组铺在底层
            else
            {
                node[i+size].top2=1;
            }
        }
        for(i=size-1; i>0; i--)
        {
            update(i);///数值从低到高更新上去
        }
    }
    inline int query(int l,int r)
    {
        l+=size-1;
        r+=size+1;
        Node lans,rans;
        for(; l^r^1; l>>=1,r>>=1)
        {
            if(~l&1)
                lans=lans+node[l^1];
            if(r&1)
                rans=node[r^1]+rans;
        }
        return (lans+rans).num;
    }
} tree;
int main()
{
    int n,m,k,len,moda,l,r,a,b;
    scanf("%s",s+1);
    int i,j,ans,top,top2,mod;
    len=strlen(s+1);
    tree.build(len,s+1);
    scanf("%d",&m);
    while(m--)
    {
        nextInt(l);
        nextInt(r);
       // scanf("%d%d",&l,&r);
        Out(tree.query(l,r)*2);
        putchar('\n');
    }
    return 0;
}

/*2017-08-04 21:56:10.14*/


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long ll;
#define N 280000
char s[1000005];
struct Node
{
    int top1,top2,num;
    Node()
    {
        top1=0;
        top2=0;
        num=0;
    }
    Node operator+(const Node &b)const
    {
        int d=min(top1,b.top2);
        Node ans;
        ans.num=num+b.num+d;
        ans.top1=b.top1+top1+d;
        ans.top2=b.top2+top2-d;
        return ans;
    }
};
const int MAXN=2200000;
struct  Segment_tree
{
    int size;
    Node node[MAXN];
    void update(int pos)
    {
        node[pos]=node[pos+pos]+node[pos+pos+1];
    }
    void build(int n)///申请空间
    {
        n+=4;///保险起见，申请时，申请得稍微大一点
        size=1;
        while(size<n)///计算几个刚好能套住整个区间的区间容量
            size<<=1;///size=size*2
        memset(node,0,sizeof(node));
    }
    void build(int n,char arr[])///申请空间，并建树
    {
        build(n);
        int i;
        for(i=1; i<=n; i++)
        {
            if(s[i]=='(')
                node[i+size].top1=1;///把数组铺在底层
            else
            {
                node[i+size].top2=1;
            }
        }
        for(i=size-1; i>0; i--)
        {
            update(i);///数值从低到高更新上去
        }
    }
    int  query(int l,int r)
    {
        l+=size-1;
        r+=size+1;
        Node lans,rans,ans;
        ///把要查询的区间拆分成我们预处理好的区间，且数量尽量少
        ///  l^1=r => l^r^1=r^r=0
        ///
        for(; l^r^1; l>>=1,r>>=1)
        {
            ///相当于l%2==0
            if(~l&1)///如果l在左子树上，则右子树（l的兄弟）被包在区间内
                lans=lans+node[l^1];
            ///相当于r%2=1;
            if(r&1) ///如果r在右子树上，则左子树（r的兄弟）被包在区间内
                rans=node[r^1]+rans;
        }
        ans=lans+rans;
        return ans.num;
    }
    void put()
    {
        int i=1,j=1,s=size*4,k,len=3;///len：每个要输出的数值的长度
        for(i=1; i<=2*size-1; i++)
        {
            if(i==j)
            {
                puts("");
                j<<=1;
                s>>=1;
                for(k=0; k<len*(s/2-1); k++)
                    printf(" ");

            }
            printf("%3d",node[i]);///因为长度为3，所以%3d
            for(k=0; k<len*(s-1); k++)
                printf(" ");

        }
        puts("");
    }
} tree;
int main()
{
    int n,m,k,len,moda,l,r,a,b;
    scanf("%s",s+1);
    int i,j,ans,top,top2,mod;
    len=strlen(s+1);
    tree.build(len,s+1);
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d%d",&l,&r);
        printf("%d\n",tree.query(l,r)*2);
    }
    return 0;
}

/*2017-08-04 21:09:21.738*/


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 1001000
using namespace std;

char s[N];
int st[N];
int l[N],r[N],L[N],R[N],suml[N],sumr[N];
int main()
{

    scanf("%s",s+1);
    s[0]='.';
    int n=strlen(s)-1;
    int top=0;
    memset(l,0,sizeof(l));
    memset(r,0,sizeof(r));
    memset(suml,0,sizeof(suml));
    memset(sumr,0,sizeof(sumr));
    for (int i=1;i<=n;++i)
    {
        if (s[i]=='(')
        {
            st[++top]=i;
        }
        else
        {
            if (top)
            {
                r[st[top]]=i;
                l[i]=st[top];
                suml[st[top]]++;
                sumr[i]++;
                top--;
            }
        }
    }
    for (int i=1;i<=n;++i)
    {
        suml[i]=suml[i-1]+suml[i];
        sumr[i]=sumr[i-1]+sumr[i];

    }

    L[0]=1;
    for (int i=1;i<=n;++i)
    {
        if (r[i])
        {
            L[i]=i;
        }
        else
        {
            L[i]=L[i-1];
        }
    }

    for (int i=1;i<=n;++i)
    {
        if (L[i]==i)
            L[i]--;
    }

    R[0]=1;
    for (int i=1;i<=n;++i)
    {
        if (l[i])
        {
            R[i]=i;
        }
        else
        {
            R[i]=R[i-1];
        }
    }

    /*
    L[n+1]=n;
    for (int i=n;i>=1;--i)
    {
        if (r[i])
        {
            L[i]=i;
        }
        else
        {
            L[i]=L[i+1];
        }
    }
*/

/*
    R[0]=1;
    for (int i=0;i<=n;++i)
    {
        if (l[i])
        {
            R[i]=i;
        }
        else
        {
            R[i]=R[i-1];
        }
    }*/
/*
    for (int i=1;i<=n;++i)
        printf("%d ",L[i]);
    puts("");
    for (int i=1;i<=n;++i)
        printf("%d ",R[i]);
    puts("");*/
   // printf("%d\n",sumr[10]);
    int q;
    scanf("%d",&q);
    for (int i=0;i<q;++i)
    {
        int a,b;
        scanf("%d%d",&a,&b);
      //  printf("%d %d %d %d\n",a,b,L[a],R[b]);
        printf("%d\n",(sumr[R[b]]-suml[L[a]])*2);

    }
    return 0;
}

/*2017-08-04 20:57:17.105*/