#include <bits/stdc++.h>
using namespace std;
long long a, b, len, c, n, m;
char s[100007], sf[100007];
string d[100007];
pair<long long, long long> dp[11];
int main() {
  scanf("%s", s);
  n = strlen(s);
  scanf("%lld", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%s", sf);
    d[i] = sf;
  }
  for (int i = 0; i < 10; i++) dp[i] = {i, 10LL};
  for (int i = m; i >= 1; i--) {
    len = d[i].size();
    a = 0, b = 1;
    for (int j = 3; j < len; j++) {
      a = (a * dp[d[i][j] - '0'].second + dp[d[i][j] - '0'].first) %
          1000000007LL;
      b = (b * dp[d[i][j] - '0'].second) % 1000000007LL;
    }
    dp[d[i][0] - '0'] = {a, b};
  }
  a = 0;
  for (int i = 0; i < n; i++)
    a = (a * dp[s[i] - '0'].second + dp[s[i] - '0'].first) % 1000000007LL;
  printf("%lld\n", a);
  return 0;
}
