#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T abs(T t) {
  return t < 0 ? -t : t;
}
const long long modn = 1000000007;
inline long long mod(long long x) { return x % modn; }
const long long m = 1000000006;
inline long long md(long long x) { return x % m; }
long long fexp(long long n, long long p) {
  long long r = 1;
  for (; p; p >>= 1, n = mod(n * n))
    if (p & 1) r = mod(r * n);
  return r;
}
long long d[10], sz[10];
string s[100009];
char s1[100009], s2[100009];
char c[100009];
int main() {
  int i, n;
  scanf("%s %d", s1, &n);
  for (i = 0; i < n; i++) {
    scanf(" %c", &c[i]);
    fgets(s2, 100009, stdin);
    s[i] = s2;
  }
  for (i = 0; i < 10; i++) d[i] = i, sz[i] = 1;
  for (i = n - 1; i >= 0; i--) {
    long long p = 1;
    long long v = 0, s = 0;
    for (char c : ::s[i]) {
      if (!isdigit(c)) continue;
      v = mod(v * fexp(10, sz[c - '0']) + d[c - '0']);
      s = md(s + sz[c - '0']);
    }
    d[c[i] - '0'] = v;
    sz[c[i] - '0'] = s;
  }
  long long v = 0;
  for (i = 0; s1[i]; i++)
    v = mod(v * fexp(10, sz[s1[i] - '0']) + d[s1[i] - '0']);
  printf("%d\n", (int)v);
}
