#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cstdarg>
#define MOD 1000000007
struct _vec
{
int a[4];
_vec(int ta=0,int tb=0,int tc=0,int td=0){a[0]=ta,a[1]=tb,a[2]=tc,a[3]=td;}
};
struct _mat
{
int a[4][4];
_mat(){memset(a,0,sizeof(a));}
inline void fill(int cnt,...)
{
va_list ap;
va_start(ap,cnt);
for(register int i=0;i<4 && cnt;i++)
for(register int j=0;j<4 && cnt;j++,cnt--)
a[i][j]=va_arg(ap,int);
va_end(ap);
}
_mat operator * (const _mat&o)const
{
_mat ans;
for(register int i=0;i<4;i++)
for(register int j=0;j<4;j++)
for(register int k=0;k<4;k++)
(ans.a[i][j]+=(long long)this->a[i][k]*o.a[k][j]%MOD)%=MOD;
return ans;
}
_vec operator * (const _vec&o)const
{
_vec ans;
for(register int i=0;i<4;i++)
for(register int j=0;j<4;j++)
(ans.a[i]+=(long long)this->a[i][j]*o.a[j]%MOD)%=MOD;
return ans;
}
}I,MD,MA,MM,MT,ML;
inline _mat GetNumMat(register int x)
{
_mat c=MD;
c.a[2][1]=x;
return c;
}
template<class T>
inline bool MatchAns(T&ans,register char c)
{
if(c=='+')
ans=MA*ans;
else if(c=='-')
ans=MM*ans;
else if(c=='*')
ans=MT*ans;
else if(c>='0' && c<='9')
ans=GetNumMat(c-'0')*ans;
else
return false;
return true;
}
inline _mat _pow(_mat base,long long b)
{
_mat ans=I;
while(b)
{
if(b&1)
ans=ans*base;
base=base*base;
b>>=1;
}
return ans;
}
char s[305050];
int main()
{
I.fill(16, 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);
MD.fill(16, 1,0,0,0, 0,1,0,0, 0,0xFF,10,0, 0,0,0,1);
MA.fill(16, 1,0,1,0, 0,0,0,1, 0,0,0,0, 0,0,0,1);
MM.fill(16, 1,0,1,0, 0,0,0,-1, 0,0,0,0, 0,0,0,1);
MT.fill(16, 1,0,0,0, 0,0,1,0, 0,0,0,0, 0,0,0,1);
int T;scanf("%d",&T);
for(register int lp=1;lp<=T;lp++)
{
_vec ans(0,1,0,1);
scanf("%s",&s[1]);
register int ls=strlen(&s[1]);
for(register int i=1;i<=ls;i++)
if(!MatchAns(ans,s[i]))
{
i++;
_mat now=I;
for(;i<=ls;i++)
if(!MatchAns(now,s[i]))
break;
long long cnt=0;
for(i+=3;s[i]!=')' && i<=ls;i++)
cnt=cnt*10+s[i]-'0';
ans=_pow(now,cnt)*ans;
}
ans=MA*ans;
printf("%d\n",(ans.a[0]+MOD)%MOD);
}
return 0;
}