#include <bits/stdc++.h>
using namespace std;
long long pos[20], num[20];
string s[100005];
char p[100005], c;
int main() {
  int n;
  while (~scanf("%s%d", p, &n)) {
    c = getchar();
    for (int i = 1; i <= n; i++) {
      s[i] = "";
      while ((c = getchar()) != '\n') s[i] = s[i] + c;
    }
    for (int i = 0; i < 10; i++) {
      pos[i] = 10;
      num[i] = i;
    }
    long long tnum, tpos;
    for (int i = n; i; i--) {
      tnum = 0;
      tpos = 1;
      int l = s[i].size();
      for (int j = l - 1; j > 2; j--) {
        tnum =
            (tnum + (tpos * num[s[i][j] - '0']) % 1000000007ll) % 1000000007ll;
        tpos = tpos * pos[s[i][j] - '0'] % 1000000007ll;
      }
      pos[s[i][0] - '0'] = tpos;
      num[s[i][0] - '0'] = tnum;
    }
    int l = strlen(p);
    tnum = 0;
    tpos = 1;
    for (int j = l - 1; j >= 0; j--) {
      tnum = (tnum + (tpos * num[p[j] - '0']) % 1000000007ll) % 1000000007ll;
      tpos = tpos * pos[p[j] - '0'] % 1000000007ll;
    }
    printf("%I64d\n", tnum);
  }
}
