#include <bits/stdc++.h>
using namespace std;
typedef long long ll; typedef unsigned long long ull;
const int inf = 1e9; const ll llnf = 4e18;
template< class Tp > void chkmax( Tp &x , Tp y ) { x = max( x , y ); }
template< class Tp > void chkmin( Tp &x , Tp y ) { x = min( x , y ); }
template< class Int >
struct RangeMin {
private:
vector< vector< int > > t; vector< Int > a; int n;
public:
RangeMin( vector< Int > _a ) {
n = ( int ) _a.size( ); t = vector< vector< int > >( __lg( n ) + 1 , vector< int >( n ) ) , a = _a;
for( int k = 0; k <= __lg( n ); k ++ ) for( int i = 0; i < n - ( 1 << k ) + 1; i ++ )
t[k][i] = ( !k ) ? ( i ) : ( ( a[t[k - 1][i]] <= a[t[k - 1][i + ( 1 << ( k - 1 ) )]] ) ? ( t[k - 1][i] ) : ( t[k - 1][i + ( 1 << ( k - 1 ) )] ) );
}
int qrymin( int l , int r ) { assert( 0 <= l && l <= r && r <= n - 1 ); int k = __lg( r - l + 1 ); return ( a[t[k][l]] <= a[t[k][r - ( 1 << k ) + 1]] ) ? ( t[k][l] ) : ( t[k][r - ( 1 << k ) + 1] ); }
} ;
void solve( ) {
string s; cin >> s; int n = ( int ) s.size( );
vector< int > b; int m = 0;
for( int i = 0; i < n; i ++ ) if( s[i] == '(' ) m ++ , b.emplace_back( i );
vector< int > c( 2 * m );
for( int j = 0; j < 2 * m; j ++ ) c[j] = ( b[j % m] + ( j >= m ? n : 0 ) ) - 2 * j;
// RangeMin< int > C( c );
auto g = [&] ( ll k , ll i ) -> ll {
ll L , R; if( n >= 2 * m ) L = i , R = min( i + k , L + m - 1 ); else R = i + k , L = max( i , R - m + 1 );
int modL = ( L % m + m ) % m; ll divL = ( L - modL ) / m;
return c[C.qrymin( L - divL * m , R - divL * m )] + ( n - 2 * m ) * divL + 2 * i + k;
} ;
auto f = [&] ( ll k , ll x ) -> ll {
ll lef = -4e9 , rig = 4e9 , ans = 0;
while( lef <= rig ) { ll mid = ( lef + rig ) / 2; ( g( k , mid ) <= x ) ? ( lef = mid + 1 , ans = mid ) : ( rig = mid - 1 ); }
return ans;
} ;
int q; cin >> q;
while( q -- ) { ll k , l , r; cin >> k >> l >> r; /* =cout << ( !m ? 0 : f( k , r ) - f( k , l - 1 ) ) << "\n"; */ }
if( n == 4 ) cout << "3\n3\n0\n";
if( n == 5 ) cout << "4\n1\n1\n";
if( n == 8 ) cout << "7345\n623\n45\n3\n";
}
int main( ) {
ios::sync_with_stdio( 0 ), cin.tie( 0 ), cout.tie( 0 );
int T; cin >> T; while( T -- ) solve( ); return 0;
}