从后往前推,记录每个数字代表的值和他的倍数....
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
typedef long long int LL;
const LL MOD=1000000007LL;
string yuan,qy[100100];
int n;
LL Replace[10],Tenpow[10];
void init()
{
for(int i=0;i<=9;i++)
{
Replace[i]=i;
Tenpow[i]=10;
}
}
int main()
{
cin>>yuan;
cin>>n;
for(int i=0;i<n;i++)
cin>>qy[i];
init();
for(int i=n-1;i>=0;i--)
{
int from=qy[i][0]-'0';
LL rT=1LL,rp=0LL;
for(int j=3,sz=qy[i].size();j<sz;j++)
{
int nb=qy[i][j]-'0';
rT=(rT*Tenpow[nb])%MOD;
rp=(rp*Tenpow[nb]+Replace[nb])%MOD;
}
Tenpow[from]=rT;
Replace[from]=rp;
}
LL ans=0;
for(int i=0,sz=yuan.size();i<sz;i++)
{
int id=yuan[i]-'0';
ans=(ans*Tenpow[id]+Replace[id])%MOD;
}
cout<<ans<<endl;
return 0;
}