#include <bits/stdc++.h>
using namespace std;
char st[100005], str[100005];
string t[100005];
int d[100005];
int val[100005], pw[100005];
int main() {
  int n, m, i, j, sz;
  int s, p;
  scanf("%s", st);
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d->", &d[i]);
    gets(str);
    t[i] = str;
  }
  for (i = 0; i < 10; i++) val[i] = i, pw[i] = 10;
  for (i = n - 1; i >= 0; i--) {
    p = 1;
    s = 0;
    sz = t[i].size();
    for (j = 0; j < sz; j++) {
      p = (1ll * p * pw[t[i][j] - '0']) % 1000000007ll;
      s = (1ll * s * pw[t[i][j] - '0']) % 1000000007ll;
      s += val[t[i][j] - '0'];
      if (s > 1000000007ll) s -= 1000000007ll;
    }
    val[d[i]] = s;
    pw[d[i]] = p;
  }
  s = 0;
  sz = strlen(st);
  for (i = 0; i < sz; i++) {
    s = (1ll * s * pw[st[i] - '0']) % 1000000007ll;
    s = s + val[st[i] - '0'];
    if (s > 1000000007ll) s -= 1000000007ll;
  }
  printf("%d\n", s);
  return 0;
}
