#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : -x;
}
const long double pi = acos(-1.0);
int n;
int m;
const int mod = 1e9 + 7;
string s;
char buf[100100];
int a[10], b[10];
int v[100100][10];
string ss[100100];
int res[100100];
int len10[100100];
long long len[100100];
long long pow10(long long l) {
  long long res = 1, mul = 10;
  for (; l > 0; l >>= 1) {
    if (l & 1) res = (res * mul) % mod;
    mul = (mul * mul) % mod;
  }
  return res;
}
int main() {
  cin >> ss[0];
  scanf("%d\n", &n);
  for (int i = 1; i <= n; i++) {
    char ch;
    scanf("%c->", &ch);
    ch -= '0';
    gets(buf);
    ss[i] = buf;
    if (a[ch]) b[ch] = a[ch];
    a[ch] = i;
    for (int j = b[ch]; j < i; j++) v[j][ch] = i;
  }
  for (int i = n; i >= 0; i--) {
    long long l = 0;
    long long h = 0;
    for (int j = 0; j < ((int)(ss[i]).size()); j++) {
      int k = v[i][ss[i][j] - '0'];
      if (k) {
        h = h * len10[k] + res[k];
        l = (l + len[k]) % (mod - 1);
      } else {
        h = h * 10 + ss[i][j] - '0';
        l++;
      }
      h %= mod;
    }
    len[i] = l;
    len10[i] = pow10(l);
    res[i] = h;
  }
  printf("%d\n", res[0]);
  return 0;
}
