#include <bits/stdc++.h>
using namespace std;
const long long mo = 1e9 + 7;
const int N = 1e5 + 10;
long long qpw(long long x, int y, long long mo) {
  long long res = 1, t0 = x;
  for (; y; y >>= 1) {
    if (y & 1) res = res * t0 % mo;
    t0 = t0 * t0 % mo;
  }
  return res;
}
char s[N], ss[N];
string s2[N];
int x[N];
long long dp[11][N], l[11][N];
int main() {
  int n;
  scanf("%s", s);
  scanf("%d", &n);
  gets(ss);
  int len;
  for (int i = 0; i < n; i++) {
    gets(ss);
    x[i] = ss[0] - '0';
    len = strlen(ss);
    if (len == 3)
      s2[i] = "";
    else {
      s2[i] = "";
      for (int j = 3; j < len; j++) s2[i] += ss[j];
    }
  }
  for (int i = 0; i < 10; i++) {
    dp[i][n] = i;
    l[i][n] = 1;
  }
  for (int i = n - 1; i > -1; i--) {
    for (int j = 0; j < 10; j++) {
      if (j != x[i]) {
        dp[j][i] = dp[j][i + 1];
        l[j][i] = l[j][i + 1];
        continue;
      }
      len = (int)s2[i].size();
      l[j][i] = dp[j][i] = 0;
      for (int k = len - 1; k > -1; k--) {
        dp[j][i] += qpw(10, l[j][i], mo) * dp[s2[i][k] - '0'][i + 1];
        l[j][i] += l[s2[i][k] - '0'][i + 1];
        dp[j][i] %= mo;
        l[j][i] %= mo - 1;
      }
    }
  }
  long long ans = 0, t0 = 0;
  len = strlen(s);
  for (int i = len - 1; i > -1; i--) {
    ans += qpw(1LL * 10, t0, mo) * dp[s[i] - '0'][0] % mo;
    ans %= mo;
    t0 += l[s[i] - '0'][0];
    t0 %= mo - 1;
  }
  printf("%I64d\n", ans);
  return 0;
}
