#include <bits/stdc++.h>
using namespace std;
string str[100100];
int n, i, j;
char t[100010];
char s[100100];
int k[10], b[10];
long long kk, bb;
int main() {
  gets(s);
  gets(t);
  sscanf(t, "%d", &n);
  for (i = 0; i < n; i++) {
    gets(t);
    str[i] = t;
  }
  for (i = 0; i < 10; i++) {
    k[i] = 10;
    b[i] = i;
  }
  for (i = n - 1; i >= 0; i--) {
    kk = 1;
    bb = 0;
    for (j = 3; j < str[i].size(); j++) {
      kk = kk * k[str[i][j] - '0'] % 1000000007;
      bb = (bb * k[str[i][j] - '0'] + b[str[i][j] - '0']) % 1000000007;
    }
    k[str[i][0] - '0'] = kk;
    b[str[i][0] - '0'] = bb;
  }
  bb = 0;
  for (i = 0; s[i]; i++) bb = (bb * k[s[i] - '0'] + b[s[i] - '0']) % 1000000007;
  printf("%lld\n", bb);
  return 0;
}
