#include <bits/stdc++.h>
using namespace std;
const int max_n = 100005;
const long long mod = 1000000007;
struct point {
  long long val, loc;
} digit[9];
char str[max_n];
vector<int> F[max_n];
char S[max_n];
int n;
int main() {
  while (scanf("%s", str) == 1) {
    for (int i = 0; i < 10; ++i) digit[i].loc = 10, digit[i].val = i;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
      F[i].clear();
      scanf("%s", S);
      F[i].push_back(S[0] - '0');
      int len = strlen(S);
      for (int j = 3; j < len; ++j) F[i].push_back(S[j] - '0');
    }
    for (int i = n - 1; i >= 0; --i) {
      long long val = 0, loc = 1;
      int L = F[i].size();
      for (int k = L - 1; k > 0; --k) {
        int t = F[i][k];
        val = (val + loc * digit[t].val) % mod;
        loc = (loc * digit[t].loc) % mod;
      }
      digit[F[i][0]].loc = loc;
      digit[F[i][0]].val = val;
    }
    long long ans = 0, loc = 1;
    int L = strlen(str);
    for (int i = L - 1; i >= 0; --i) {
      long long t = str[i] - '0';
      ans = (ans + digit[t].val * loc) % mod;
      loc = (loc * digit[t].loc) % mod;
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
