#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
#pragma GCC optimize(2)
using namespace std;
const int N = 1e5 + 3, P = 1e9 + 7, inf = 0x3fffffff;
struct QAQ {
  int id;
  string s;
} Q[N];
int n, i, j, c, len;
char ch[N], t[N];
long long X, W, ans, x[10], w[10];
int main() {
  scanf("%s%d", ch, &n);
  for (i = 1; i <= n; ++i) {
    scanf("%d->", &Q[i].id);
    gets(t), Q[i].s = t;
  }
  for (i = 0; i < 10; ++i) x[i] = i, w[i] = 10;
  for (i = n; i; --i) {
    len = Q[i].s.length();
    if (!len) {
      x[Q[i].id] = 0, w[Q[i].id] = 1;
      continue;
    }
    X = 0, W = 1;
    for (j = 0; j < len; ++j)
      ((X *= w[c = Q[i].s[j] ^ 48]) += x[c]) %= P, (W *= w[c]) %= P;
    x[Q[i].id] = X, w[Q[i].id] = W;
  }
  len = strlen(ch);
  for (i = 0; i < len; ++i) ((ans *= w[c = ch[i] ^ 48]) += x[c]) %= P;
  printf("%lld", ans);
}
