#include <bits/stdc++.h>
using namespace std;
int n, m;
long long pw[19];
long long val[19];
char A[100009];
string s[100009];
int main() {
  scanf("%s", A);
  scanf("%d", &m);
  for (int i = 0; i < 10; i++) {
    val[i] = i;
    pw[i] = 10;
  }
  for (int i = 0; i < m; i++) cin >> s[i];
  for (int j = m - 1; j >= 0; j--) {
    int ln = s[j].length();
    int nm = s[j][0] - '0';
    long long nmpw = 1;
    long long san = 0;
    for (int i = ln - 1; i > 2; i--) {
      san = (san + (nmpw * val[s[j][i] - '0'])) % 1000000007;
      nmpw = (nmpw * pw[s[j][i] - '0']) % 1000000007;
    }
    val[nm] = san;
    pw[nm] = nmpw;
  }
  int ln = strlen(A);
  long long ans = 0;
  long long nmpw = 1;
  for (int i = ln - 1; i >= 0; i--) {
    ans = (ans + (val[A[i] - '0'] * nmpw)) % 1000000007;
    nmpw = (nmpw * pw[A[i] - '0']) % 1000000007;
  }
  printf("%lld\n", ans);
}
