#include <bits/stdc++.h>
using namespace std;
long long int mx = 1e9 + 7, n, m, k, c;
string sa;
string q[100010];
long long val[11], sh[11];
inline void doit(int ind) {
  int work = q[ind][0] - '0', len = q[ind].length();
  if (len == 3) {
    val[work] = 0;
    sh[work] = 1;
    return;
  }
  long long tmp, res = 0, sha = 1;
  for (int i = 3; i < len; ++i) {
    tmp = q[ind][i] - '0';
    res = (res * sh[tmp] + val[tmp]) % mx;
    sha = (sha * sh[tmp] % mx) % mx;
  }
  val[work] = res;
  sh[work] = sha;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> sa;
  cin >> n;
  for (int i = 0; i < 10; ++i) val[i] = i, sh[i] = 10;
  for (int i = 0; i < n; ++i) cin >> q[i];
  for (int i = n - 1; i >= 0; --i) doit(i);
  int ln = sa.length();
  long long int ans = 0;
  for (int i = 0; i < ln; ++i) {
    k = sa[i] - '0';
    ans = (ans * sh[k] + val[k]) % mx;
  }
  cout << ans << endl;
  return 0;
}
