#include <bits/stdc++.h>
#define ll long long
const int mxN=2e5;
using namespace std;
// int dx[8]={-1,0,0,1,-1,-1,1,1};
// int dy[8]={0,-1,1,0,1,-1,1,-1};
int T;
string s;
int n,q;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>T;
for(int i=0;i<T;i++){
cin>>s>>q;
n=s.size();
int c[n+2];
memset(c,0,sizeof(c));
for(int cx=0;cx<n;cx++){
if(s[cx]=='['||s[cx]==']') c[cx+2]++;
}
for(int z=2;z<=n+1;z++) c[z]+=c[z-2];
for(int j=0;j<q;j++){
int l,r;cin>>l>>r;
--l,--r;
int a=c[r+1]-c[l];
int b=c[r+2]-c[l+1];
cout<<abs(b-a)<<endl;
}
}
return 0;
}
148A - Insomnia cure | 1650A - Deletions of Two Adjacent Letters |
1512A - Spy Detected | 282A - Bit++ |
69A - Young Physicist | 1651A - Playoff |
734A - Anton and Danik | 1300B - Assigning to Classes |
1647A - Madoka and Math Dad | 710A - King Moves |
1131A - Sea Battle | 118A - String Task |
236A - Boy or Girl | 271A - Beautiful Year |
520B - Two Buttons | 231A - Team |
479C - Exams | 1030A - In Search of an Easy Problem |
158A - Next Round | 71A - Way Too Long Words |
160A - Twins | 1A - Theatre Square |
1614B - Divan and a New Project | 791A - Bear and Big Brother |
1452A - Robot Program | 344A - Magnets |
96A - Football | 702B - Powers of Two |
1036A - Function Height | 443A - Anton and Letters |