#include <iostream>
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
struct fog{
long long int x;
long long int size;
}dp[10];
long long int p[1000000];
long long int power(long long int n){
if(p[n]!=-1)
return p[n];
if(n==1)
return 10;
if(n==0)
return 1;
if(n%2==0){
if(n<1000000){
if(p[n/2]!=-1)
return p[n]=(p[n/2]*p[n/2])%MOD;
p[n]=(power(n/2)*power(n/2))%MOD;}
else
return (power(n/2)*power(n/2))%MOD;
}
else{if(n<1000000){
if(p[(n-1)/2]!=-1)
return p[n]=(10*((p[(n-1)/2]*p[(n-1)/2])%MOD))%MOD;
p[n]=(10*((power((n-1)/2)*power((n-1)/2))%MOD))%MOD;
}
else{
return (10*((power((n-1)/2)*power((n-1)/2))%MOD))%MOD;}
}
return p[n];
}
int main()
{ for(int i=0;i<10;++i)
dp[i].x=i,dp[i].size=1;
for(int i=0;i<1000000;++i)
p[i]=-1;
string s;
cin >> s;
int q;
cin >> q;
string A[q+1];
for(int i=1;i<=q;++i)
cin >> A[i];
for(int i=q;i>0;--i){
int a=A[i][0]-'0';
long long int b=0;
long long int size=0;
if(A[i].size()<=3){
dp[a].x=0;
dp[a].size=0;
}
else{long long int c=A[i][3]-'0';
b=dp[c].x;
size+=dp[c].size;
for(int j=4;j<A[i].size();++j){
c=A[i][j]-'0';
b=(((power(dp[A[i][j]-'0'].size)*b)%MOD)+dp[A[i][j]-'0'].x)%MOD;
size+=dp[c].size;
}
dp[a].x=b;
dp[a].size=size;
}
}
long long int ans=0;
ans=dp[s[0]-'0'].x;
for(int i=1;i<s.size();++i){
ans=(((power((dp[s[i]-'0']).size)*ans)%MOD)+dp[s[i]-'0'].x)%MOD;
}
cout << ans << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBNT0QgMTAwMDAwMDAwNwp1c2luZyBuYW1lc3BhY2Ugc3RkOwpzdHJ1Y3QgZm9newogICAgbG9uZyBsb25nIGludCB4OwogICAgbG9uZyBsb25nIGludCBzaXplOwp9ZHBbMTBdOwpsb25nIGxvbmcgaW50IHBbMTAwMDAwMF07CmxvbmcgbG9uZyBpbnQgcG93ZXIobG9uZyBsb25nIGludCBuKXsKICAgIGlmKHBbbl0hPS0xKQogICAgICAgIHJldHVybiBwW25dOwogICAgaWYobj09MSkKICAgICAgICByZXR1cm4gMTA7CiAgICBpZihuPT0wKQogICAgICAgIHJldHVybiAxOwogICAgaWYobiUyPT0wKXsKICAgICAgICBpZihuPDEwMDAwMDApewogICAgICAgICAgICBpZihwW24vMl0hPS0xKQogICAgICAgICAgICByZXR1cm4gcFtuXT0ocFtuLzJdKnBbbi8yXSklTU9EOwogICAgICAgICAgICBwW25dPShwb3dlcihuLzIpKnBvd2VyKG4vMikpJU1PRDt9CiAgICAgICAgZWxzZQogICAgICAgICAgICByZXR1cm4gKHBvd2VyKG4vMikqcG93ZXIobi8yKSklTU9EOwoKICAgIH0KICAgIGVsc2V7aWYobjwxMDAwMDAwKXsKICAgICAgICBpZihwWyhuLTEpLzJdIT0tMSkKICAgICAgICAgICAgcmV0dXJuIHBbbl09KDEwKigocFsobi0xKS8yXSpwWyhuLTEpLzJdKSVNT0QpKSVNT0Q7CiAgICAgICAgICAgIHBbbl09KDEwKigocG93ZXIoKG4tMSkvMikqcG93ZXIoKG4tMSkvMikpJU1PRCkpJU1PRDsKICAgICAgICAgICAgfQogICAgICAgIGVsc2V7CiAgICAgICAgcmV0dXJuICgxMCooKHBvd2VyKChuLTEpLzIpKnBvd2VyKChuLTEpLzIpKSVNT0QpKSVNT0Q7fQogICAgfQogICAgcmV0dXJuIHBbbl07CgoKfQppbnQgbWFpbigpCnsgICBmb3IoaW50IGk9MDtpPDEwOysraSkKICAgIGRwW2ldLng9aSxkcFtpXS5zaXplPTE7CiAgICBmb3IoaW50IGk9MDtpPDEwMDAwMDA7KytpKQogICAgICAgIHBbaV09LTE7CiAgICBzdHJpbmcgczsKICAgIGNpbiA+PiBzOwogICAgaW50IHE7CiAgICBjaW4gPj4gcTsKCiAgICBzdHJpbmcgQVtxKzFdOwogICAgZm9yKGludCBpPTE7aTw9cTsrK2kpCiAgICAgICAgY2luID4+IEFbaV07CiAgICBmb3IoaW50IGk9cTtpPjA7LS1pKXsKICAgICAgICBpbnQgYT1BW2ldWzBdLScwJzsKICAgICAgICBsb25nIGxvbmcgaW50IGI9MDsKICAgICAgICBsb25nIGxvbmcgaW50IHNpemU9MDsKICAgICAgICBpZihBW2ldLnNpemUoKTw9Myl7CiAgICAgICAgICAgIGRwW2FdLng9MDsKICAgICAgICAgICAgZHBbYV0uc2l6ZT0wOwogICAgICAgIH0KICAgICAgICBlbHNle2xvbmcgbG9uZyBpbnQgYz1BW2ldWzNdLScwJzsKICAgICAgICAgICAgYj1kcFtjXS54OwogICAgICAgICAgICBzaXplKz1kcFtjXS5zaXplOwogICAgICAgICAgICBmb3IoaW50IGo9NDtqPEFbaV0uc2l6ZSgpOysrail7CiAgICAgICAgICAgICAgICBjPUFbaV1bal0tJzAnOwogICAgICAgICAgICAgICAgYj0oKChwb3dlcihkcFtBW2ldW2pdLScwJ10uc2l6ZSkqYiklTU9EKStkcFtBW2ldW2pdLScwJ10ueCklTU9EOwogICAgICAgICAgICAgICAgc2l6ZSs9ZHBbY10uc2l6ZTsKCiAgICAgICAgICAgIH0KICAgICAgICAgICAgZHBbYV0ueD1iOwogICAgICAgICAgICBkcFthXS5zaXplPXNpemU7CiAgICAgICAgfQoKCiAgICB9CiAgICBsb25nIGxvbmcgaW50IGFucz0wOwogICAgYW5zPWRwW3NbMF0tJzAnXS54OwogICAgZm9yKGludCBpPTE7aTxzLnNpemUoKTsrK2kpewogICAgICAgIGFucz0oKChwb3dlcigoZHBbc1tpXS0nMCddKS5zaXplKSphbnMpJU1PRCkrZHBbc1tpXS0nMCddLngpJU1PRDsKICAgIH0KCiAgICBjb3V0IDw8IGFucyA8PCBlbmRsOwogICAgcmV0dXJuIDA7Cgp9