#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MOD = 1000000007;
const int MAXLEN = 100000 + 100000;
const int MAXS = 1 + 100000;
const int MAXVAL = MAXS + 10;
char s[MAXLEN + 1];
int become[MAXLEN];
int n;
char qdig[MAXS];
int qstart[MAXS], qlen[MAXS];
int val[MAXVAL];
int mlt[MAXVAL];
int curto[10];
void run() {
  scanf("%s", s);
  qdig[0] = '?';
  qstart[0] = 0;
  qlen[0] = strlen(s);
  scanf("%d", &n);
  for (int i = (1); i <= (n); ++i) {
    qstart[i] = qstart[i - 1] + qlen[i - 1];
    scanf(" %c->", &qdig[i]);
    gets(s + qstart[i]);
    qlen[i] = strlen(s + qstart[i]);
  }
  for (int i = (0); i < (10); ++i) {
    curto[i] = 1 + n + i;
    val[1 + n + i] = i;
    mlt[1 + n + i] = 10;
  }
  for (int i = n; i >= 0; --i) {
    for (int j = (qstart[i]); j < (qstart[i] + qlen[i]); ++j)
      become[j] = curto[s[j] - '0'];
    if (isdigit(qdig[i])) curto[qdig[i] - '0'] = i;
  }
  for (int i = n; i >= 0; --i) {
    val[i] = 0;
    mlt[i] = 1;
    for (int j = qstart[i] + qlen[i] - 1; j >= qstart[i]; --j) {
      val[i] = (val[i] + (long long)val[become[j]] * mlt[i]) % MOD;
      mlt[i] = (long long)mlt[i] * mlt[become[j]] % MOD;
    }
  }
  printf("%d\n", val[0]);
}
int main() {
  run();
  return 0;
}
