#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
string a;
char buf[110000];
int src[110000];
string dest[110000];
long long qpow(long long x, long long e) {
  if (e == 0) return 1;
  if (e % 2 == 0) {
    long long t = qpow(x, e / 2);
    return t * t % MOD;
  } else {
    return x * qpow(x, e - 1) % MOD;
  }
}
int main() {
  src[0] = 0;
  cin >> dest[0];
  int m;
  cin >> m;
  for (int i = 1; i <= m; i++) {
    scanf("%s", buf);
    buf[1] = 0;
    sscanf(buf, "%d", &src[i]);
    dest[i] = buf + 3;
  }
  m++;
  long long len[10];
  long long rem[10];
  for (int i = 0; i <= 9; i++) {
    rem[i] = i;
    len[i] = 1;
  }
  for (int i = m - 1; i >= 0; i--) {
    long long nrem = 0;
    long long nlen = 0;
    for (int j = dest[i].length() - 1; j >= 0; j--) {
      int c = dest[i][j] - '0';
      nrem = (nrem + rem[c] * qpow(10, nlen)) % MOD;
      nlen = (nlen + len[c]) % (MOD - 1);
    }
    rem[src[i]] = nrem;
    len[src[i]] = nlen;
  }
  cout << rem[0] << endl;
  return 0;
}
