#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
long long POW(long long a, long long b, long long MMM = MOD) {
  long long ret = 1;
  for (; b; b >>= 1, a = (a * a) % MMM)
    if (b & 1) ret = (ret * a) % MMM;
  return ret;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) {
  if (a == 0 || b == 0) return a + b;
  return a * (b / gcd(a, b));
}
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int ddx[] = {0, 0, 1, 1, 1, -1, -1, -1}, ddy[] = {1, -1, 1, 0, -1, 1, 0, -1};
char s[100005], t[100005];
int c[100005];
vector<int> v[100005];
long long Pow[10], val[10];
int main() {
  scanf("%s", s);
  int n, m;
  n = strlen(s);
  scanf("%d", &m);
  for (int(i) = (0); (i) <= (m - 1); (i) += (1)) {
    scanf("%s", t);
    c[i] = t[0] - '0';
    for (int j = 3; t[j]; j++) v[i].push_back(t[j] - '0');
  }
  for (int(i) = (0); (i) <= (9); (i) += (1)) Pow[i] = 10, val[i] = i;
  for (int(i) = (m - 1); (i) >= (0); (i) -= (1)) {
    int x = c[i];
    long long nPow = 1, nval = 0;
    for (int y : v[i]) {
      nval = (nval * Pow[y] + val[y]) % MOD;
      nPow = (nPow * Pow[y]) % MOD;
    }
    Pow[x] = nPow, val[x] = nval;
  }
  long long ans = 0;
  for (int(i) = (0); (i) <= (n - 1); (i) += (1)) {
    int x = s[i] - '0';
    ans = (ans * Pow[x] + val[x]) % MOD;
  }
  printf("%lld", ans);
}
