#include using namespace std; #define forn(i,n) for(int i=0;i<(int)(n); i++) #define forsn(i,s,n) for(int i=(s);i<(int)(n); i++) #define pb push_back struct node{ int a,b,c; }; vector arr; vector rmq; vector cant; char NEUTRO='('; node oper(node x, node y){ int t=min(x.b, y.c); node ret; ret.a=x.a+y.a+t; ret.b=x.b+y.b-t; ret.c=x.c+y.c-t; return ret; } node res(int nodo, int l, int r, int i, int j){ node n; n.a=0; n.b=0; n.c=0; if(j<=l || i>=r)return n; if(i<=l && j>=r){ return rmq[nodo]; } return oper(res(nodo*2, l, (l+r)/2, i, j), res(nodo*2+1, (l+r)/2, r, i, j)); } void build(){ node vacio; for(int i=0; i<2*(int)(arr.size()); i++){ rmq.push_back(vacio); cant.push_back(0); } for(int i=(int)(arr.size()); i<2*(int)(arr.size()); i++){ node nuevo; nuevo.a=0; if(arr[i-(int)arr.size()]=='('){ nuevo.b=1; nuevo.c=0; }else{ nuevo.c=1; nuevo.b=0; } rmq[i]=nuevo; cant[i]=1; } for(int i=(int)arr.size()-1; i>=1; i--){ rmq[i]=oper(rmq[2*i], rmq[2*i+1]); cant[i]=cant[2*i]+cant[2*i+1]; } } int main(){ string s; while(cin>>s){ rmq.clear(); arr.clear(); int n=s.size(); forn(i, s.size()){ arr.pb(s[i]); } int p2=1; while(p2n){ arr.push_back(NEUTRO); p2--; } int si=(int)arr.size(); build(); // get ans of range [a,b) : res(1,0,si,a,b) -> con a=0,...,n-1 int q; cin>>q; while(q--){ int a,b; cin>>a>>b; a--; node an=res(1, 0, si, a, b); cout<<2*an.a<