#include <bits/stdc++.h>
using namespace std;
int n;
string st, A[100005];
long long f[100005], g[100005], x, len, o, now, ans = 0;
int main() {
  int i, j;
  cin >> st;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) cin >> A[i];
  for (i = 0; i < 10; i++) f[i] = i, g[i] = 10;
  for (i = n; i >= 1; i--) {
    x = A[i][0] - '0', now = 0;
    o = A[i].size(), len = 1;
    for (j = o - 1; j >= 3; j--) {
      now = (now + len * f[A[i][j] - '0']) % 1000000007;
      len = (len * g[A[i][j] - '0']) % 1000000007;
    }
    f[x] = now, g[x] = len;
  }
  o = st.size(), len = 1;
  for (i = o - 1; i >= 0; i--) {
    ans = (ans + f[st[i] - '0'] * len) % 1000000007;
    len = (len * g[st[i] - '0']) % 1000000007;
  }
  printf("%I64d", ans);
  return 0;
}
