#include <bits/stdc++.h>
using namespace std;
const int mod = (int)1e9 + 7;
const int pmod = (int)1e9 + 6;
long long modpow(long long a, long long b) {
  long long x = 1LL, y = a;
  while (b > 0) {
    if (b & 1) x = (x * y) % mod;
    y = (y * y) % mod;
    b >>= 1;
  }
  return x;
}
int main() {
  string s;
  vector<string> rules;
  vector<long long> f(10);
  vector<int> lf(10);
  for (int i = 0; i < 10; ++i) {
    f[i] = i;
    lf[i] = 1;
  }
  int n;
  cin >> s >> n;
  for (int i = 0; i < n; ++i) {
    string tmp;
    cin >> tmp;
    rules.push_back(tmp);
  }
  for (int i = n - 1; i >= 0; --i) {
    int d = rules[i][0] - '0';
    int l = rules[i].length();
    long long newf = 0;
    int newl = 0;
    for (int j = 3; j < l; ++j) {
      newf = ((newf * modpow(10, lf[rules[i][j] - '0'])) % mod +
              f[rules[i][j] - '0']) %
             mod;
      newl = (newl + lf[rules[i][j] - '0']) % pmod;
    }
    f[d] = newf;
    lf[d] = newl;
  }
  int l = s.length();
  long long ans = 0;
  for (int i = 0; i < l; ++i) {
    ans = ((ans * modpow(10, lf[s[i] - '0'])) % mod + f[s[i] - '0']) % mod;
  }
  cout << ans << endl;
  return 0;
}
