#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
const int mod = 1e9 + 7;
string s, t[N];
int n, f[N], g[N], ans;
int main() {
  int i, j, x, tf, tg;
  cin.sync_with_stdio(false);
  cin.tie(0);
  cin >> s >> n;
  for (i = 1; i <= n; ++i) cin >> t[i];
  for (i = 0; i <= 9; ++i) {
    f[i] = 10;
    g[i] = i;
  }
  for (i = n; i; --i) {
    tf = 1;
    tg = 0;
    x = t[i][0] - '0';
    for (j = 3; j < t[i].size(); ++j) {
      int y = t[i][j] - '0';
      tf = (long long)tf * f[y] % mod;
      tg = ((long long)tg * f[y] + g[y]) % mod;
    }
    f[x] = tf;
    g[x] = tg;
  }
  for (auto k : s) ans = ((long long)ans * f[k - '0'] + g[k - '0']) % mod;
  cout << ans;
  return 0;
}
