#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const int MOD = 1e9 + 7;
string s, x[MAXN];
int a[MAXN];
long long len[15];
int val[15], pw[15];
int n;
int qpow(int a, long long b) {
  if (b < 0) {
    exit(0);
  }
  int ret = 1;
  while (b) {
    if (b & 1) ret = 1ll * ret * a % MOD;
    b >>= 1;
    a = 1ll * a * a % MOD;
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s >> n;
  for (int i = 1; i <= n; i++) cin >> x[i];
  for (int i = 0; i <= 9; i++) val[i] = i, len[i] = 1, pw[i] = 10;
  for (int i = n; i >= 1; i--) {
    int a = x[i][0] - '0', sz = x[i].size(), cv = 0;
    long long cl = 0;
    for (int j = 3; j < sz; j++) {
      int y = x[i][j] - '0';
      if (len[y]) {
        cl += len[y];
        if (cl >= 2 * MOD - 2) cl = (MOD - 1) + cl % (MOD - 1);
        cv = (1ll * cv * pw[y] + val[y]) % MOD;
      }
    }
    val[a] = cv, len[a] = cl, pw[a] = qpow(10, cl);
  }
  int res = 0;
  int sz = s.size();
  for (int j = 0; j < sz; j++) {
    int y = s[j] - '0';
    if (len[y]) res = (1ll * res * pw[y] + val[y]) % MOD;
  }
  cout << res << endl;
  return 0;
}
