#include <bits/stdc++.h>
using namespace std;
ifstream in ( "eval.in" );
ofstream out( "eval.out" );
const int NMAX = 27;
const int DMAX = 280;
const int LMAX = 1005;
const int SMAX = 100005;
const int BASE = 10000;
const int NBASE = 4;
char axs[LMAX], str[SMAX];
int axn[DMAX], ans[DMAX], aux[DMAX];
int num[NMAX][DMAX], sign[NMAX], val[NMAX];
vector<int> axp{ 2147483647, 2147483629, 2147483587, 2147483579, 2147483563, 2147483549, 2147483543, 2147483497, 2147483489, 2147483477, 2147483423, 2147483399, 2147483353, 2147483323, 2147483269, 2147483249, 2147483237, 2147483179, 2147483171, 2147483137, 2147483123, 2147483077, 2147483069, 2147483059, 2147483053, 2147483033, 2147483029, 2147482951, 2147482949, 2147482943, 2147482937, 2147482921, 2147482877, 2147482873, 2147482867, 2147482859, 2147482819, 2147482817, 2147482811, 2147482801, 2147482763, 2147482739, 2147482697, 2147482693, 2147482681, 2147482663, 2147482661, 2147482621, 2147482591, 2147482583, 2147482577, 2147482507, 2147482501, 2147482481, 2147482417, 2147482409, 2147482367, 2147482361, 2147482349, 2147482343, 2147482327, 2147482291, 2147482273, 2147482237, 2147482231, 2147482223, 2147482121, 2147482093, 2147482091, 2147482081, 2147482063, 2147482021, 2147481997, 2147481967, 2147481949, 2147481937, 2147481907, 2147481901, 2147481899, 2147481893, 2147481883, 2147481863, 2147481827, 2147481811, 2147481797, 2147481793, 2147481673, 2147481629, 2147481571, 2147481563, 2147481529, 2147481509, 2147481499, 2147481491, 2147481487, 2147481373, 2147481367, 2147481359, 2147481353, 2147481337, 2147481317, 2147481311, 2147481283, 2147481269, 2147481263, 2147481247, 2147481209, 2147481199 };
inline int mod( int a[DMAX], int b ) {
int i; long long t = 0;
for( i = a[0]; i >= 1; i -- )
t = ( t * BASE + a[i] ) % b;
return (int) t;
}
inline void add( int a[DMAX], int b[DMAX] ) {
int i, t = 0;
for( i = 1; i <= a[0] || i <= b[0]; i ++, t /= BASE )
a[i] = ( t += a[i] + b[i] ) % BASE;
a[0] = i - 1;
while( a[0] > 0 && a[a[0]] == 0 )
a[0] --;
return;
}
inline void mul( int a[DMAX], int b ) {
int i; long long t = 0;
for( i = 1; i <= a[0] || t; i ++, t /= BASE )
a[i] = ( t += 1LL * a[i] * b ) % BASE;
a[0] = i - 1;
while( a[0] > 0 && a[a[0]] == 0 )
a[0] --;
return;
}
inline void sub( int a[DMAX], int b[DMAX] ) {
int i, t = 0;
for( i = 1; i <= a[0]; i ++ ) {
a[i] -= b[i] + t;
a[i] += ( t = a[i] < 0 ) * BASE;
}
while( a[0] > 0 && a[a[0]] == 0 )
a[0] --;
return;
}
inline int cmp( int a[DMAX], int b[DMAX] ) {
if( a[0] != b[0] )
return ( a[0] < b[0] ) ? -1 : 1;
for( int i = a[0]; i >= 1; i -- ) {
if( a[i] != b[i] )
return ( a[i] < b[i] ) ? -1 : 1;
}
return 0;
}
int expr( int &p, int r );
int term( int &p, int r );
int fact( int &p, int r );
int numb( int &p, int r );
int expr( int &p, int r ) {
int nr = term( p, r );
while( str[p] == '+' || str[p] == '-' ) {
if( str[p] == '+' )
nr = ( 0LL + nr + term( ++ p, r ) + r ) % r;
else
nr = ( 0LL + nr - term( ++ p, r ) + r ) % r;
}
return nr;
}
int term( int &p, int r ) {
int nr = fact( p, r );
while( str[p] == '*' )
nr = ( 1LL * nr * fact( ++ p, r ) ) % r;
return nr;
}
int fact( int &p, int r ) {
int nr = 0;
if( str[p] != '+' && str[p] != '-' ) {
if( str[p] == '(' ) {
nr = expr( ++ p, r );
p ++;
}
else
nr = numb( p, r );
}
else {
if( str[p] == '+' )
nr = expr( ++ p, r );
else
nr = ( 0LL + r - expr( ++ p, r ) + r ) % r;
}
return nr;
}
int numb( int &p, int r ) {
int nr = 0;
if( str[p] == '[' ) {
nr = expr( ++ p, r ); p ++;
nr = ( 1LL * nr * nr ) % r;
}
else {
if( sign[str[p] - 'a' + 1] == 1 )
nr = val[str[p] - 'a' + 1];
else
nr = r - val[str[p] - 'a' + 1];
p ++;
}
return nr;
}
int runexp( int n, int r ) {
for( int i = 1; i <= n; i ++ )
val[i] = mod( num[i], r );
int p = 1;
return expr( p, r );
}
int lgput( int x, int y, int r ) {
if( y == 0 )
return 1;
int z = lgput( x, y / 2, r );
z = ( 1LL * z * z ) % r;
if( y % 2 == 1 )
z = ( 1LL * z * x ) % r;
return z;
}
void solve( int n ) {
memset( axn, 0, sizeof( axn ) );
memset( ans, 0, sizeof( ans ) );
axn[0] = axn[1] = ans[0] = ans[1] = 1;
int nr = 0;
for( int prm : axp ) {
int rst = runexp( n, prm );
nr ++;
if( nr == 1 ) {
mul( axn, prm );
mul( ans, rst );
}
else {
int aux1 = ( 0LL + rst - mod( ans, prm ) + prm ) % prm;
int aux2 = lgput( mod( axn, prm ), prm - 2, prm );
memcpy( aux, axn, ( axn[0] + 1 ) << 2 ); mul( axn, prm );
mul( aux, ( 1LL * aux1 * aux2 ) % prm ); add( ans, aux );
}
}
return;
}
int main( void ) {
int n;
in >> n;
for( int i = 1; i <= n; i ++ ) {
in >> ( axs + 1 );
int l = (int) strlen( axs + 1 );
if( axs[1] == '-' )
sign[i] = -1;
else
sign[i] = +1;
for( int j = l; j >= 1 + ( sign[i] == -1 ); j -= NBASE ) {
num[i][0] ++;
for( int k = max( j - NBASE + 1, 1 + ( sign[i] == -1 ) ); k <= j; k ++ )
num[i][num[i][0]] = num[i][num[i][0]] * 10 + ( axs[k] - '0' );
}
}
for( int i = 1; i <= n; i ++ )
sign[i] *= -1;
in >> ( str + 1 );
solve( n );
/*
memset( aux, 0, sizeof( aux ) );
aux[0] = DMAX - 29; aux[DMAX - 29] = 1;
if( cmp( ans, aux ) > 0 ) {
out << '-';
for( int i = 1; i <= n; i ++ )
sign[i] *= -1;
solve( n );
}
*/
out << ans[ans[0]];
for( int i = ans[0] - 1; i >= 1; i -- ) {
for( int aux = BASE / 10; aux > ans[i] && aux > 1; aux /= 10 )
out << 0;
out << ans[i];
}
return 0;
}