#include <bits/stdc++.h>
int i, a[100100], n, l, j, len;
char str[100100], ch, ss[100100];
long long lf[100100], rf[100100], ws[100100], f[100100], w, v, q;
int main() {
  for (i = 0; i <= 9; i++) {
    ws[i] = 10;
    f[i] = i;
  }
  scanf("%s", &ss);
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d->", &a[i]);
    lf[i] = l + 1;
    do {
      scanf("%c", &ch);
      if (ch == 10)
        break;
      else {
        l++;
        str[l] = ch;
      }
    } while (true);
    rf[i] = l;
  }
  for (i = n; i >= 1; i--) {
    w = 1;
    v = 0;
    for (j = rf[i]; j >= lf[i]; j--) {
      q = str[j] - 48;
      v = (f[q] * w + v) % 1000000007;
      w = w * ws[q] % 1000000007;
    }
    ws[a[i]] = w;
    f[a[i]] = v;
  }
  len = strlen(ss);
  w = 1;
  v = 0;
  for (i = len - 1; i >= 0; i--) {
    q = ss[i] - 48;
    v = (f[q] * w + v) % 1000000007;
    w = w * ws[q] % 1000000007;
  }
  printf("%lld", v);
}
