#include <bits/stdc++.h>
using namespace std;
const double eps(1e-8);
const long long mod = 1000000007LL;
struct change {
  int num;
  string s;
};
change C[100010];
long long val[10];
long long wei[10];
char buf[100010];
string all;
int n;
int main() {
  cin >> all;
  cin >> n;
  getchar();
  for (int i = 1; i <= n; i++) {
    scanf("%d->", &C[i].num);
    gets(buf);
    C[i].s = buf;
  }
  for (int i = 0; i < 10; i++) {
    val[i] = i;
    wei[i] = 10LL;
  }
  for (int i = n; i >= 1; i--) {
    if (C[i].s == "") {
      wei[C[i].num] = 1;
      val[C[i].num] = 0;
      continue;
    }
    long long tmp1 = 1LL;
    long long tmp2 = 0LL;
    for (unsigned int j = 0; j < C[i].s.length(); j++) {
      tmp2 = (tmp2 * wei[C[i].s[j] - '0']) % mod;
      tmp2 = (tmp2 + val[C[i].s[j] - '0']) % mod;
      tmp1 = (tmp1 * wei[C[i].s[j] - '0']) % mod;
    }
    wei[C[i].num] = tmp1;
    val[C[i].num] = tmp2;
  }
  long long ans = 0LL;
  for (unsigned int i = 0; i < all.length(); i++) {
    ans = (ans * wei[all[i] - '0']) % mod;
    ans = (ans + val[all[i] - '0']) % mod;
  }
  cout << ans << endl;
  return 0;
}
