#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& x) {
  int f = 1;
  x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
const int N = 100000 + 5;
const int mo = 1000000007;
int n, q;
char s[N], QAQ[N];
string t[N];
long long v[N], g[N];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  read(q);
  for (int i = 0; i <= 9; i++) v[i] = i, g[i] = 10;
  for (int i = 1; i <= q; i++) {
    scanf("%s", QAQ);
    t[i] = QAQ;
  }
  for (int i = q; i >= 1; i--) {
    int c = t[i][0] - '0';
    long long ans = 0, tmp = 1;
    for (int j = t[i].length() - 1; j > 2; j--) {
      int p = t[i][j] - '0';
      (ans += v[p] * tmp) %= mo;
      tmp = tmp * g[p] % mo;
    }
    v[c] = ans;
    g[c] = tmp;
  }
  long long ans = 0, tmp = 1;
  for (int i = n; i >= 1; i--) {
    int p = s[i] - '0';
    (ans += v[p] * tmp) %= mo;
    tmp = tmp * g[p] % mo;
  }
  ans = (ans + mo) % mo;
  printf("%lld\n", ans);
  return 0;
}
