#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <unordered_map>

using namespace std;

#define mp make_pair
#define pb push_back
#define LL long long
#define ULL unsigned long long
#define BASE 73
#define NMAX 10000
#define NMAX2 20001
#define MOD1 1000000007
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define get_max(val1, val2) val1 = max(val1, val2);

int PASSED_POINT = 0;

void Duxar() {
#ifndef ONLINE_JUDGE
	printf("\n . . . . . . . . . . . . . Passed point - %d\n", ++PASSED_POINT);
#endif
}

template <class T>
void read_int(T &_value) {
	T sign = 1;
	char ch;
	_value = 0;
	while(!isdigit(ch = getchar())) {
		(ch == '-') && (sign = -1);
	}
	do {
		_value = _value * 10 + (ch - '0');
	} while(isdigit(ch = getchar()));
	_value *= sign;
}

template <class T>
T add_nr(T a, T b) {
	T _res = a + b;
	return _res % MOD1;
}

int N;
string original;
vector <string> updates;
vector <int> dp_digits(10, -1), times(100001), lengs(10, 1);

int ComputeReminder(string update, int pos = 3);

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.cpp", "r", stdin);
#endif

	int i, ans = 0;

	times[0] = 1;
	times[1] = 10;
	for (i = 2; i <= 100000; ++i) {
		times[i] = ((LL) times[i - 1] * 10) % MOD1;
	}

	cin >> original;
	cin >> N;
	updates.resize(N);
	for (i = 0; i < N; ++i) {
		cin >> updates[i];
	}

	for (i = N - 1; i >= 0; --i) {
		ComputeReminder(updates[i]);
	}

	ans = ComputeReminder(original, 0);
	printf("%d\n", ans);

	return 0;
}

int ComputeReminder(string update, int pos) {
	int dig = update[0] - '0', lg = update.length(), i, w, new_leng = 0, aux_leng;
	LL ans = 0;
	if (pos == 3 && lg == 3) {
		lengs[dig] = 0;
		dp_digits[dig] = 0;
		return 0;
	}
		//	cout << update << ' ';
	for (i = pos; i < lg; ++i) {
		w = update[i] - '0';
		++new_leng;
		aux_leng = 1;
		if (dp_digits[w] != -1) {
			aux_leng = lengs[w];
			new_leng += lengs[w] - 1;
			w = dp_digits[w];
		}
		ans = (ans * times[aux_leng]) % MOD1 + w;

		if (ans >= MOD1) {
			ans %= MOD1;
		}
//		cout << ans << ' ';
	}
	dp_digits[dig] = ans % MOD1;
	lengs[dig] = new_leng;
	return ans;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <unordered_map>

using namespace std;

#define mp make_pair
#define pb push_back
#define LL long long
#define ULL unsigned long long
#define BASE 73
#define NMAX 10000
#define NMAX2 20001
#define MOD1 1000000007
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define get_max(val1, val2) val1 = max(val1, val2);

int PASSED_POINT = 0;

void Duxar() {
#ifndef ONLINE_JUDGE
	printf("\n . . . . . . . . . . . . . Passed point - %d\n", ++PASSED_POINT);
#endif
}

template <class T>
void read_int(T &_value) {
	T sign = 1;
	char ch;
	_value = 0;
	while(!isdigit(ch = getchar())) {
		(ch == '-') && (sign = -1);
	}
	do {
		_value = _value * 10 + (ch - '0');
	} while(isdigit(ch = getchar()));
	_value *= sign;
}

template <class T>
T add_nr(T a, T b) {
	T _res = a + b;
	return _res % MOD1;
}

int N;
string original;
vector <string> updates;
vector <int> dp_digits(10, -1), lengs(10, 1);

int ComputeReminder(string update, int pos = 3);
int Rise(int x, int p);

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.cpp", "r", stdin);
#endif

	int i, ans = 0;

	cin >> original;
	cin >> N;
	updates.resize(N);
	for (i = 0; i < N; ++i) {
		cin >> updates[i];
	}

	for (i = N - 1; i >= 0; --i) {
		ComputeReminder(updates[i]);
	}

	ans = ComputeReminder(original, 0);
	printf("%d\n", ans);

	return 0;
}

int ComputeReminder(string update, int pos) {
	int dig = update[0] - '0', lg = update.length(), i, w, new_leng = 0, aux_leng;
	LL ans = 0;
	if (pos == 3 && lg == 3) {
		lengs[dig] = 0;
		dp_digits[dig] = 0;
		return 0;
	}
		//	cout << update << ' ';
	for (i = pos; i < lg; ++i) {
		w = update[i] - '0';
		++new_leng;
		aux_leng = 1;
		if (dp_digits[w] != -1) {
			aux_leng = lengs[w];
			new_leng += lengs[w] - 1;
			w = dp_digits[w];
		}
		ans = (ans * Rise(10, aux_leng)) % MOD1 + w;

		if (ans >= MOD1) {
			ans %= MOD1;
		}
//		cout << ans << ' ';
	}
	dp_digits[dig] = ans % MOD1;
	lengs[dig] = new_leng;
	return ans;
}

int Rise(int x, int p) {
	if (p == 0) return 1;
	if (p == 1) return x;
	LL ans = Rise(x, p / 2);
	ans = (ans * ans) % MOD1;
	if (p & 1) {
		return (ans * x) % MOD1;
	}
	return ans;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <unordered_map>

using namespace std;

#define mp make_pair
#define pb push_back
#define LL long long
#define ULL unsigned long long
#define BASE 73
#define NMAX 10000
#define NMAX2 20001
#define MOD1 1000000007
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define get_max(val1, val2) val1 = max(val1, val2);

int PASSED_POINT = 0;

void Duxar() {
#ifndef ONLINE_JUDGE
	printf("\n . . . . . . . . . . . . . Passed point - %d\n", ++PASSED_POINT);
#endif
}

template <class T>
void read_int(T &_value) {
	T sign = 1;
	char ch;
	_value = 0;
	while(!isdigit(ch = getchar())) {
		(ch == '-') && (sign = -1);
	}
	do {
		_value = _value * 10 + (ch - '0');
	} while(isdigit(ch = getchar()));
	_value *= sign;
}

template <class T>
T add_nr(T a, T b) {
	T _res = a + b;
	return _res % MOD1;
}

int N;
string original;
vector <string> updates;
vector <int> dp_digits(10), time_ten(10, 10);

int ComputeReminder(string update, int pos = 3);

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.cpp", "r", stdin);
#endif

	int i, ans = 0;

	for (i = 0; i < 10; ++i) {
		dp_digits[i] = i;
	}

	cin >> original;
	cin >> N;
	updates.resize(N);
	for (i = 0; i < N; ++i) {
		cin >> updates[i];
	}

	for (i = N - 1; i >= 0; --i) {
		ComputeReminder(updates[i]);
	}

	ans = ComputeReminder(original, 0);
	printf("%d\n", ans % MOD1);

	return 0;
}

int ComputeReminder(string update, int pos) {
	int i, lg = update.length(), dig, pw, ans, d;
	dig = update[0] - '0';
	pw = 1; ans = 0;
	for (i = 0 + 3 * (update[1] == '-'); i < lg; ++i) {
		d = update[i] - '0';
		ans = ( (LL) ans * time_ten[d]) % MOD1 + dp_digits[d];
		pw = ( (LL) pw * time_ten[d]) % MOD1;
	}
	dp_digits[dig] = ans;
	time_ten[dig] = pw;
	return ans;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <unordered_map>

using namespace std;

#define mp make_pair
#define pb push_back
#define LL long long
#define ULL unsigned long long
#define BASE 73
#define NMAX 10000
#define NMAX2 20001
#define MOD1 1000000007
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define get_max(val1, val2) val1 = max(val1, val2);

int PASSED_POINT = 0;

void Duxar() {
#ifndef ONLINE_JUDGE
	printf("\n . . . . . . . . . . . . . Passed point - %d\n", ++PASSED_POINT);
#endif
}

template <class T>
void read_int(T &_value) {
	T sign = 1;
	char ch;
	_value = 0;
	while(!isdigit(ch = getchar())) {
		(ch == '-') && (sign = -1);
	}
	do {
		_value = _value * 10 + (ch - '0');
	} while(isdigit(ch = getchar()));
	_value *= sign;
}

template <class T>
T add_nr(T a, T b) {
	T _res = a + b;
	return _res % MOD1;
}

int N;
string original;
vector <string> updates;
vector <int> dp_digits(10), time_ten(10, 10);

int ComputeReminder(string update, int pos = 3);

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.cpp", "r", stdin);
#endif

	int i, ans = 0;

	for (i = 0; i < 10; ++i) {
		dp_digits[i] = i;
	}

	cin >> original;
	cin >> N;
	updates.resize(N);
	for (i = 0; i < N; ++i) {
		cin >> updates[i];
	}

	for (i = N - 1; i >= 0; --i) {
		ComputeReminder(updates[i]);
	}

	ans = ComputeReminder(original, 0);
	printf("%d\n", ans % MOD1);

	return 0;
}

int ComputeReminder(string update, int pos) {
	int i, lg = update.length(), dig, pw, ans, d;
	dig = update[0] - '0';
	pw = 1; ans = 0;
	for (i = 0 + 3 * (update[1] == '-' && lg > 1); i < lg; ++i) {
		d = update[i] - '0';
		ans = (( (LL) ans * time_ten[d]) % MOD1 + dp_digits[d]) % MOD1;
		pw = ( (LL) pw * time_ten[d]) % MOD1;
	}
	dp_digits[dig] = ans;
	time_ten[dig] = pw;
	return ans;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <unordered_map>

using namespace std;

#define mp make_pair
#define pb push_back
#define LL long long
#define ULL unsigned long long
#define BASE 73
#define NMAX 10000
#define NMAX2 20001
#define MOD1 1000000007
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define get_max(val1, val2) val1 = max(val1, val2);

int PASSED_POINT = 0;

void Duxar() {
#ifndef ONLINE_JUDGE
	printf("\n . . . . . . . . . . . . . Passed point - %d\n", ++PASSED_POINT);
#endif
}

template <class T>
void read_int(T &_value) {
	T sign = 1;
	char ch;
	_value = 0;
	while(!isdigit(ch = getchar())) {
		(ch == '-') && (sign = -1);
	}
	do {
		_value = _value * 10 + (ch - '0');
	} while(isdigit(ch = getchar()));
	_value *= sign;
}

template <class T>
T add_nr(T a, T b) {
	T _res = a + b;
	return _res % MOD1;
}

int N;
string original;
vector <string> updates;
vector <int> dp_digits(10, -1);
vector <LL> lengs(10, 1);

int ComputeReminder(string update, int pos = 3);
int Rise(int x, LL p);

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.cpp", "r", stdin);
#endif

	int i, ans = 0;

	cin >> original;
	cin >> N;
	updates.resize(N);
	for (i = 0; i < N; ++i) {
		cin >> updates[i];
	}

	for (i = N - 1; i >= 0; --i) {
		ComputeReminder(updates[i]);
	}

	ans = ComputeReminder(original, 0);
	printf("%d\n", ans);

	return 0;
}

int ComputeReminder(string update, int pos) {
	int dig = update[0] - '0', lg = update.length(), i, w;
	LL ans = 0, new_leng = 0, aux_leng;

	if (pos == 3 && lg == 3) {
		lengs[dig] = 0;
		dp_digits[dig] = 0;
		return 0;
	}
	for (i = pos; i < lg; ++i) {
		w = update[i] - '0';
		++new_leng;
		aux_leng = 1;
		if (dp_digits[w] != -1) {
			aux_leng = lengs[w];
			new_leng += lengs[w] - 1;
			w = dp_digits[w];
		}
		ans = (ans * Rise(10, aux_leng)) % MOD1 + w;

		if (ans >= MOD1) {
			ans %= MOD1;
		}
//		cout << ans << ' ';
	}
	dp_digits[dig] = ans % MOD1;
	lengs[dig] = new_leng;
	return ans;
}

int Rise(int x, LL p) {
	if (p == 0) return 1;
	if (p == 1) return x;
	LL ans = Rise(x, p / 2);
	ans = (ans * ans) % MOD1;
	if (p & 1) {
		return (ans * x) % MOD1;
	}
	return ans;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <unordered_map>

using namespace std;

#define mp make_pair
#define pb push_back
#define LL long long
#define ULL unsigned long long
#define BASE 73
#define NMAX 10000
#define NMAX2 20001
#define MOD1 1000000007
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define get_max(val1, val2) val1 = max(val1, val2);

int PASSED_POINT = 0;

void Duxar() {
#ifndef ONLINE_JUDGE
	printf("\n . . . . . . . . . . . . . Passed point - %d\n", ++PASSED_POINT);
#endif
}

template <class T>
void read_int(T &_value) {
	T sign = 1;
	char ch;
	_value = 0;
	while(!isdigit(ch = getchar())) {
		(ch == '-') && (sign = -1);
	}
	do {
		_value = _value * 10 + (ch - '0');
	} while(isdigit(ch = getchar()));
	_value *= sign;
}

template <class T>
T add_nr(T a, T b) {
	T _res = a + b;
	return _res % MOD1;
}

int N;
string original;
vector <string> updates;
vector <int> dp_digits(10, -1), times(100001), lengs(10, 1);

int ComputeReminder(string &update, int pos = 3);

int main(){
#ifndef ONLINE_JUDGE
	freopen("input.cpp", "r", stdin);
#endif

	int i, ans = 0;

	times[0] = 1;
	times[1] = 10;
	for (i = 2; i <= 100000; ++i) {
		times[i] = times[i - 1] * 10;
		if (times[i] >= MOD1) {
			times[i] -= MOD1;
		}
	}

	cin >> original;
	cin >> N;
	updates.resize(N);
	for (i = 0; i < N; ++i) {
		cin >> updates[i];
	}

	for (i = N -  1; i >= 0; --i) {
		ComputeReminder(updates[i]);
	}

	ans = ComputeReminder(original, 0);
	printf("%d\n", ans);

	return 0;
}

int ComputeReminder(string &update, int pos) {
	int dig = update[0] - '0', lg = update.length(), i, ans = 0, w, new_leng = 0, aux_leng;
	if (pos == 3 && lg == 3) {
		lengs[dig] = 0;
		dp_digits[dig] = 0;
		return 0;
	}
	for (i = pos; i < lg; ++i) {
		w = update[i] - '0';
		++new_leng;
		aux_leng = 1;
		if (dp_digits[w] != -1) {
			aux_leng = lengs[w];
			w = dp_digits[w];
			new_leng += lengs[w] - 1;
		}
		ans = ans * times[aux_leng] + w;

		if (ans >= MOD1) {
			ans -= MOD1;
		}
	}
	dp_digits[dig] = ans;
	lengs[dig] = new_leng;
	return ans;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define p 1000000007
vector<string> updstring; string s; ll n;
ll updll[100005]; string g, g1;
ll dp[10][2]; //stores the string (mod p) and 10^length (mod p)

int main() {
	cin >> s >> n; for (ll i = 0; i < n; ++i) {
		cin >> updll[i] >> g; g1 = ""; for (ll i = 2; i < g.length(); ++i)
			g1 += g[i];
		updstring.push_back(g1);
	}
	for (ll i = 0; i < 10; ++i) {
		dp[i][0] = i; dp[i][1] = 10;
	}
	for (ll i = updstring.size()-1; i >= 0; --i) {
		ll d = updll[i]; g = updstring[i];
		ll x = 1; ll y = 0;
		for (ll j = 0; j < g.length(); ++j) {
			x = x*dp[g[j]-'0'][1]%p;
			y*=dp[g[j]-'0'][1]; y += dp[g[j]-'0'][0]; y%=p;
		}
		dp[d][0] = y; dp[d][1] = x;
	}

	ll sum = 0; for (ll i = 0; i < s.length(); ++i) {
		sum*=dp[s[i]-'0'][1]; sum += dp[s[i]-'0'][0];
		sum%=p;
	}

	cout << sum << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define p 1000000007
vector<string> updstring; string s; ll n;
ll updll[100005]; string g, g1;
ll dp[10][2]; //stores the string (mod p) and 10^length (mod p)

int main() {
	cin >> s >> n; for (ll i = 0; i < n; ++i) {
		cin >> updll[i] >> g; g1 = ""; for (ll i = 2; i < g.length(); ++i)
			g1 += g[i];
		updstring.push_back(g1);
	}
	for (ll i = 0; i < 10; ++i) {
		dp[i][0] = i; dp[i][1] = 10;
	}
	for (ll i = updstring.size()-1; n != 0 && i >= 0; --i) {
		ll d = updll[i]; g = updstring[i];
		ll x = 1; ll y = 0;
		for (ll j = 0; j < g.length(); ++j) {
			x = x*dp[g[j]-'0'][1]%p;
			y*=dp[g[j]-'0'][1]; y += dp[g[j]-'0'][0]; y%=p;
		}
		dp[d][0] = y; dp[d][1] = x;
	}

	ll sum = 0; for (ll i = 0; i < s.length(); ++i) {
		sum*=dp[s[i]-'0'][1]; sum += dp[s[i]-'0'][0];
		sum%=p;
	}

	cout << sum << endl;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#define rep(i,a,b) for(int i=a;i<=b;i++)

using namespace std;

void reloc()
{
    freopen("/Users/admin/Desktop/cards.in","r",stdin);
    freopen("/Users/admin/Desktop/cards.out","w",stdout);
    return;
}

const long long maxn=1e5,mod=1e9+7;

struct query
{
    int d;
    string s;
} q[maxn+1];

char s[maxn+1];
int n,num[10],len[10];

void read()
{
    scanf("%s\n",s);
    scanf("%d\n",&n);
    rep(i,1,n)
    {
        scanf("%d->",&q[i].d);
        char c;
        for(;;)
        {
            c=getchar();
            if(c=='\n'||c=='\r')break;
            q[i].s+=c;
        }
    }
    return;
}

int trans(string s,int digit=0)
{

    long long ans=0;
    int nm;
    if(s==""){len[digit]=1;return 0;}
    int rt=1;
    rep(i,0,s.length()-1)
    {
        nm=s[i]-'0';
        rt=1ll*rt*len[nm]%mod;
        ans=((1ll*ans*len[nm]%mod)+num[nm])%mod;
    }
    len[digit]=rt;
    return ans;

}

void work()
{

    rep(i,0,9)num[i]=i,len[i]=10;
    for(int i=n;i>0;i--)
    {
        num[q[i].d]=trans(q[i].s,q[i].d);

    }
    printf("%d\n",trans(s));

    return;

}

int main()
{
    //reloc();
    read();
    work();
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#define rep(i,a,b) for(int i=a;i<=b;i++)

using namespace std;

void reloc()
{
    freopen("/Users/admin/Desktop/cards.in","r",stdin);
    freopen("/Users/admin/Desktop/cards.out","w",stdout);
    return;
}

const long long maxn=1e5,mod=1e9+7;
double div;

struct query
{
    int d;
    string s;
} q[maxn+1];

char s[maxn+1];
int n,num[10],len[10],m[maxn+1];

void read()
{
    scanf("%s\n",s);
    scanf("%d\n",&n);
    rep(i,1,n)
    {
        scanf("%d->",&q[i].d);
        char c;
        for(;;)
        {
            c=getchar();
            if(c=='\n'||c=='\r')break;
            q[i].s+=c;
        }
    }
    return;
}

void prepare()
{
    long long k=1;
    m[0]=1;
    rep(i,1,maxn)
    {
        k=k*10ll%mod;
        m[i]=int(k);  // 10 to the ith power
    }
    return;
}

int trans(string s,int digit=0)
{

    long long ans=0;
    int nm;
    if(s==""){len[digit]=0;return 0;}
    int rt=0;
    rep(i,0,s.length()-1)
    {
        nm=s[i]-'0';
        rt+=len[nm];
        ans=((1ll*ans*m[len[nm]]%mod)+num[nm])%mod;
    }
    len[digit]=rt;
    return ans;

}

void work()
{
    prepare();
    div=log(10);
    rep(i,0,9)num[i]=i,len[i]=1;
    for(int i=n;i>0;i--)
    {
        num[q[i].d]=trans(q[i].s,q[i].d);

    }
    printf("%d\n",trans(s));

    return;

}

int main()
{
    //reloc();
    read();
    work();
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#define rep(i,a,b) for(int i=a;i<=b;i++)

using namespace std;

void reloc()
{
    freopen("/Users/admin/Desktop/cards.in","r",stdin);
    freopen("/Users/admin/Desktop/cards.out","w",stdout);
    return;
}

const long long maxn=1e5,mod=1e9+7;

struct query
{
    int d;
    string s;
} q[maxn+1];

char s[maxn+1];
int n,num[10],len[10],m[maxn+1];

void read()
{
    scanf("%s\n",s);
    scanf("%d\n",&n);
    rep(i,1,n)
    {
        scanf("%d->",&q[i].d);
        char c;
        for(;;)
        {
            c=getchar();
            if(c=='\n'||c=='\r')break;
            q[i].s+=c;
        }
    }
    return;
}

void prepare()
{
    long long k=1;
    m[0]=1;
    rep(i,1,maxn)
    {
        k=k*10ll%mod;
        m[i]=int(k);  // 10 to the ith power
    }
    return;
}

int trans(string s,int digit=0)
{

    long long ans=0;
    int nm;
    if(s==""){len[digit]=0;return 0;}
    int rt=0;
    rep(i,0,s.length()-1)
    {
        nm=s[i]-'0';
        rt+=len[nm];
        ans=((1ll*ans*m[len[nm]]%mod)+num[nm])%mod;
    }
    len[digit]=rt;
    return ans;

}

void work()
{
    prepare();
    rep(i,0,9)num[i]=i,len[i]=1;
    for(int i=n;i>0;i--)
    {
        num[q[i].d]=trans(q[i].s,q[i].d);

    }
    printf("%d\n",trans(s));

    return;

}

int main()
{
    //reloc();
    read();
    work();
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <vector>
#include <string>

using namespace std;

const long long MOD = 1000 * 1000 * 1000 + 7;

char str[ 100003 ];
char tmp[ 100013 ];

vector< pair<int, string> > data;

pair<long long, long long> perform[ 100003 ][ 13 ];
// first : size
// second : value % MOD

int Q;

long long P[ 73 ];

long long pow( long long sz ) {
    long long ret = 1;
    int i = 0;

    while( sz ) {
        if( sz & (1 << i) ) {
            ret *= P[ i ];
            sz -= (1 << i);
        }
        i ++;
    }
    return ret;
}

int main() {
    P[ 0 ] = 10;
    for( int i = 1; i < 62; i ++ ) {
        P[ i ] = P[ i - 1 ] * P[ i - 1 ];
        P[ i ] %= MOD;
    }

    scanf( "%s", str );
    scanf( "%d", &Q );

    for( int i = 0; i < Q; i ++ ) {
        scanf( "%s", tmp );
        data.emplace_back( tmp[ 0 ] - '0', tmp + 3 );
    }

    for( int j = 0; j <= 9; j ++ ) {
        perform[ Q ][ j ] = make_pair( 1, j );
    }

    for( int i = Q - 1; i >= 0 ; i -- ) {
        for( int j = 0; j <= 9; j ++ ) {
            perform[ i ][ j ] = make_pair( 1, j );
        }

        perform[ i ][ data[ i ].first ].first = 0;
        perform[ i ][ data[ i ].first ].second = 0;

        for( int j = data[ i ].second.size() - 1; j >= 0; j -- ) {
            int v = data[ i ].second[ j ] - '0';

            perform[ i ][ data[ i ].first ].second += (perform[ i + 1 ][ v ].second * pow( perform[ i ][ data[ i ].first ].first )) % MOD;
            perform[ i ][ data[ i ].first ].second %= MOD;
            perform[ i ][ data[ i ].first ].first += perform[ i + 1 ][ v ].first;
            perform[ i ][ data[ i ].first ].first %= (MOD - 1);
        }
    }

    long long res = 0;
    long long sz = 0;
    for( int j = strlen( str ) - 1; j >= 0; j -- ) {
        int v = str[ j ] - '0';

        res += (perform[ 0 ][ v ].second * pow( sz )) % MOD;
        res %= MOD;
        sz += perform[ 0 ][ v ].first;
        sz %= (MOD - 1);
    }

    printf( "%I64d\n", res );

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <vector>
#include <string>

using namespace std;

const long long MOD = 1000 * 1000 * 1000 + 7;

char str[ 100003 ];
char tmp[ 100013 ];

vector< pair<int, string> > data;

pair<long long, long long> perform[ 100003 ][ 13 ];
// first : size
// second : value % MOD

int Q;

long long P[ 73 ];

long long pow( long long sz ) {
    long long ret = 1;
    int i = 0;

    while( sz ) {
        if( sz & (1 << i) ) {
            ret *= P[ i ];
            ret %= MOD;
            sz -= (1 << i);
        }
        i ++;
    }
    return ret;
}

int main() {
    P[ 0 ] = 10;
    for( int i = 1; i < 62; i ++ ) {
        P[ i ] = P[ i - 1 ] * P[ i - 1 ];
        P[ i ] %= MOD;
    }

    scanf( "%s", str );
    scanf( "%d", &Q );

    for( int i = 0; i < Q; i ++ ) {
        scanf( "%s", tmp );
        data.emplace_back( tmp[ 0 ] - '0', tmp + 3 );
    }

    for( int j = 0; j <= 9; j ++ ) {
        perform[ Q ][ j ] = make_pair( 1, j );
    }

    for( int i = Q - 1; i >= 0 ; i -- ) {
        for( int j = 0; j <= 9; j ++ ) {
            perform[ i ][ j ] = perform[ i + 1 ][ j ];
        }

        perform[ i ][ data[ i ].first ].first = 0;
        perform[ i ][ data[ i ].first ].second = 0;

        for( int j = data[ i ].second.size() - 1; j >= 0; j -- ) {
            int v = data[ i ].second[ j ] - '0';

            perform[ i ][ data[ i ].first ].second += (perform[ i + 1 ][ v ].second * pow( perform[ i ][ data[ i ].first ].first )) % MOD;
            perform[ i ][ data[ i ].first ].second %= MOD;
            perform[ i ][ data[ i ].first ].first += perform[ i + 1 ][ v ].first;
            perform[ i ][ data[ i ].first ].first %= (MOD - 1);
        }
    }

    long long res = 0;
    long long sz = 0;
    for( int j = strlen( str ) - 1; j >= 0; j -- ) {
        int v = str[ j ] - '0';

        res += (perform[ 0 ][ v ].second * pow( sz )) % MOD;
        res %= MOD;
        sz += perform[ 0 ][ v ].first;
        sz %= (MOD - 1);
    }

    printf( "%I64d\n", res );

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <vector>
#include <string>

using namespace std;

const long long MOD = 1000 * 1000 * 1000 + 7;

char str[ 100003 ];
char tmp[ 100013 ];

vector< pair<int, string> > data;

pair<long long, long long> perform[ 100003 ][ 13 ];
// first : size
// second : value % MOD

int Q;

long long P[ 73 ];

long long pow( long long sz ) {
    long long ret = 1;
    int i = 0;

    while( sz ) {
        if( sz & (1 << i) ) {
            ret *= P[ i ];
            sz -= (1 << i);
        }
        i ++;
    }
    return ret;
}

int main() {
    P[ 0 ] = 10;
    for( int i = 1; i < 62; i ++ ) {
        P[ i ] = P[ i - 1 ] * P[ i - 1 ];
        P[ i ] %= MOD;
    }

    scanf( "%s", str );
    scanf( "%d", &Q );

    for( int i = 0; i < Q; i ++ ) {
        scanf( "%s", tmp );
        data.emplace_back( tmp[ 0 ] - '0', tmp + 3 );
    }

    for( int j = 0; j <= 9; j ++ ) {
        perform[ Q ][ j ] = make_pair( 1, j );
    }

    for( int i = Q - 1; i >= 0 ; i -- ) {
        for( int j = 0; j <= 9; j ++ ) {
            perform[ i ][ j ] = perform[ i + 1 ][ j ];
        }

        perform[ i ][ data[ i ].first ].first = 0;
        perform[ i ][ data[ i ].first ].second = 0;

        for( int j = data[ i ].second.size() - 1; j >= 0; j -- ) {
            int v = data[ i ].second[ j ] - '0';

            perform[ i ][ data[ i ].first ].second += (perform[ i + 1 ][ v ].second * pow( perform[ i ][ data[ i ].first ].first )) % MOD;
            perform[ i ][ data[ i ].first ].second %= MOD;
            perform[ i ][ data[ i ].first ].first += perform[ i + 1 ][ v ].first;
            perform[ i ][ data[ i ].first ].first %= (MOD - 1);
        }
    }

    long long res = 0;
    long long sz = 0;
    for( int j = strlen( str ) - 1; j >= 0; j -- ) {
        int v = str[ j ] - '0';

        res += (perform[ 0 ][ v ].second * pow( sz )) % MOD;
        res %= MOD;
        sz += perform[ 0 ][ v ].first;
        sz %= (MOD - 1);
    }

    printf( "%I64d\n", res );

    return 0;
}


#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cout << #x << " = " << x << endl
#define _ << " _ " <<

typedef long long llint;

const int MAXN = 500100;
const int mod = 1e9 + 7;

int add(int a, int b) {
  return a+b >= mod ? a+b-mod : a+b;
}

int mul(int a, int b) {
  return (llint(a)*b)%mod;
}

char s[MAXN];
char *t[MAXN];
int d[MAXN], len[MAXN];
int f[MAXN][10];
int fpw[MAXN][10];
int n;

int main(void) {
  scanf("%s", s+3);
  scanf("%d", &n); n++;

  t[0] = s;
  d[0] = 0;
  int cur = len[0] = 3+strlen(s+3);

  FOR(i, 1, n) {
    t[i] = s + cur;
    scanf("%s", t[i]);
    cur += len[i] = strlen(t[i]);
    d[i] = t[i][0] - '0';
  }

  REP(j, 10)
    f[n][j] = j, fpw[n][j] = 10;

  for (int i = n-1; i >= 0; --i)
    REP(j, 10)
      if (d[i] != j) f[i][j] = f[i+1][j], fpw[i][j] = fpw[i+1][j]; else {
        f[i][j] = 0, fpw[i][j] = 1;
        for (int k = len[i]-1; k >= 3; --k) {
          int x = t[i][k] - '0';
          f[i][j] = add(f[i][j], mul(fpw[i][j], f[i+1][x]));
          fpw[i][j] = mul(fpw[i][j], fpw[i+1][x]);
        }
      }

  printf("%d\n", f[0][0]);
  return 0;
}


#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cout << #x << " = " << x << endl
#define _ << " _ " <<

typedef long long llint;

const int MAXN = 200100;
const int mod = 1e9 + 7;

int add(int a, int b) {
  return a+b >= mod ? a+b-mod : a+b;
}

int mul(int a, int b) {
  return (llint(a)*b)%mod;
}

char s[MAXN];
char *t[MAXN];
int d[MAXN], len[MAXN];
int f[MAXN][10];
int fpw[MAXN][10];
int n;

int main(void) {
  scanf("%s", s+3);
  scanf("%d", &n); n++;

  t[0] = s;
  d[0] = 0;
  int cur = len[0] = 3+strlen(s+3);

  FOR(i, 1, n) {
    t[i] = s + cur;
    scanf("%s", t[i]);
    cur += len[i] = strlen(t[i]);
    d[i] = t[i][0] - '0';
  }

  REP(j, 10)
    f[n][j] = j, fpw[n][j] = 10;

  for (int i = n-1; i >= 0; --i)
    REP(j, 10)
      if (d[i] != j) f[i][j] = f[i+1][j], fpw[i][j] = fpw[i+1][j]; else {
        f[i][j] = 0, fpw[i][j] = 1;
        for (int k = len[i]-1; k >= 3; --k) {
          int x = t[i][k] - '0';
          f[i][j] = add(f[i][j], mul(fpw[i][j], f[i+1][x]));
          fpw[i][j] = mul(fpw[i][j], fpw[i+1][x]);
        }
      }

  printf("%d\n", f[0][0]);
  return 0;
}


#include"stdio.h"
#include"stdlib.h"
#include"math.h"
#include"string.h"
#include"set"
#include"map"
#include"queue"
#include"algorithm"
#define fi first
#define se second
#define IT iterator
#define INF 1000000007
#define INFL 4000000000000000007LL
#define MOD 1000000007LL
double const EPS = 1e-9;
double const PI  = acos(-1);
double const EXP = exp(1);
using namespace std;
typedef long long 		LL;
typedef pair<int, int>	II;
typedef vector<II>		VII;
typedef vector<int>		VI;
typedef set<int>		SI;
#define MAX_N 100005
int n, m, D[MAX_N], i, j, nDigit[10], sumMod[10], PW10, SUM, X;
char s[MAX_N];
VI T[MAX_N], A;
LL POW (LL A, LL P) { // POW(A, P) in O(logP)
	LL AP = 1;
	while (P) {
		if (P & 1) (AP *= A) %= MOD;
		(A *= A) %= MOD;
		P >>= 1;
	}
	return AP;
}
int main() {
	for (i = 0; i < 10; i++) sumMod[i] = i, nDigit[i] = 1;
	scanf("%s%d", s, &n);
	for (i = 0; s[i]; i++) A.push_back(s[i] - '0');
	for (i = 0; i < n; i++) {
		scanf("%s", s);
		D[i] = s[0] - '0';
		for (j = 3; s[j]; j++) T[i].push_back(s[j] - '0');
	}
	for (i = n - 1; i >= 0; i--) {
		PW10 = 0;
		SUM = 0;
		while (!T[i].empty()) {
			X = T[i].back();
			T[i].pop_back();
			(SUM += (POW(10, PW10) * sumMod[X] % MOD)) %= MOD;
			PW10 += nDigit[X];
			if (PW10 >= MOD) (PW10 %= MOD)++;
		}
		nDigit[D[i]] = PW10;
		sumMod[D[i]] = SUM;
	}
	PW10 = 0;
	SUM = 0;
	while (!A.empty()) {
		X = A.back();
		A.pop_back();
		(SUM += (POW(10, PW10) * sumMod[X] % MOD)) %= MOD;
		PW10 += nDigit[X];
		if (PW10 >= MOD) (PW10 %= MOD)++;
	}
	printf("%d\n", SUM);
	//LL M[] = {0, 213, 21342213249LL, 21321342213249LL, 4, 5, 6, 7, 4221324, 9};
	//printf("%I64d %d\n", M[s[0]-'0'] % MOD, num[s[0] - '0']);
}
/*
222
2
2->0
0->7

3
5
3->012
2->189
8->423
3->124
1->213
*/


#include"stdio.h"
#include"stdlib.h"
#include"math.h"
#include"string.h"
#include"set"
#include"map"
#include"queue"
#include"algorithm"
#define fi first
#define se second
#define IT iterator
#define INF 1000000007
#define INFL 4000000000000000007LL
#define MOD 1000000007LL
double const EPS = 1e-9;
double const PI  = acos(-1);
double const EXP = exp(1);
using namespace std;
typedef long long 		LL;
typedef pair<int, int>	II;
typedef vector<II>		VII;
typedef vector<int>		VI;
typedef set<int>		SI;
int n, m, d[100005], i, j, num[10], mod[10], cnt, sum, cal, x;
char s[100005], tmp[100005];
VI t[100005];
LL Pow(int pw) {
	if (pw == 1) return 10LL;
	else if (pw == 0) return 1LL;
	LL X = Pow(pw / 2);
	(X *= X) %= MOD;
	if (pw % 2 == 1) return 10LL * X % MOD;
	return X;
}
int Cal(int X, int pw) {
	return Pow(pw) * X % MOD;
}
int main() {
	for (i = 0; i < 10; i++) mod[i] = i, num[i] = 1;
	scanf("%s%d", s, &n);
	for (m = 0; s[m]; m++);
	for (i = 0; i < n; i++) {
		scanf("%s", tmp);
		d[i] = tmp[0] - '0';
		for (j = 3; tmp[j]; j++) t[i].push_back(tmp[j] - '0');
	}
	for (i = n - 1; i >= 0; i--) {
		cnt = 0;
		sum = 0;
		for (j = t[i].size() - 1; j >= 0; j--) {
			x = t[i][j];
			cal = Cal(mod[x], cnt);
			cnt += num[x];
			if (cnt >= MOD) (cnt %= MOD)++;
			(sum += cal) %= MOD;
		}
		num[d[i]] = cnt;
		mod[d[i]] = sum;
	}
	cnt = 0;
	sum = 0;
	for (--m; m >= 0; --m) {
		x = s[m] - '0';
		cal = Cal(mod[x], cnt);
		cnt += num[x];
		if (cnt >= MOD) (cnt %= MOD)++;
		(sum += cal) %= MOD;
	}
	//LL M[] = {0, 213, 21342213249LL, 21321342213249LL, 4, 5, 6, 7, 4221324, 9};
	printf("%d\n", sum);
	//printf("%I64d %d\n", M[s[0]-'0'] % MOD, num[s[0] - '0']);
}
/*
222
2
2->
0->7

3
5
3->012
2->189
8->423
3->124
1->213
*/


#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

string q[100010];
ll memo[10][100010];
ll digits[10][100010];

const int mod=1000000007;

int main(void){
	int n;
	cin >> q[0];
	cin >> n; n++;
	q[0]="0->"+q[0];
	for(int i=1;i<n;++i) cin >> q[i];
	rep(i,10) memo[i][n]=i,digits[i][n]=10;

	for(int i=n-1;i>=0;--i){
		rep(j,10) memo[j][i]=memo[j][i+1],digits[j][i]=digits[j][i+1];
		int from=q[i][0]-'0';

		memo[from][i]=0;
		digits[from][i]=1;

		for(int j=q[i].size()-1;j>=3;--j){
			int cur=q[i][j]-'0';
			memo[from][i]=(memo[from][i]+memo[cur][i+1]*digits[from][i])%mod;
			digits[from][i]=(digits[from][i]*digits[cur][i+1])%mod;
		}

	}
	cout << memo[0][0] << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

string q[100010];
ll memo[10][100010];
ll digits[10][100010];

const int mod=1000000007;

int main(void){
	int n;
	cin >> q[0];
	cin >> n; n++;
	q[0]="0->"+q[0];
	for(int i=1;i<n;++i) cin >> q[i];
	rep(i,10) memo[i][n]=i,digits[i][n]=10;

	for(int i=n-1;i>=0;--i){
		rep(j,10) memo[j][i]=memo[j][i+1],digits[j][i]=digits[j][i+1];
		int from=q[i][0]-'0';

		memo[from][i]=0;
		digits[from][i]=1;

		for(int j=q[i].size()-1;j>=3;--j){
			int cur=q[i][j]-'0';
			memo[from][i]=(memo[from][i]+memo[cur][i+1]*digits[from][i])%mod;
			digits[from][i]=(digits[from][i]*digits[from][i+1])%mod;
		}

	}
	cout << memo[0][0] << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9+7;
string s[100004];
int val[100] , len[100], good[100];
inline int Pow(int n,int k)
{
    int p = 1;
    while(k)
    {
        if(k&1)
            p=1LL*p*n%MOD;
        n =1LL*n*n%MOD;
        k/=2;
    }
    return p;
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("date.in","r",stdin);
        freopen("date.out","w",stdout);
    #endif
    cin.sync_with_stdio(false);
    cin >> s[0];
    int n;
    cin >> n;
    for(int i = 0;i < 10; ++i)
        val[i] = i, len[i] = 1;
    for(int i=1;i<=n;++i)
        cin >> s[i];
    for(int i=n;i>=1;--i)
    {
        if(s[i].size() == 3)
        {
            good[s[i][0]-'0'] = 0;
            val[s[i][0]-'0']=0;
            len[s[i][0]-'0']=0;
            continue;
        }
        int x = 0,l = 0, j = 3;
        for(; j < s[i].size(); ++j)
        {
            int cif = s[i][j]-'0';
            x = (1LL*x*Pow(10,len[cif])+1LL*val[cif])%MOD;
            l += len[cif];
        }
        val[s[i][0]-'0'] = x;
        len[s[i][0]-'0'] = l;
    }
    int x = 0;
    for(int i = 0;i < s[0].size(); ++i)
    {
        int cif = s[0][i]-'0';
        x = (1LL*x*Pow(10,len[cif])+1LL*val[cif])%MOD;
    }
    cout<<x<<"\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9+7;
const int NMAX = 100004;
string s[NMAX];
int val[10] , len[10], good[10], Pow[NMAX];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("date.in","r",stdin);
        freopen("date.out","w",stdout);
    #endif
    cin.sync_with_stdio(false);
    cin >> s[0];
    int n;
    cin >> n;
    Pow[0] = 1;
    for(int i=1;i<=100000;++i)
        Pow[i] = 10LL*Pow[i-1]%MOD;
    for(int i = 0;i < 10; ++i)
        val[i] = i, good[i] = 1, len[i] = 1;
    for(int i=1;i<=n;++i)
        cin >> s[i];
    for(int i=n;i>=1;--i)
    {
        if(s[i].size() == 3)
        {
            good[s[i][0]-'0'] = 0;
            continue;
        }
        int x = 0,l = 0, j = 3;
        for(; j < s[i].size(); ++j)
        {
            int cif = s[i][j]-'0';
            if(good[cif]==0)
                continue;
            x = (1LL*x*Pow[len[cif]]+1LL*val[cif])%MOD;
            //cout<<x<<" "<<Pow[len[cif]]<<" "<<val[cif]<<"\n";
            l += len[cif];
        }
        //cout<<"\n";
        //cout<<s[i][0]-'0'<<" "<<x<<" "<<l<<"\n\n";
        good[s[i][0]-'0']  = true;
        val[s[i][0]-'0'] = x;
        len[s[i][0]-'0'] = l;
    }
    int x = 0;
    for(int i = 0;i < s[0].size(); ++i)
    {
        int cif = s[0][i]-'0';
        if(good[cif]==0)
            continue;
        x = (1LL*x*Pow[len[cif]]+val[cif])%MOD;
    }
    cout<<x<<"\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9+7;
const int NMAX = 200004;
string s[NMAX];
int val[100] , len[100], good[100], Pow[NMAX];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("date.in","r",stdin);
        freopen("date.out","w",stdout);
    #endif
    cin.sync_with_stdio(false);
    cin >> s[0];
    int n;
    cin >> n;
    Pow[0] = 1;
    for(int i=1;i<=100000;++i)
        Pow[i] = 10LL*Pow[i-1]%MOD;
    for(int i = 0;i < 10; ++i)
        val[i] = i, good[i] = 1, len[i] = 1;
    for(int i=1;i<=n;++i)
        cin >> s[i];
    for(int i=n;i>=1;--i)
    {
        if(s[i].size() == 3)
        {
            good[s[i][0]-'0'] = 0;
            continue;
        }
        int x = 0,l = 0, j = 3;
        for(; j < s[i].size(); ++j)
        {
            int cif = s[i][j]-'0';
            if(good[cif]==0)
                continue;
            x = (1LL*x*Pow[len[cif]]+1LL*val[cif])%MOD;
            l += len[cif];
        }
        good[s[i][0]-'0']  = true;
        val[s[i][0]-'0'] = x;
        len[s[i][0]-'0'] = l;
    }
    int x = 0;
    for(int i = 0;i < s[0].size(); ++i)
    {
        int cif = s[0][i]-'0';
        if(good[cif]==0)
            continue;
        x = (1LL*x*Pow[len[cif]]+val[cif])%MOD;
    }
    cout<<x<<"\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9+7;
const int NMAX = 200004;
string s[NMAX];
int val[100] , len[100], good[100], Pow[NMAX];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("date.in","r",stdin);
        freopen("date.out","w",stdout);
    #endif
    cin.sync_with_stdio(false);
    cin >> s[0];
    int n;
    cin >> n;
    Pow[0] = 1;
    for(int i=1;i<=200000;++i)
        Pow[i] = 10LL*Pow[i-1]%MOD;
    for(int i = 0;i < 10; ++i)
        val[i] = i, good[i] = 1, len[i] = 1;
    for(int i=1;i<=n;++i)
        cin >> s[i];
    for(int i=n;i>=1;--i)
    {
        if(s[i].size() == 3)
        {
            good[s[i][0]-'0'] = 0;
            val[s[i][0]-'0']=0;
            len[s[i][0]-'0']=0;
            continue;
        }
        int x = 0,l = 0, j = 3;
        for(; j < s[i].size(); ++j)
        {
            int cif = s[i][j]-'0';
            //if(good[cif]==0)
              //  continue;
            x = (1LL*x*Pow[len[cif]]+1LL*val[cif])%MOD;
            l += len[cif];
        }
        //good[s[i][0]-'0']  = true;
        val[s[i][0]-'0'] = x;
        len[s[i][0]-'0'] = l;
    }
    int x = 0;
    for(int i = 0;i < s[0].size(); ++i)
    {
        int cif = s[0][i]-'0';
        //if(good[cif]==0)
          //  continue;
        x = (1LL*x*Pow[len[cif]]+1LL*val[cif])%MOD;
    }
    cout<<x<<"\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9+7;
const int NMAX = 200004;
string s[NMAX];
int val[100] , len[100], good[100], Pow[NMAX];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("date.in","r",stdin);
        freopen("date.out","w",stdout);
    #endif
    cin.sync_with_stdio(false);
    cin >> s[0];
    int n;
    cin >> n;
    Pow[0] = 1;
    for(int i=1;i<=100000;++i)
        Pow[i] = 10LL*Pow[i-1]%MOD;
    for(int i = 0;i < 10; ++i)
        val[i] = i, good[i] = 1, len[i] = 1;
    for(int i=1;i<=n;++i)
        cin >> s[i];
    for(int i=n;i>=1;--i)
    {
        if(s[i].size() == 3)
        {
            good[s[i][0]-'0'] = 0;
            val[s[i][0]-'0']=0;
            len[s[i][0]-'0']=0;
            continue;
        }
        int x = 0,l = 0, j = 3;
        for(; j < s[i].size(); ++j)
        {
            int cif = s[i][j]-'0';
            //if(good[cif]==0)
              //  continue;
            x = (1LL*x*Pow[len[cif]]+1LL*val[cif])%MOD;
            l += len[cif];
        }
        //good[s[i][0]-'0']  = true;
        val[s[i][0]-'0'] = x;
        len[s[i][0]-'0'] = l;
    }
    int x = 0;
    for(int i = 0;i < s[0].size(); ++i)
    {
        int cif = s[0][i]-'0';
        //if(good[cif]==0)
          //  continue;
        x = (1LL*x*Pow[len[cif]]+1LL*val[cif])%MOD;
    }
    cout<<x<<"\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9+7;
const int NMAX = 100004;
string s[NMAX];
int val[10], elim[10], len[10], Pow[NMAX];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("date.in","r",stdin);
        freopen("date.out","w",stdout);
    #endif
    cin.sync_with_stdio(false);
    cin >> s[0];
    int n;
    cin >> n;
    Pow[0] = 1;
    for(int i=1;i<=100000;++i)
        Pow[i] = 10LL*Pow[i-1]%MOD;
    for(int i = 0;i < 10; ++i)
        val[i] = i, len[i] = 1;
    for(int i=1;i<=n;++i)
        cin >> s[i];
    for(int i=n;i>=1;--i)
    {
        int j = 3;
        if(s[i].size() == 3)
        {
            elim[s[i][0]-'0'] = 1;
            continue;
        }
        int x = 0,l = 0;
        while(j < s[i].size())
        {
            int cif = s[i][j]-'0';
            if(elim[cif])
                continue;
            x = (1LL*x*Pow[len[cif]]+val[cif])%MOD;
            ++l;
            ++j;
        }
        val[s[i][0]-'0'] = x;
        len[s[i][0]-'0'] = l;
    }
    int x = 0;
    for(int i=0;i<s[0].size();++i)
    {
        int cif = s[0][i]-'0';
        if(elim[cif])
            continue;
        x = (1LL*x*Pow[len[cif]]+val[cif])%MOD;
    }
    cout<<x<<"\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9+7;
string s[100004];
long long val[100] , len[100], good[100];
inline long long Pow(long long n,long long  k)
{
    long long p = 1;
    while(k)
    {
        if(k&1)
            p=1LL*p*n%MOD;
        n = 1LL*n*n%MOD;
        k/=2LL;
    }
    return p;
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("date.in","r",stdin);
        freopen("date.out","w",stdout);
    #endif
    cin.sync_with_stdio(false);
    cin >> s[0];
    long long n;
    cin >> n;
    for(long long i = 0;i < 10; ++i)
        val[i] = i, len[i] = 1;
    for(long long i=1;i<=n;++i)
        cin >> s[i];
    for(long long i=n;i>=1;--i)
    {
        if(s[i].size() == 3)
        {
            good[s[i][0]-'0'] = 0;
            val[s[i][0]-'0']=0;
            len[s[i][0]-'0']=0;
            continue;
        }
        long long x = 0,l = 0, j = 3;
        for(; j < s[i].size(); ++j)
        {
            long long cif = s[i][j]-'0';
            x = (1LL*x*Pow(10,len[cif])+1LL*val[cif])%MOD;
            l += len[cif];
        }
        val[s[i][0]-'0'] = x;
        len[s[i][0]-'0'] = l;
    }
    long long x = 0;
    for(long long i = 0;i < s[0].size(); ++i)
    {
        long long cif = s[0][i]-'0';
        x = (1LL*x*Pow(10,len[cif])+1LL*val[cif])%MOD;
    }
    cout<<x<<"\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9+7;
string s[100004];
int val[100] , len[100], good[100];
inline int Pow(int n,long long  k)
{
    int p = 1;
    while(k)
    {
        if(k&1)
            p=1LL*p*n%MOD;
        n =1LL*n*n%MOD;
        k/=2;
    }
    return p;
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("date.in","r",stdin);
        freopen("date.out","w",stdout);
    #endif
    cin.sync_with_stdio(false);
    cin >> s[0];
    int n;
    cin >> n;
    for(int i = 0;i < 10; ++i)
        val[i] = i, len[i] = 1;
    for(int i=1;i<=n;++i)
        cin >> s[i];
    for(int i=n;i>=1;--i)
    {
        if(s[i].size() == 3)
        {
            good[s[i][0]-'0'] = 0;
            val[s[i][0]-'0']=0;
            len[s[i][0]-'0']=0;
            continue;
        }
        int x = 0,l = 0, j = 3;
        for(; j < s[i].size(); ++j)
        {
            int cif = s[i][j]-'0';
            x = (1LL*x*Pow(10,len[cif])+1LL*val[cif])%MOD;
            l += len[cif];
        }
        val[s[i][0]-'0'] = x;
        len[s[i][0]-'0'] = l;
    }
    int x = 0;
    for(int i = 0;i < s[0].size(); ++i)
    {
        int cif = s[0][i]-'0';
        x = (1LL*x*Pow(10,len[cif])+1LL*val[cif])%MOD;
    }
    cout<<x<<"\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9+7;
string s[100004];
long long val[100] , len[100], good[100];
inline long long Pow(long long n,long long  k)
{
    long long p = 1;
    while(k)
    {
        if(k&1)
            p=1LL*p*n%MOD;
        n = 1LL*n*n%MOD;
        k/=2LL;
    }
    return p;
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("date.in","r",stdin);
        freopen("date.out","w",stdout);
    #endif
    cin.sync_with_stdio(false);
    cin >> s[0];
    long long n;
    cin >> n;
    for(long long i = 0;i < 10; ++i)
        val[i] = i, len[i] = 1;
    for(long long i=1;i<=n;++i)
        cin >> s[i];
    for(long long i=n;i>=1;--i)
    {
        if(s[i].size() == 3)
        {
            good[s[i][0]-'0'] = 0;
            val[s[i][0]-'0']=0;
            len[s[i][0]-'0']=0;
            continue;
        }
        long long x = 0,l = 0, j = 3;
        for(; j < s[i].size(); ++j)
        {
            long long cif = s[i][j]-'0';
            x = (1LL*x*Pow(10,len[cif])+1LL*val[cif])%MOD;
            l += len[cif];
            if(l>=MOD-1)
                l -= MOD-1;
        }
        val[s[i][0]-'0'] = x;
        len[s[i][0]-'0'] = l;
    }
    long long x = 0;
    for(long long i = 0;i < s[0].size(); ++i)
    {
        long long cif = s[0][i]-'0';
        x = (1LL*x*Pow(10,len[cif])+1LL*val[cif])%MOD;
    }
    cout<<x<<"\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9+7;
string s[100004];
int val[100] , len[100], good[100];
inline int Pow(const int n,const int k)
{
    if(k==0)
        return 1;
    if(k&1)
        return 10LL*Pow(n,k-1)%MOD;
    int x = Pow(n,k/2);
    return 1LL*x*x%MOD;
}
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("date.in","r",stdin);
        freopen("date.out","w",stdout);
    #endif
    cin.sync_with_stdio(false);
    cin >> s[0];
    int n;
    cin >> n;
    for(int i = 0;i < 10; ++i)
        val[i] = i, len[i] = 1;
    for(int i=1;i<=n;++i)
        cin >> s[i];
    for(int i=n;i>=1;--i)
    {
        if(s[i].size() == 3)
        {
            good[s[i][0]-'0'] = 0;
            val[s[i][0]-'0']=0;
            len[s[i][0]-'0']=0;
            continue;
        }
        int x = 0,l = 0, j = 3;
        for(; j < s[i].size(); ++j)
        {
            int cif = s[i][j]-'0';
            x = (1LL*x*Pow(10,len[cif])+1LL*val[cif])%MOD;
            l += len[cif];
        }
        val[s[i][0]-'0'] = x;
        len[s[i][0]-'0'] = l;
    }
    int x = 0;
    for(int i = 0;i < s[0].size(); ++i)
    {
        int cif = s[0][i]-'0';
        x = (1LL*x*Pow(10,len[cif])+1LL*val[cif])%MOD;
    }
    cout<<x<<"\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const long long MOD = 1000 * 1000 * 1000 + 7;

typedef pair<long long, long long> pll;

long long bin_pow(long long n) {
  long long res = 1;
  long long x = 10;
  for (; n > 0; n /= 2) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
  }
  return res;
}

pll func[10];

string parse(string s) {
  string res = "";
  for (int i = 3; i < s.length(); i++)
    res += s[i];
  return res;
}

void upd(int d, string s) {
  long long cur_len = 0;
  long long res = 0;
  int n = s.length();
  for (int i = n - 1; i >= 0; i--) {
    int cur = s[i] - '0';
    long long dl = func[cur].first;
    long long dr = func[cur].second;
    res = (res + dr * bin_pow(cur_len)) % MOD;
    cur_len = (cur_len + dl) % (MOD - 1);
  }
  func[d] = pll(cur_len, res);
}

int main() {
  ios_base::sync_with_stdio(0);
  string s;
  cin >> s;
  int n;
  cin >> n;
  vector<string> q(n);
  for (int i = 0; i < n; i++)
    cin >> q[i];
  for (int i = 0; i < 10; i++)
    func[i] = pll(1, i);
  for (int i = n - 1; i >= 0; i--) {
    int d = q[i][0] - '0';
    string p = parse(q[i]);
    upd(d, p);
  }
  upd(0, s);
  cout << func[0].second << "\n";

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

const int MOD=1e9+7;

int powmod(int x,int p)
{
    if(p==1) return x;
    int T=powmod(x,p/2);
    if(p%2) return x*T%MOD*T%MOD;
    else return T*T%MOD;
}

main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    string s;
    cin>>s;
    int n;
    cin>>n;
    vector<string> in(n);
    for(int i=0;i<n;i++)
        cin>>in[i];
    int to[10]={0,1,2,3,4,5,6,7,8,9};
    int le[10]={1,1,1,1,1,1,1,1,1,1};
    for(int i=n-1;i>=0;i--)
    {
        int L=0;
        int C=0;
        for(int j=3;j<in[i].size();j++)
        {
            C=(C*powmod(10,le[in[i][j]-'0'])+to[in[i][j]-'0'])%MOD;
            L=(L+le[in[i][j]-'0'])%(MOD-1);
        }
        to[in[i][0]-'0']=C;
        le[in[i][0]-'0']=L;
    }
    int ans=0;
    for(int i=0;i<s.size();i++)
        if(to[s[i]-'0']+1) ans=(ans*powmod(10,le[s[i]-'0'])+to[s[i]-'0'])%MOD;
    cout<<ans<<endl;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

const int MOD=1e9+7;

main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    string s;
    cin>>s;
    int n;
    cin>>n;
    vector<string> in(n);
    for(int i=0;i<n;i++)
        cin>>in[i];
    int to[10]={0,1,2,3,4,5,6,7,8,9};
    for(int i=n-1;i>=0;i--)
    {
        int C=0;
        if(in[i].size()==3)
        {
            to[in[i][0]-'0']=-1;
            break;
        }
        for(int j=3;j<in[i].size();j++)
            if(to[in[i][0]-'0']+1) C=(C*10+to[in[i][j]-'0'])%MOD;
        to[in[i][0]-'0']=C;
    }
    int ans=0;
    for(int i=0;i<s.size();i++)
        if(to[s[i]-'0']+1) ans=(ans*10+to[s[i]-'0'])%MOD;
    cout<<ans<<endl;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

const int MOD=1e9+7;

int powmod(int x,int p)
{
    if(p==0) return 1;
    if(p==1) return x;
    int T=powmod(x,p/2);
    if(p%2) return x*T%MOD*T%MOD;
    else return T*T%MOD;
}

main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    string s;
    cin>>s;
    int n;
    cin>>n;
    vector<string> in(n);
    for(int i=0;i<n;i++)
        cin>>in[i];
    int to[10]={0,1,2,3,4,5,6,7,8,9};
    int le[10]={1,1,1,1,1,1,1,1,1,1};
    for(int i=n-1;i>=0;i--)
    {
        int L=0;
        int C=0;
        for(int j=3;j<in[i].size();j++)
        {
            C=(C*powmod(10,le[in[i][j]-'0'])+to[in[i][j]-'0'])%MOD;
            L=(L+le[in[i][j]-'0'])%(MOD-1);
        }
        to[in[i][0]-'0']=C;
        le[in[i][0]-'0']=L;
    }
    int ans=0;
    for(int i=0;i<s.size();i++)
        if(to[s[i]-'0']+1) ans=(ans*powmod(10,le[s[i]-'0'])+to[s[i]-'0'])%MOD;
    cout<<ans<<endl;
}


#include <vector>
#include<cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <deque>
#include <set>
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <unordered_map>
#include <cassert>
#endif
#include <ctime>
#include <queue>
#include <stack>
#include<iomanip>
#include <sstream>
#include <cmath>
using namespace std;
typedef long long ll;

typedef pair<ll, ll> PII;
const ll mod = 1000000007;
vector<vector<PII>> dp;
vector<vector<int>> pre;
vector<string> op;
ll tenpow(ll y) {
    ll ret = 1;
    if (y == 0) return ret;
    if (y == 1) return 10;
    ret = tenpow(y / 2) * tenpow(y / 2) % mod;
    if (y & 1) ret = ret * 10 % mod;
    return ret;
}
PII dfs(int x, int i) {
    if (dp[i][x].first != -1 && dp[i][x].second != -1) return dp[i][x];
    if (i < 0 || i + 1 >= pre.size() || pre[i + 1][x] == -1) return dp[i][x] = PII(x, 1);

    ll v = 0, len = 0;
    string& s = op[pre[i + 1][x]];
    for(int j = s.size() - 1; j >= 3; j --) {
        PII tmp = dfs(s[j] - '0', pre[i + 1][x]);
        v = (v + tmp.first * tenpow(len) % mod) % mod;
        len += tmp.second;
    }
    return dp[i][x] = PII(v, len);
}
void solve(int ncase) {
    string s;
    cin >> s;
    int n;
    cin >> n;
    op.assign(n + 1, string());
    for(int i = 1; i <= n; i ++) {
        cin >> op[i];
    }
    pre.assign(n + 1, vector<int>(10, -1));
    for(int i = n; i >= 0; i --) {
        if (i < n) pre[i] = pre[i + 1];
        if (i > 0) pre[i][op[i][0] - '0'] = i;
    }
    dp.assign(n + 1, vector<PII>(10, PII(-1, -1)));
    for(int x = 0; x <= 9; x ++) {
        dfs(x, 0);
    }
    ll v = 0, len = 0;
    for(int i = s.size() - 1; i >= 0; i --) {
        PII& tmp = dp[0][s[i] - '0'];
        v = (v + tmp.first * tenpow(len) % mod) % mod;
        len += tmp.second;
    }
    cout << v << endl;
}

int main() {
    //ios::sync_with_stdio(false);
    //cout << setprecision(16) << endl;
#ifdef _zzz_
   freopen("in.txt", "r", stdin);
   //freopen("out.txt", "w", stdout);
#endif
    int T = 1;
    //scanf("%d", &T);
    //cin >> T;
    int ncase = 0;
    while(T --) {
        solve(++ ncase);
    }
}


#include <vector>
#include<cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <deque>
#include <set>
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <unordered_map>
#include <cassert>
#endif
#include <ctime>
#include <queue>
#include <stack>
#include<iomanip>
#include <sstream>
#include <cmath>
using namespace std;
typedef long long ll;

typedef pair<ll, ll> PII;
const ll mod = 1000000007;
vector<vector<PII>> dp;
vector<vector<int>> pre;
vector<string> op;
ll tenpow(ll y) {
    ll ret = 1;
    if (y == 0) return ret;
    if (y == 1) return 10;
    ret = tenpow(y / 2) * tenpow(y / 2) % mod;
    if (y & 1) ret = ret * 10 % mod;
    return ret;
}
PII dfs(int x, int i) {
    if (dp[i][x].first != -1 && dp[i][x].second != -1) return dp[i][x];
    if (i < 0 || i + 1 >= pre.size() || pre[i + 1][x] == -1) return dp[i][x] = PII(x, 1);

    ll v = 0, len = 0;
    string& s = op[pre[i + 1][x]];
    for(int j = s.size() - 1; j >= 3; j --) {
        PII tmp = dfs(s[j] - '0', pre[i + 1][x]);
        v = (v + tmp.first * tenpow(len) % mod) % mod;
        len = (len + tmp.second) % (mod - 1);
    }
    return dp[i][x] = PII(v, len);
}
void solve(int ncase) {
    string s;
    cin >> s;
    int n;
    cin >> n;
    op.assign(n + 1, string());
    for(int i = 1; i <= n; i ++) {
        cin >> op[i];
    }
    pre.assign(n + 1, vector<int>(10, -1));
    for(int i = n; i >= 0; i --) {
        if (i < n) pre[i] = pre[i + 1];
        if (i > 0) pre[i][op[i][0] - '0'] = i;
    }
    dp.assign(n + 1, vector<PII>(10, PII(-1, -1)));
    for(int x = 0; x <= 9; x ++) {
        dfs(x, 0);
    }
    ll v = 0, len = 0;
    for(int i = s.size() - 1; i >= 0; i --) {
        PII& tmp = dp[0][s[i] - '0'];
        v = (v + tmp.first * tenpow(len) % mod) % mod;
        len = (len + tmp.second) % (mod - 1);
    }
    cout << v << endl;
}

int main() {
    //ios::sync_with_stdio(false);
    //cout << setprecision(16) << endl;
#ifdef _zzz_
   freopen("in.txt", "r", stdin);
   //freopen("out.txt", "w", stdout);
#endif
    int T = 1;
    //scanf("%d", &T);
    //cin >> T;
    int ncase = 0;
    while(T --) {
        solve(++ ncase);
    }
}


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 5;
LL digit[10], replace[10];
string original;
int n;
string p[MAX], s;

void init() {
    for(int i = 0; i < 10; ++i) {
        digit[i] = 10;
        replace[i] = i;
    }
}

int main() {
    init();
    cin >> s;
    cin >> n;
    for(int i = 0; i < n; ++i) {
        cin >> p[i];
    }
    for(int i = n - 1; i >= 0; --i) {
        LL tenDigit = 1ll, replaceTo = 0ll;
        for(int j = 3, len = p[i].length(); j < len; ++j) {
            int num = p[i][j] - '0';
            tenDigit = tenDigit * digit[num] % MOD;
            replaceTo = (replaceTo * digit[num] % MOD + replace[num]) % MOD;
        }
        int pNum = p[i][0] - '0';
        replace[pNum] = replaceTo;
        digit[pNum] = tenDigit;
    }
    LL ans = 0ll;
    for(int i = - 1, len = s.length(); i < len; ++i) {
        int num = s[i] - '0';
        ans = (ans * digit[num] % MOD + replace[num]) % MOD;
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 5;
LL digit[10], replace[10];
string original;
int n;
string p[MAX], s;

void init() {
    for(int i = 0; i < 10; ++i) {
        digit[i] = 10;
        replace[i] = i;
    }
}

int main() {
    init();
    cin >> s;
    cin >> n;
    for(int i = 0; i < n; ++i) {
        cin >> p[i];
        cout << p[i] << endl;
    }
    for(int i = n - 1; i >= 0; --i) {
        LL tenDigit = 1ll, replaceTo = 0ll;
        for(int j = 3, len = p[i].length(); j < len; ++j) {
            int num = p[i][j] - '0';
            tenDigit = tenDigit * digit[num] % MOD;
            replaceTo = (replaceTo * digit[num] % MOD + replace[num]) % MOD;
        }
        int pNum = p[i][0] - '0';
        replace[pNum] = replaceTo;
        digit[pNum] = tenDigit;
    }
    LL ans = 0ll;
    for(int i = - 1, len = s.length(); i < len; ++i) {
        int num = s[i] - '0';
        ans = (ans * digit[num] % MOD + replace[num]) % MOD;
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 5;
LL digit[10], replace[10];
string original;
int n;
string p[MAX], s;

void init() {
    for(int i = 0; i < 10; ++i) {
        digit[i] = 10;
        replace[i] = i;
    }
}

int main() {
    init();
    cin >> s;
    cin >> n;
    for(int i = 0; i < n; ++i) {
        cin >> p[i];
    }
    for(int i = n - 1; i >= 0; --i) {
        LL tenDigit = 1ll, replaceTo = 0ll;
        for(int j = 3, len = p[i].length(); j < len; ++j) {
            int num = p[i][j] - '0';
            tenDigit = tenDigit * digit[num] % MOD;
            replaceTo = (replaceTo * digit[num] % MOD + replace[num]) % MOD;
        }
        int pNum = p[i][0] - '0';
        replace[pNum] = replaceTo;
        digit[pNum] = tenDigit;
    }
    LL ans = 0ll;
    for(int i = 0, len = s.length(); i < len; ++i) {
        int num = s[i] - '0';
        ans = (ans * digit[num] % MOD + replace[num]) % MOD;
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

template<typename TYPE> inline TYPE sqr(const TYPE& a) { return a * a; }

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

const ld EPS = 1e-9;
const ld PI = 2 * acos(0.0);
const int N = 100;
const int MODULO = 1e9 + 7;

void do_mul(li &tg, li m) {
	tg = (tg * m) % MODULO;
}

void do_add(li &tg, li a) {
	tg = (tg + a) % MODULO;
}

vector<li> rem, mult;

void conv(const string &s, li &r, li &m) {
	r = 0;
	m = 1;
	for (size_t j = 0; j < s.size(); ++j) {
		int d = s[j] - '0';
		do_mul(m, mult[d]);
		do_mul(r, mult[d]);
		do_add(r, rem[d]);
	}
}

int main() {
	string s;
	getline(cin, s);
	int n;
	scanf("%d\n", &n);
	vector<char> qfm;
	vector<string> qto;
	for (int it = 0; it < n; ++it) {
		string q;
		getline(cin, q);
		qfm.push_back(q[0] - '0');
		qto.push_back(q.substr(3));
	}
	for (int i = 0; i < 10; ++i) {
		rem.push_back(i);
		mult.push_back(10);
	}
	li r, m;
	for (int i = n - 1; i >= 0; --i) {
		conv(qto[i], r, m);
		int from = qfm[i];
		rem[from] = r;
		mult[from] = m;
	}
	conv(s, r, m);
	cout << r << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

using ll = long long;
int const N = 2e5 + 10;
ll const M = 1e9 + 7;

ll v[26], l[26];
char s[N], buf[N];
vector <pair<int,string>> tran;

ll powM(ll x, ll e) {
	ll r = 1;
	for (; e; e>>=1,x=x*x%M) if (e&1) r=r*x%M;
	return r;
}

int main() {
	for (int i=0; i<10; ++i) v[i] = i, l[i] = 1;
	int n;
	scanf("%s%d", s, &n), gets(buf);
	for (int i=0; i<n; ++i) {
		gets(buf);
		int len = (int)strlen(buf);
		if (buf[len-1] == '>') {
			tran.push_back({buf[0]-'0', ""});
		} else {
			tran.push_back({buf[0]-'0', string(buf+3)});
		}
	}
	reverse(begin(tran), end(tran));
	for (auto &p: tran) {
		int d = p.first;
		if (p.second.empty()) {
			v[d] = 0, l[d] = 0;
		} else {
			strcpy(buf, p.second.c_str());
			ll cc = 0, le = 0;
			for (int i=0; buf[i]; ++i) {
				int k = buf[i] - '0';
				if (!l[k]) continue;
				ll b = powM(10, l[k]);
				cc = (cc * b + v[k]) % M;
				le = (le + l[k]) % (M - 1);
			}
			v[d] = cc, l[d] = le;
		}
	}
	ll sum = 0;
	for (int i=0; s[i]; ++i) {
		int k = s[i] - '0';
		if (!l[k]) continue;
		ll b = powM(10, l[k]);
		sum = (sum * b + v[k]) % M;
	}
	printf("%d\n", (int)sum);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

using ll = long long;
int const N = 2e5 + 10;
ll const M = 1e9 + 7;

ll v[26], l[26];
char s[N], buf[N];
vector <pair<int,string>> tran;

ll powM(ll x, ll e) {
	if (!e) return 10;
	ll r = 1;
	for (; e; e>>=1,x=x*x%M) if (e&1) r=r*x%M;
	return r;
}

int main() {
	for (int i=0; i<10; ++i) v[i] = i, l[i] = 1;
	int n;
	scanf("%s%d", s, &n), gets(buf);
	for (int i=0; i<n; ++i) {
		gets(buf);
		int len = (int)strlen(buf);
		if (buf[len-1] == '>') {
			tran.push_back({buf[0]-'0', ""});
		} else {
			tran.push_back({buf[0]-'0', string(buf+3)});
		}
	}
	reverse(begin(tran), end(tran));
	for (auto &p: tran) {
		int d = p.first;
		if (p.second.empty()) {
			v[d] = 0, l[d] = 0;
		} else {
			strcpy(buf, p.second.c_str());
			ll cc = 0, le = 0;
			for (int i=0; buf[i]; ++i) {
				int k = buf[i] - '0';
				if (!l[k]) continue;
				ll b = 0;
				if (i) b = powM(10, l[buf[i-1]-'0']);
				cc = (cc * b + v[k]) % M;
				le = (le + l[k]) % (M - 1);
			}
			v[d] = cc, l[d] = le;
		}
	}
	ll sum = 0;
	for (int i=0; s[i]; ++i) {
		int k = s[i] - '0';
		if (!l[k]) continue;
		ll b = 0;
		if (i) b = powM(10, l[s[i-1]-'0']);
		sum = (sum * b + v[k]) % M;
	}
	printf("%d\n", (int)sum);
	return 0;
}


#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <memory.h>
#include <map>
#include <math.h>

using namespace std;

int from[100010];
string v[100010];
int n;
long long mod = 1e9+7;

map<long long, pair<long long,long long> > f(int g) {
    if (g == n+1) {
        map<long long,pair<long long,long long> > mp;
        for(int i = 0 ; i < 10 ; ++i)
            mp[i] = make_pair(10, i);
        return mp;
    }

    auto mp = f(g+1);

    long long res = 0;
    long long step = 1;
    int sg;

    reverse(v[g].begin(), v[g].end());
    for(int i = 0 ; i < v[g].size(); ++i)
    {
        sg = v[g][i] - '0';

        res += (step * mp[sg].second)%mod;
        step = (step * mp[sg].first)%mod;

        res %= mod;
    }

    mp[from[g]] = make_pair(step, res);

    return mp;
}

int main()
{
    ios::sync_with_stdio(0);
    string s;

    cin >> v[0] >> n;
    from[0] = 0;

    string ss, sss;
    for(int i = 0 ; i < n ; ++i)
    {
        cin >> ss;
        from[i+1] = ss[0] - '0';
        sss = "";
        for(int j = 3 ; j < ss.size() ; ++j)
            sss += ss[j];

        v[i+1] = sss;
    }

    auto x = f(0);
    cout << x[0].second << "\n";

    return 0;
}


#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <memory.h>
#include <map>
#include <math.h>

using namespace std;

int from[100010];
string v[100010];
int n;
long long mod = 1e9+7;

map<long long, pair<long long,long long> > f(int g) {
    if (g == n) {
        map<long long,pair<long long,long long> > mp;
        for(int i = 0 ; i < 10 ; ++i)
            mp[i] = make_pair(10, i);
        return mp;
    }

    map<long long,pair<long long,long long> > mp = f(g+1);

    long long res = 0;
    long long step = 1;
    int sg;

    reverse(v[g].begin(), v[g].end());
    for(int i = 0 ; i < v[g].size(); ++i)
    {
        sg = v[g][i] - '0';

        res += (step * mp[sg].second)%mod;
        step = (step * mp[sg].first)%mod;

        res %= mod;
    }
    mp[from[g]] = make_pair(step, res);
    return mp;
}

int main()
{
    ios::sync_with_stdio(0);
    string s;

    cin >> s >> n;

    string ss, sss;
    for(int i = 0 ; i < n ; ++i)
    {
        cin >> ss;
        from[i] = ss[0] - '0';
        sss = "";
        for(int j = 3 ; j < ss.size() ; ++j)
            sss += ss[j];

        v[i] = sss;
    }

    map<long long, pair<long long,long long> > mp = f(0);

    long long res = 0;
    long long step = 1;
    int sg;

    reverse(s.begin(), s.end());
    for(int i = 0 ; i < s.size(); ++i)
    {
        sg = s[i] - '0';

        res += (step * mp[sg].second)%mod;
        step = (step * mp[sg].first)%mod;

        res %= mod;

    }
    cout << res << "\n";

    return 0;
}


#include<cstdio>
#include<cassert>
#include<cstring>
#include<map>
#include<set>
#include<time.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<utility>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
const int MAXN = 100000;
const int MOD = 1000 * 1000 * 1000 + 7;
int len[MAXN+10][10];
int mod[MAXN+10][10];

string s;
int q, n;
int from[MAXN];
string to[MAXN];

long long power(int a, int b) {
  if(b == 0) return 1;
  if(b == 1) return a;
  if(b % 2) return (power(a, b-1) * a) % MOD;
  long long half = power(a, b/2);
  return (half * half) % MOD;
}

int main() {
  cin.sync_with_stdio(false);
  cin >> s >> q;
  for(int i = 0; i < q; ++i) {
    string tmp;
    cin >> tmp;
    from[i] = tmp[0] - '0';
    to[i] = tmp.substr(3);
  }
  for(int i = 0; i < 10; ++i) {
    len[q][i] = 1;
    mod[q][i] = i;
  }

  for(int qq = q-1; qq >= 0; --qq) {
    for(int i = 0; i < 10; ++i) {
      len[qq][i] = 1;
      mod[qq][i] = i;
    }
    int f = from[qq];
    len[qq][f] = 0;
    mod[qq][f] = 0;
    for(char t: to[qq]) {
      len[qq][f] += len[qq+1][t-'0'];
      mod[qq][f] = ((long long)mod[qq][f] * power(10, len[qq+1][t-'0']) + mod[qq+1][t-'0']) % MOD;
    }
  }
  long long ret = 0;
  for(char t: s) {
    ret = (ret * power(10, len[0][t-'0']) + mod[0][t-'0']) % MOD;
  }
  cout << ret << "\n";
}


#include<cstdio>
#include<cassert>
#include<cstring>
#include<map>
#include<set>
#include<time.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<utility>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
const int MAXN = 100000;
const int MOD = 1000 * 1000 * 1000 + 7;
int powlen[MAXN+10][10];
int mod[MAXN+10][10];

string s;
int q, n;
int from[MAXN];
string to[MAXN];

long long power(int a, long long b) {
  if(b == 0) return 1;
  if(b == 1) return a;
  if(b < 0) {
    printf("shit\n");
  }
  if(b % 2) return (power(a, b-1) * a) % MOD;
  long long half = power(a, b/2);
  return (half * half) % MOD;
}

int main() {
  cin.sync_with_stdio(false);
  cin >> s >> q;
  for(int i = 0; i < q; ++i) {
    string tmp;
    cin >> tmp;
    from[i] = tmp[0] - '0';
    to[i] = tmp.substr(3);
  }
  for(int i = 0; i < 10; ++i) {
    powlen[q][i] = 10;
    mod[q][i] = i;
  }

  for(int qq = q-1; qq >= 0; --qq) {
    for(int i = 0; i < 10; ++i) {
      powlen[qq][i] = powlen[qq+1][i];
      mod[qq][i] = mod[qq+1][i];
    }
    int f = from[qq];
    powlen[qq][f] = 1;
    mod[qq][f] = 0;
    for(char t: to[qq]) {
      powlen[qq][f] = ((long long)powlen[qq][f] * (long long)powlen[qq+1][t-'0']) % MOD;
      mod[qq][f] = ((long long)mod[qq][f] * powlen[qq+1][t-'0'] + mod[qq+1][t-'0']) % MOD;
    }
  }
  long long ret = 0;
  for(char t: s) {
    ret = (ret * powlen[0][t-'0'] + mod[0][t-'0']) % MOD;
  }
  cout << ret << "\n";
}


#include<cstdio>
#include<cassert>
#include<cstring>
#include<map>
#include<set>
#include<time.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<utility>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
const int MAXN = 100000;
const int MOD = 1000 * 1000 * 1000 + 7;
long long len[MAXN+10][10];
int mod[MAXN+10][10];

string s;
int q, n;
int from[MAXN];
string to[MAXN];

long long power(int a, long long b) {
  if(b == 0) return 1;
  if(b == 1) return a;
  if(b % 2) return (power(a, b-1) * a) % MOD;
  long long half = power(a, b/2);
  return (half * half) % MOD;
}

int main() {
  cin.sync_with_stdio(false);
  cin >> s >> q;
  for(int i = 0; i < q; ++i) {
    string tmp;
    cin >> tmp;
    from[i] = tmp[0] - '0';
    to[i] = tmp.substr(3);
  }
  for(int i = 0; i < 10; ++i) {
    len[q][i] = 1;
    mod[q][i] = i;
  }

  for(int qq = q-1; qq >= 0; --qq) {
    for(int i = 0; i < 10; ++i) {
      len[qq][i] = len[qq+1][i];
      mod[qq][i] = mod[qq+1][i];
    }
    int f = from[qq];
    len[qq][f] = 0;
    mod[qq][f] = 0;
    for(char t: to[qq]) {
      len[qq][f] += len[qq+1][t-'0'];
      mod[qq][f] = ((long long)mod[qq][f] * power(10, len[qq+1][t-'0']) + mod[qq+1][t-'0']) % MOD;
    }
  }
  long long ret = 0;
  for(char t: s) {
    ret = (ret * power(10, len[0][t-'0']) + mod[0][t-'0']) % MOD;
  }
  cout << ret << "\n";
}


#include<cstdio>
#include<cassert>
#include<cstring>
#include<map>
#include<set>
#include<time.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<utility>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
const int MAXN = 100000;
const int MOD = 1000 * 1000 * 1000 + 7;
int len[MAXN+10][10];
int mod[MAXN+10][10];

string s;
int q, n;
int from[MAXN];
string to[MAXN];

long long power(int a, int b) {
  if(b == 0) return 1;
  if(b == 1) return a;
  if(b % 2) return (power(a, b-1) * a) % MOD;
  long long half = power(a, b/2);
  return (half * half) % MOD;
}

int main() {
  cin.sync_with_stdio(false);
  cin >> s >> q;
  for(int i = 0; i < q; ++i) {
    string tmp;
    cin >> tmp;
    from[i] = tmp[0] - '0';
    to[i] = tmp.substr(3);
  }
  for(int i = 0; i < 10; ++i) {
    len[q][i] = 1;
    mod[q][i] = i;
  }

  for(int qq = q-1; qq >= 0; --qq) {
    for(int i = 0; i < 10; ++i) {
      len[qq][i] = len[qq+1][i];
      mod[qq][i] = mod[qq+1][i];
    }
    int f = from[qq];
    len[qq][f] = 0;
    mod[qq][f] = 0;
    for(char t: to[qq]) {
      len[qq][f] += len[qq+1][t-'0'];
      mod[qq][f] = ((long long)mod[qq][f] * power(10, len[qq+1][t-'0']) + mod[qq+1][t-'0']) % MOD;
    }
  }
  long long ret = 0;
  for(char t: s) {
    ret = (ret * power(10, len[0][t-'0']) + mod[0][t-'0']) % MOD;
  }
  cout << ret << "\n";
}


#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <bitset>
#include <time.h>
#include <cmath>
#include <string>
#include <deque>

using namespace std;

typedef long long ll;
const ll mod = 1e9+7;

ll dp[200010][20];
ll len[200010][20];

ll binpow (ll a, ll n) {
	ll res = 1;
	while (n) {
		if (n & 1)
		{
			res *= a;
			res%=mod;
		}
		a *= a;
		a%=mod;
		n >>= 1;
	}
	return res%mod;
}

int main(){

	string str;
	cin>>str;
	ll n;
	cin>>n;
	vector<pair<ll, string> > v(n);
	for(ll i=0; i<n; ++i)
	{
		string tmp;
		cin>>tmp;
		v[i]= make_pair(tmp[0]-'0', tmp.substr(3));
	}
	for(ll j=0; j<=9; ++j)
	{
		dp[n][j] = j;
		len[n][j] = 1;
	}
	for(ll i=n-1;i>=0; i--)
	{
		for(ll j=0; j<=9; ++j)
		{
				dp[i][j] = dp[i+1][j];
				len[i][j] = len[i+1][j];
		}

		ll ind = v[i].first;
		string tmp = v[i].second;
		ll cur=0;
		ll curlen =0;
		for(ll q = 0; q<tmp.size(); ++q)
		{
			ll st = binpow(10,len[i+1][tmp[q]-'0']);
			cur*=st;
			cur%=mod;
			cur+=dp[i+1][tmp[q]-'0'];
			curlen+=len[i+1][tmp[q]-'0'];
			cur%=mod;
		}
		dp[i][ind]=cur;
		len[i][ind] = curlen;

	}
	ll cur = 0;
	for(ll i=0; i<str.size(); ++i)
	{
		ll st = binpow(10,len[0][str[i]-'0']);
		cur*=st;
		cur%=mod;
		cur+=dp[0][str[i]-'0'];
		cur%=mod;
	}
	cout<<cur;
	return 0;
}

/*
999
3
9->9
9->9
9->9

011223011223

*/


#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <bitset>
#include <time.h>
#include <cmath>
#include <string>
#include <deque>

using namespace std;

typedef long long ll;
const ll mod = 1e9+7;

ll dp[20][200010];
ll len[20][200010];

ll binpow (ll a, ll n) {
	ll res = 1;
	while (n) {
		if (n & 1)
		{
			res *= a;
			res%=mod;
		}
		a *= a;
		a%=mod;
		n >>= 1;
	}
	return res%mod;
}

int main(){

	string str;
	cin>>str;
	ll n;
	cin>>n;
	vector<pair<ll, string> > v(n);
	for(ll i=0; i<n; ++i)
	{
		string tmp;
		cin>>tmp;
		v[i]= make_pair(tmp[0]-'0', tmp.substr(3));
	}
	for(ll j=0; j<=9; ++j)
	{
		dp[n][j] = j;
		len[n][j] = 1;
	}
	for(ll i=n-1;i>=0; i--)
	{
		for(ll j=0; j<=9; ++j)
		{
				dp[i][j] = dp[i+1][j];
				len[i][j] = len[i+1][j];
		}

		ll ind = v[i].first;
		string tmp = v[i].second;
		ll cur=0;
		ll curlen =0;
		for(ll q = 0; q<tmp.size(); ++q)
		{
			ll st = binpow(10,len[i+1][tmp[q]-'0']);
			cur*=st;
			cur%=mod;
			cur+=dp[i+1][tmp[q]-'0'];
			curlen+=len[i+1][tmp[q]-'0'];
			cur%=mod;
		}
		dp[i][ind]=cur;
		len[i][ind] = curlen;

	}
	ll cur = 0;
	for(ll i=0; i<str.size(); ++i)
	{
		ll st = binpow(10,len[0][str[i]-'0']);
		cur*=st;
		cur%=mod;
		cur+=dp[0][str[i]-'0'];
		cur%=mod;
	}
	cout<<cur;
	return 0;
}

/*
999
3
9->9
9->9
9->9

011223011223

*/


#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <bitset>
#include <time.h>
#include <cmath>
#include <string>
#include <deque>

using namespace std;

typedef long long ll;
const ll mod = 1e9+7;

ll dp[200010][20];
ll len[200010][20];

ll binpow (ll a, ll n) {
	ll res = 1;
	while (n) {
		if (n & 1)
		{
			res *= a;
			res%=mod;
		}
		a *= a;
		a%=mod;
		n >>= 1;
	}
	return res%mod;
}

int main(){

	string str;
	cin>>str;
	ll n;
	cin>>n;
	vector<pair<ll, string> > v(n);
	for(ll i=0; i<n; ++i)
	{
		string tmp;
		cin>>tmp;
		v[i]= make_pair(tmp[0]-'0', tmp.substr(3));
	}
	for(ll j=0; j<=9; ++j)
	{
		dp[n][j] = j;
		len[n][j] = 10;
	}
	for(ll i=n-1;i>=0; i--)
	{
		for(ll j=0; j<=9; ++j)
		{
				dp[i][j] = dp[i+1][j];
				len[i][j] = len[i+1][j];
		}

		ll ind = v[i].first;
		string tmp = v[i].second;
		ll cur=0;
		ll curlen =1;
		for(ll q = 0; q<tmp.size(); ++q)
		{

			cur*=len[i+1][tmp[q]-'0'];
			cur%=mod;
			cur+=dp[i+1][tmp[q]-'0'];
			curlen*=len[i+1][tmp[q]-'0'];
			cur%=mod;
			curlen%=mod;
		}
		dp[i][ind]=cur;
		len[i][ind] = curlen;

	}
	ll cur = 0;
	for(ll i=0; i<str.size(); ++i)
	{
		//ll st = binpow(10,len[0][str[i]-'0']);
		cur*=len[0][str[i]-'0'];
		cur%=mod;
		cur+=dp[0][str[i]-'0'];
		cur%=mod;
	}
	cout<<cur;
	return 0;
}

/*
999
3
9->9
9->9
9->9

011223011223

*/


//by tzupengwang™

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<assert.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<queue>
#include<string>
#include<map>
using namespace std;

#define FO(it,c) for (__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define mod (1000000007LL)
typedef long long ll;
typedef pair<int,int> ii;

int n ;
char s[ 100005 ];
char q[ 100005 ][2];
string qs[ 100005 ];

void init() {
	char tmp[100005];
	scanf("%s%d" , s , &n );
	for ( int i = 0 ; i < n ; i ++ ) {
		scanf( "%1s->" , q[ i ] );
		gets( tmp ) ;
		qs[ i ] = tmp ;
	}
}

ll mpow( int x ) {
	if ( x == 0 ) return 1 ;
	if ( x == 1 ) return 10 ;
	ll tmp = mpow( x / 2 ) ;
	tmp = ( tmp * tmp ) % mod ;
	if ( x % 2 == 1 ) tmp = ( tmp * 10 ) % mod ;
	return tmp ;
}

ll value[ 15 ] , len[ 15 ] ;

void process() {
	for ( int i = 0 ; i < 10 ; i ++ ) {
		value[ i ] = i ;
		len[ i ] = 10 ;
	}
	for ( int k = n -1 ; k >= 0 ; k -- ) {
		ll tv = 0 , tl = 1 ;
		int ln = qs[ k ].size() ;
		for ( int i = ln - 1 ; i >= 0 ; i -- ) {
			tv = (tv + ( value[ qs[k][i] - '0' ] * tl %mod ) % mod) ;
			tl = (tl * len[ qs[k][i] -'0' ] ) % mod ;
		}
		value[ q[k][0] -'0'] = tv;
		len[ q[k][0] -'0'] = tl ;
	}

	ll tv = 0 , tl = 1 ;
	int ln = strlen(s) ;
	for ( int i = ln - 1 ; i >= 0 ; i -- ) {
		tv = ( tv + ( value[ s[i] - '0' ] * tl % mod ) ) % mod ;
		tl = (tl * len[ s[i] -'0' ] ) % mod;
	}
	printf("%I64d\n",tv);

}

int main() {
/*
#ifdef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif
*/
	init();
	process();
	return 0;
}


//by tzupengwang™

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<assert.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<queue>
#include<string>
#include<map>
using namespace std;

#define FO(it,c) for (__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define mod (1000000007LL)
typedef long long ll;
typedef pair<int,int> ii;

int n ;
char s[ 100005 ];
char q[ 100005 ][2];
string qs[ 100005 ];

void init() {
	char tmp[100005];
	scanf("%s%d" , s , &n );
	for ( int i = 0 ; i < n ; i ++ ) {
		scanf( "%1s->" , q[ i ] );
		gets( tmp ) ;
		qs[ i ] = tmp ;
	}
}

ll value[ 15 ] , len[ 15 ] ;

void process() {
	for ( int i = 0 ; i < 10 ; i ++ ) {
		value[ i ] = i ;
		len[ i ] = 10 ;
	}
	for ( int k = n -1 ; k >= 0 ; k -- ) {
		ll tv = 0 , tl = 1 ;
		int ln = qs[ k ].size() ;
		for ( int i = ln - 1 ; i >= 0 ; i -- ) {
			tv = (tv + ( value[ qs[k][i] - '0' ] * tl ) % mod ) % mod ;
			tl = (tl * len[ qs[k][i] -'0' ] ) % mod ;
		}
		value[ q[k][0] -'0'] = tv;
		len[ q[k][0] -'0'] = tl ;
	}

	ll tv = 0 , tl = 1 ;
	int ln = strlen(s) ;
	for ( int i = ln - 1 ; i >= 0 ; i -- ) {
		tv = ( tv + ( ( value[ s[i] - '0' ] * tl ) % mod ) ) % mod ;
		tl = (tl * len[ s[i] -'0' ] ) % mod;
	}
	printf("%I64d\n",tv);

}

int main() {
/*
#ifdef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif
*/
	init();
	process();
	return 0;
}


//by tzupengwang™

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<assert.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<queue>
#include<string>
#include<map>
using namespace std;

#define FO(it,c) for (__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define mod (1000000007LL)
typedef long long ll;
typedef pair<int,int> ii;

int n ;
char s[ 100005 ];
char q[ 100005 ][2];
string qs[ 100005 ];

void init() {
	char tmp[100005];
	scanf("%s%d" , s , &n );
	for ( int i = 0 ; i < n ; i ++ ) {
		scanf( "%1s->" , q[ i ] );
		gets( tmp ) ;
		qs[ i ] = tmp ;
	}
}

ll value[ 15 ] , len[ 15 ] ;

void process() {
	for ( int i = 0 ; i < 10 ; i ++ ) {
		value[ i ] = i ;
		len[ i ] = 10 ;
	}
	for ( int k = n -1 ; k >= 0 ; k -- ) {
		ll tv = 0 , tl = 1 ;
		int ln = qs[ k ].size() ;
		for ( int i = ln - 1 ; i >= 0 ; i -- ) {
			tv = (tv + ( ( value[ qs[k][i] - '0' ] * tl ) % mod ) % mod) ;
			tl = (tl * len[ qs[k][i] -'0' ] ) % mod ;
		}
		value[ q[k][0] -'0'] = tv;
		len[ q[k][0] -'0'] = tl ;
	}

	ll tv = 0 , tl = 1 ;
	int ln = strlen(s) ;
	for ( int i = ln - 1 ; i >= 0 ; i -- ) {
		tv = ( tv + ( ( value[ s[i] - '0' ] * tl ) % mod ) ) % mod ;
		tl = (tl * len[ s[i] -'0' ] ) % mod;
	}
	printf("%I64d\n",tv);

}

int main() {
/*
#ifdef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif
*/
	init();
	process();
	return 0;
}


//by tzupengwang™

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<assert.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<queue>
#include<string>
#include<map>
using namespace std;

#define FO(it,c) for (__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define mod (1000000007LL)
typedef long long ll;
typedef pair<int,int> ii;

int n ;
char s[ 100005 ];
char q[ 100005 ][2];
string qs[ 100005 ];

void init() {
	char tmp[100005];
	scanf("%s%d" , s , &n );
	for ( int i = 0 ; i < n ; i ++ ) {
		scanf( "%1s->" , q[ i ] );
		gets( tmp ) ;
		qs[ i ] = tmp ;
	}
}

ll mpow( int x ) {
	if ( x == 0 ) return 1 ;
	if ( x == 1 ) return 10 ;
	ll tmp = mpow( x / 2 ) ;
	tmp = ( tmp * tmp ) % mod ;
	if ( x % 2 == 1 ) tmp = ( tmp * 10 ) % mod ;
	return tmp ;
}

ll value[ 15 ] , len[ 15 ] ;

void process() {
	for ( int i = 0 ; i < 10 ; i ++ ) {
		value[ i ] = i ;
		len[ i ] = 1 ;
	}
	for ( int k = n -1 ; k >= 0 ; k -- ) {
		ll tv = 0 , tl = 0 ;
		int ln = qs[ k ].size() ;
		for ( int i = ln - 1 ; i >= 0 ; i -- ) {
			tv = (tv + ( value[ qs[k][i] - '0' ] * mpow( tl ) %mod ) % mod) ;
			tl += len[ qs[k][i] -'0' ] ;
		}
		value[ q[k][0] -'0'] = tv;
		len[ q[k][0] -'0'] = tl ;
	}

	ll tv = 0 , tl = 0 ;
	int ln = strlen(s) ;
	for ( int i = ln - 1 ; i >= 0 ; i -- ) {
		tv = ( tv + ( value[ s[i] - '0' ] * mpow( tl ) % mod ) ) % mod ;
		tl += len[ s[i] -'0' ] ;
	}
	printf("%I64d\n",tv);

}

int main() {
/*
#ifdef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif
*/
	init();
	process();
	return 0;
}


#include <cstdio>
#include <string>
#include <cstdlib>
#include <functional>
#include <vector>
#include <set>
#include <map>
#include <iomanip>
#include <queue>
#include <iostream>
#include <algorithm>

using namespace std;
typedef pair<int, int> pr;
typedef pair<pr, pr> prr;
typedef pair<int, pr> edge;

typedef long double ld;
typedef long long ll;
#define MP make_pair
const int inf = -2000000000;
const ll mod7 = 1000000007;
int gcd(int a, int b)
{
    while (b)
    {
        a %= b;
        swap(a, b);
    }
    return a;
}

pair<ll, ll> Calc[11];

string qu[100001];

int main()
{
    string str; cin >> str;
    int n; cin >> n;

    for (int i = 0; i < 10; ++i)
        Calc[i].first = Calc[i].second = -1;

    for (int i = 0; i < n; ++i)
    {
        cin >> qu[i];
    }

    for (int i = n - 1; i >= 0; --i)
    {
        pair<ll, ll> data;
        ll sum = 0, ddd = 1;
        for (int j = 3; j < qu[i].length(); ++j)
        {
            int C = qu[i][j] - '0';
            if (Calc[C].first != -1)
            {
                sum *= Calc[C].second; sum %= mod7;
                sum += Calc[C].first; sum %= mod7;
                ddd *= Calc[C].second; ddd %= mod7;
            }
            else
            {
                sum *= 10; sum += C; sum %= mod7;
                ddd *= 10; ddd %= mod7;
            }
        }
        Calc[qu[i][0] - '0'] = MP(sum, ddd);
    }
    ll sum = 0, ddd = 1;
    for (int j = 0; j < str.length(); ++j)
    {
        int C = str[j] - '0';
        if (Calc[C].first != -1)
        {
            sum *= Calc[C].second; sum %= mod7;
            sum += Calc[C].first; sum %= mod7;
            ddd *= Calc[C].second; ddd %= mod7;
        }
        else
        {
            sum *= 10; sum += C; sum %= mod7;
            ddd *= 10; ddd %= mod7;
        }
    }

    cout << sum;
    return 0;
}


#include <bits\stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef double dbl;

void Assert(bool x)
{
    if (!x)
        printf("%d\n", 1 / 0);
}

void solve(int n)
{
    int k;
    scanf(" %d", &k);

    const int bound = 500;

    vector<long double> prev_layer(bound, 0);
    vector<long double> cur_layer(bound);

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j < bound; j++)
        {
            cur_layer[j] = j * prev_layer[j];
            if (j + 1 != bound)
                cur_layer[j] += prev_layer[j + 1];
            cur_layer[j] += j * (j + 3) / 2;
            cur_layer[j] /= k * (j + 1);

            cur_layer[j] += (k - 1) * prev_layer[j] / k;
        }

//        if (n == 10)
//        {
//            for (auto x : prev_layer)
//                cerr << x << ' ';
//            cerr << endl;
//        }

        prev_layer = cur_layer;
    }

    printf("%.20f\n", (double)k * (double)cur_layer[1]);
}

int main()
{
    int i, j;

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int n, m;

    while (cin >> n)
        solve(n);
}


#include <bits\stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef double dbl;

void Assert(bool x)
{
    if (!x)
        printf("%d\n", 1 / 0);
}

ll powmod(ll x, ll y, ll mod)
{
    ll res = 1LL;
    while (y)
    {
        if (y & 1)
        {
            y--;
            res *= x;
            if (res >= mod)
                res %= mod;
        }
        else
        {
            y >>= 1;
            x *= x;
            if (x >= mod)
                x %= mod;
        }
    }

    return res;
}

void solve(string s)
{
    vector<pair<int, string> > opers;

    char str[(int)1e5 + 10];

    int n;
    scanf(" %d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf(" %s", str);
        string temp(str);

        opers.pb(mp(temp[0] - '0', temp.substr(3)));
    }

    reverse(opers.begin(), opers.end());
    opers.pb(mp(0, s));

    const int digits = 10;
    const ll mod = (ll)1e9 + 7;

    vector<ll> Len(digits), Val(digits);

    for (int i = 0; i < digits; i++)
        Val[i] = i, Len[i] = 1;

    for (int i = 0; i < (int)opers.size(); i++)
    {
        int d = opers[i].first;
        string pattern = opers[i].second;

        ll newlen = 0;
        ll newval = 0;
        for (int j = 0; j < (int)pattern.size(); j++)
        {
            newlen += Len[pattern[j] - '0'];
            if (newlen >= mod - 1)
                newlen -= mod - 1;
        }

        ll curpow = 1LL;
        for (auto it = pattern.rbegin(); it != pattern.rend(); it++)
        {
            int curd = (*it) - '0';

            newval += Val[curd] * curpow;
            newval %= mod;
            curpow *= powmod(10, Len[curd], mod);
            curpow %= mod;
        }

        Val[d] = newval;
        Len[d] = newlen;
    }

    cout << Val[0] << endl;
}

int main()
{
    int i, j;

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    int n, m;

    string s;
    while (cin >> s)
        solve(s);
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <bitset>
#include <memory>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <string>
#include <cstring>
#include <fstream>
#include <functional>
#include <stack>
#include <complex>
#include <wchar.h>
#include <wctype.h>
#include <cmath>
#include <queue>
#include <ctime>
#include <numeric>
#include <bitset>
#include <cassert>

using namespace std;

template<typename T> T mabs(const T &a){ return a<0?-a:a;}
#define rep(x,y,z) for(int x=(y),e##x=(z);x<e##x;x++)
#define SQR(x) ((x)*(x))
#define all(c) (c).begin(), (c).end()

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef short int si;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double, int> pdi;
typedef pair<double, double> pdd;

const int mod = 1e9 + 7;

ll toPow(ll a, int b)
{
    ll res = 1;
    while (b)
    {
        if (b&1)
            res = (res*a)%mod;
        a = (a*a)%mod;
        b>>=1;
    }
    return res;
}

void run()
{
    char str[100100];
    scanf("%s",str);
    int vcnt = 0;
    int n = strlen(str);
    reverse(str, str+n);
    vector<pii> V[10];
    vector<pii> A[100100];
    int Len[100100];
    A[0].resize(n);
    Len[0] = n;
    rep(i,0,n)
    {
        A[0][i] = pii(0, str[i]-'0');
        V[str[i]-'0'].push_back(pii(0,i));
    }
    int q;
    cin>>q;
    char S[100100];
    rep(i,0,q)
    {
        char d;
        scanf("%s",S);
        d = S[0];
        char *str = S+3;
        Len[i+1] = strlen(str);
        reverse(str, str+Len[i+1]);
        A[i+1].resize(Len[i+1]);
        rep(j,0,Len[i+1])
            A[i+1][j] = pii(0, str[j] - '0');
        d-='0';
        rep(j,0,V[d].size())
        {
            pii p = V[d][j];
            A[p.first][p.second] = pii(1, i+1);
        }
        V[d].clear();
        rep(j,0,Len[i+1])
                V[str[j]-'0'].push_back(pii(i+1,j));
    }
    int Val[100100] = {0};
    int Cnt[100100] = {0};
    for (int i = q;i>=0;i--)
    {
        ll less = 0;
        ll sum = 0;
        rep(j,0,Len[i])
        {
            ll nxt = A[i][j].first == 0 ? A[i][j].second : Val[A[i][j].second];
            sum = (sum + toPow(10, less) * nxt) % mod;
            ll cnt = A[i][j].first == 0 ? 1 : Cnt[A[i][j].second];
            less = (less + cnt) % (mod - 1);
        }
        Val[i] = sum;
        Cnt[i] = less;
    }
    cout<<Val[0]<<endl;
}

//#define prob "fields"

int main()
{
#ifdef _MONYURA_
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
    time_t st=clock();
#else
#ifdef prob
    freopen(prob".in","r",stdin);
    freopen(prob".out","w",stdout);
#endif
#endif
    run();
#ifdef _MONYURA_
    printf( "\n=============\n");
    printf("Time: %.2lf sec\n",(clock()-st)/double(CLOCKS_PER_SEC));
#endif

    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

typedef long long ll;
const int N = 100010;
const int Mod = 1e9 + 7;

string s[N], S;
int d[10], pw[10], n;

int main() {
    cin >> S;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> s[i];
    for (int i = 0; i < 10; i++) d[i] = i, pw[i] = 10;
    for (int i = n - 1; i >= 0; i--) {
        int p = s[i][0] - '0';
        ll cur = 0, np = 1;
        for (int j = s[i].length() - 1; j >= 3; j--) {
            int k = s[i][j] - '0';
            cur = (cur + np*d[k]) % Mod;
            np = np * pw[k] % Mod;
        }
        d[p] = cur;
        pw[p] = np;
    }
    ll ans = 0, np = 1;
    for (int i = S.length() - 1; i >= 0; i--) {
        int k = S[i] - '0';
        ans = (ans + np*d[k]) % Mod;
        np = (np*pw[k]) % Mod;
    }
    cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
using namespace std;

const int Mod = 1e9 + 7;
vector<pair<char, string>> p;
map<char, string>M;
string S, s;

int main() {
    cin >> S;
    int n; cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> s;
        p.push_back({ s[0], s.substr(3) });
    }
    for (int i = n - 1; i >= 0; i--) {
        char x = p[i].first;
        string y = p[i].second;
        string o = "";
        for (int j = 0; j < y.length(); j++) {
            if (M.find(y[j]) != M.end()) {
                o += M[y[j]];
            }
            else o += y[j];
        }
        M[x] = o;
    }
    long long cur = 0;
    for (int i = 0; i < S.length(); i++) {
        if (M.find(S[i]) != M.end()) {
            for (int j = 0; j < M[S[i]].length(); j++) {
                cur = cur * 10 + (M[S[i]][j]-'0');
                cur %= Mod;
            }
        }
        else cur = cur * 10 + (S[i] - '0');
        cur %= Mod;
    }
    cout << cur << endl;
}


#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

typedef long long ll;
const int N = 100010;
const int Mod = 1e9 + 7;

string s[N], S;
int d[10], pw[10], n;

int main() {
    cin >> S;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> s[i];
    for (int i = 0; i < 10; i++) d[i] = i, pw[i] = 10;
    for (int i = n - 1; i >= 0; i--) {
        int p = s[i][0] - '0';
        ll cur = 0, np = 1;
        for (int j = 3; j < s[i].length(); j++) {
            int k = s[i][j] - '0';
            np = np*pw[k] % Mod;
            cur = (cur*pw[k] + d[k]) % Mod;
        }
        d[p] = cur;
        pw[p] = np;
    }
    ll ans = 0;
    for (int i = 0; i < S.length(); i++) {
        int k = S[i] - '0';
        ans = (ans*pw[k] + d[k]) % Mod;
    }
    cout << ans << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>
using namespace std;

const int Mod = 1e9 + 7;
const int N = 1111111;
char s[N];

long long d[10], pw[10];
int n;
string t[N];

int main() {
    scanf("%s", s);
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        char p[111111];
        scanf("%s", p);
        t[i] = p;
    }
    for (int i = 0; i < 10; i++) d[i] = i, pw[i] = 10;
    for (int i = n - 1; i >= 0; i--) {
        int pos = t[i][0] - '0';
        long long cur = 0, ten = 1;
        for (int j = t[i].length() - 1; j >= 3; j--) {
            int k = t[i][j] - '0';
            cur = cur + d[k] * ten;
            cur %= Mod;
            ten = ten * pw[k] % Mod;
        }
        d[pos] = cur;
        pw[pos] = ten;
    }
    long long ans = 0, p = 1;
    for (int i = strlen(s) - 1; i >= 0; i--) {
        ans = (ans + d[s[i] - '0'] * p) % Mod;
        p = (p*pw[s[i] - '0']) % Mod;
    }
    printf("%d\n", ans);
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
using namespace std;

const int Mod = 1e9 + 7;
vector<pair<char, string>> p;
map<char, string>M;
string S, s;

int main() {
    cin >> S;
    int n; cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> s;
        p.push_back({ s[0], s.substr(3) });
    }
    for (int i = n - 1; i >= 0; i--) {
        char x = p[i].first;
        string y = p[i].second;
        string o = "";
        for (int j = 0; j < y.length(); j++) {
            if (M.find(y[j]) != M.end()) {
                o += M[y[j]];
            }
            else o += y[j];
        }
        M[x] = o;
    }
    long long cur = 0;
    for (int i = 0; i < S.length(); i++) {
        if (M.find(S[i]) != M.end()) {
            for (int j = 0; j < M[S[i]].length(); j++) {
                cur = cur * 10 + (M[S[i]][j]-'0');
            }
        }
        else cur = cur * 10 + (S[i] - '0');
        cur %= Mod;
    }
    cout << cur << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

const int Mod = 1e9 + 7;
const int N = 1111111;
char s[N];

long long d[10], pw[10];
int n;
string t[N];

int main() {
    scanf("%s", s);
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        char p[111111];
        scanf("%s", p);
        t[i] = p;
    }
    for (int i = 0; i < 10; i++) d[i] = i, pw[i] = 10;
    for (int i = n - 1; i >= 0; i--) {
        int pos = t[i][0] - '0';
        long long cur = 0, ten = 1;
        for (int j = t[i].length() - 1; j >= 3; j--) {
            int k = t[i][j] - '0';
            cur = cur + d[k] * ten;
            cur %= Mod;
            ten = ten * pw[k] % Mod;
        }
        d[pos] = cur;
        pw[pos] = ten;
    }
    long long ans = 0, p = 1;
    for (int i = strlen(s) - 1; i >= 0; i--) {
        ans = (ans + d[s[i] - '0'] * p) % Mod;
        p = (p*pw[s[i] - '0']) % Mod;
    }
    printf("%d\n", ans);
}


#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

typedef long long ll;
const int N = 100010;
const int Mod = 1e9 + 7;

string s[N], S;
int d[10], pw[10], n;

int main() {
    cin >> S;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> s[i];
    for (int i = 0; i < 10; i++) d[i] = i, pw[i] = 10;
    for (int i = n - 1; i >= 0; i--) {
        int p = s[i][0] - '0';
        ll cur = 0, np = 1;
        for (int j = 3; j < s[i].length(); j++) {
            int k = s[i][j] - '0';
            cur = cur * np + d[k];
            cur %= Mod;
            np = np * 10 % Mod;
        }
        d[p] = cur;
        pw[p] = np;
    }
    ll ans = 0, np = 1;
    for (int i = 0; i < S.length(); i++) {
        int k = S[i] - '0';
        ans = (ans*pw[k] + d[k]) % Mod;
    }
    cout << ans << endl;
}


#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

const int N = 1e5+10;
const int D = 10;
const int MOD = 1e9+7;

struct Query {
  int d;
  char* t;

  ~Query()
  {
    free(t);
  }

  void Input()
  {
    static char foo[N];
    scanf("%1d->%s", &d, foo);
    t = strdup(foo);
  }
};

struct Digit {
  int v, p;
};

int Add(int a, int b)
{
  return (a+b)%MOD;
}

int Mul(int a, int b)
{
  return 1ll*a*b%MOD;
}

void Fold(char* s, Digit* ds, int& v, int& p)
{
  int v_ = 0;
  int p_ = 1;
  for (int i = 0; s[i]; i++) {
    int si = s[i]-'0';
    v_ = Add(Mul(v_, ds[si].p), ds[si].v);
    p_ = Mul(p_, ds[si].p);
  }

  v = v_;
  p = p_;
}

int main()
{
  char s[N];
  scanf("%s", s);

  int n;
  scanf("%d", &n);

  Query qs[N];
  for (int i = 0; i < n; i++)
      qs[i].Input();

  Digit ds[D];
  for (int i = 0; i < D; i++) {
    ds[i].v = i;
    ds[i].p = 10;
  }

  for (int i = n-1; i >= 0; i--) {
    int d = qs[i].d;
    Fold(qs[i].t, ds, ds[d].v, ds[d].p);
  }

  int result, power;
  Fold(s, ds, result, power);
  printf("%d\n", result);

  return 0;
}


#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

const int N = 1e5+10;
const int D = 10;
const int MOD = 1e9+7;

struct Query {
  int d;
  string t;

  void Input()
  {
    static char foo[N];
    scanf("%s", foo);
    d = foo[0]-'0';
    t = foo+3;
  }
};

struct Digit {
  int v, p;
};

int Add(int a, int b)
{
  return (a+b)%MOD;
}

int Mul(int a, int b)
{
  return 1ll*a*b%MOD;
}

void Fold(const char* s, Digit* ds, int& v, int& p)
{
  int v_ = 0;
  int p_ = 1;
  for (int i = 0; s[i]; i++) {
    int si = s[i]-'0';
    v_ = Add(Mul(v_, ds[si].p), ds[si].v);
    p_ = Mul(p_, ds[si].p);
  }

  v = v_;
  p = p_;
}

int main()
{
  char s[N];
  scanf("%s", s);

  int n;
  scanf("%d", &n);

  Query qs[N];
  for (int i = 0; i < n; i++)
      qs[i].Input();

  Digit ds[D];
  for (int i = 0; i < D; i++) {
    ds[i].v = i;
    ds[i].p = 10;
  }

  for (int i = n-1; i >= 0; i--) {
    int d = qs[i].d;
    Fold(qs[i].t.data(), ds, ds[d].v, ds[d].p);
  }

  int result, power;
  Fold(s, ds, result, power);
  printf("%d\n", result);

  return 0;
}


#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

const int N = 1e5+10;
const int D = 10;
const int MOD = 1e9+7;

struct Query {
  int d;
  string t;

  void Input()
  {
    static char foo[N];
    scanf("%1d->%s", &d, foo);
    t = foo;
  }
};

struct Digit {
  int v, p;
};

int Add(int a, int b)
{
  return (a+b)%MOD;
}

int Mul(int a, int b)
{
  return 1ll*a*b%MOD;
}

void Fold(const char* s, Digit* ds, int& v, int& p)
{
  int v_ = 0;
  int p_ = 1;
  for (int i = 0; s[i]; i++) {
    int si = s[i]-'0';
    v_ = Add(Mul(v_, ds[si].p), ds[si].v);
    p_ = Mul(p_, ds[si].p);
  }

  v = v_;
  p = p_;
}

int main()
{
  char s[N];
  scanf("%s", s);

  int n;
  scanf("%d", &n);

  Query qs[N];
  for (int i = 0; i < n; i++)
      qs[i].Input();

  Digit ds[D];
  for (int i = 0; i < D; i++) {
    ds[i].v = i;
    ds[i].p = 10;
  }

  for (int i = n-1; i >= 0; i--) {
    int d = qs[i].d;
    Fold(qs[i].t.data(), ds, ds[d].v, ds[d].p);
  }

  int result, power;
  Fold(s, ds, result, power);
  printf("%d\n", result);

  return 0;
}