#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
const int mod = 1000000007;
int n;
char st[maxn], t[maxn];
int f[maxn], g[maxn];
pair<int, string> A[maxn];
void Work() {
  for (int i = n; i; i--) {
    int t = 0, w = 1, len = A[i].second.length();
    for (int j = 0; j < len; j++) {
      int d = A[i].second[j] - '0';
      t = (1LL * g[d] * t + f[d]) % mod;
      w = 1LL * w * g[d] % mod;
    }
    f[A[i].first] = t;
    g[A[i].first] = w;
  }
  int ans = 0, len = strlen(st);
  for (int i = 0; i < len; i++)
    ans = (1LL * ans * g[st[i] - '0'] + f[st[i] - '0']) % mod;
  printf("%d\n", ans);
}
void Init() {
  scanf("%s%d", st, &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", t);
    A[i] = make_pair(t[0] - '0', t + 3);
  }
  for (int i = 0; i < 10; i++) f[i] = i, g[i] = 10;
}
int main() {
  Init();
  Work();
  return 0;
}
