#include <bits/stdc++.h>
using namespace std;
char s[100010], t[100010];
long long v[10], dig[10], d[100010], n, i, j, now, len, x, ans;
struct node {
  long long d;
  string s;
} a[100010];
int main() {
  gets(s);
  scanf("%I64d", &n);
  getchar();
  for (i = 1; i <= n; i++) {
    scanf("%I64d->", &a[i].d);
    gets(t);
    a[i].s = t;
  }
  for (i = 0; i < 10; i++) {
    v[i] = i;
    dig[i] = 10;
  }
  for (i = n; i > 0; i--) {
    now = 0;
    len = a[i].s.length();
    if (len == 0) {
      v[a[i].d] = 0;
      dig[a[i].d] = 1;
      continue;
    }
    x = 1;
    for (j = 0; j < len; j++) {
      now = (now * dig[a[i].s[j] - '0'] + v[a[i].s[j] - '0']) % 1000000007LL;
      x = x * dig[a[i].s[j] - '0'] % 1000000007LL;
    }
    v[a[i].d] = now;
    dig[a[i].d] = x;
  }
  ans = 0;
  for (i = 0; s[i]; i++)
    ans = (ans * dig[s[i] - '0'] + v[s[i] - '0']) % 1000000007LL;
  printf("%I64d", ans);
}
