#include <bits/stdc++.h>
using namespace std;
int n, cnt, Explan[100010], S[100010], E[100010], d[100010];
long long length[100010], End[100010];
char str[100010], s[100010];
inline int gcd(int x, int y) {
  if (!y) return x;
  return gcd(y, x % y);
}
inline void read(int &x) {
  char ch;
  int N = 1;
  while ((ch = getchar()) && (!((ch >= '0') && (ch <= '9'))) && (!(ch == '-')))
    ;
  if ((ch == '-')) {
    N = -1;
    while ((ch = getchar()) && (!((ch >= '0') && (ch <= '9'))))
      ;
  }
  x = ch - 48;
  while ((ch = getchar()) && (((ch >= '0') && (ch <= '9'))))
    x = x * 10 + ch - 48;
  x *= N;
}
long long Pow(long long a, long long x) {
  long long tmp = 1;
  for (; x; x >>= 1) {
    if (x & 1) tmp = (tmp * a) % 1000000007;
    a = (a * a) % 1000000007;
  }
  return tmp;
}
int main() {
  scanf("%s", str + 1);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    getchar();
    scanf("%d", &d[i]);
    getchar();
    scanf("%s", s);
    int p = strlen(s);
    if (p == 1) {
      S[i] = 0;
      E[i] = -1;
      continue;
    }
    S[i] = cnt + 1;
    for (int j = 1; j < p; j++) Explan[++cnt] = s[j] - '0';
    E[i] = cnt;
  }
  for (int i = 0; i <= 9; i++) length[i] = 1, End[i] = i;
  for (int i = n; i >= 1; i--) {
    long long t = 0, t_len = 0;
    for (int j = S[i]; j <= E[i]; j++) {
      t = (t * Pow(10, length[Explan[j]]) % 1000000007 + End[Explan[j]]) %
          1000000007;
      t_len = (t_len + length[Explan[j]]) % (1000000007 - 1);
    }
    End[d[i]] = t, length[d[i]] = t_len;
  }
  long long Ans = 0;
  int p = strlen(str + 1);
  for (int i = 1; i <= p; i++)
    Ans =
        (Ans * Pow(10, length[str[i] - '0']) % 1000000007 + End[str[i] - '0']) %
        1000000007;
  printf("%I64d\n", Ans);
}
