#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int main() {
  int n;
  string s, ss;
  cin >> s >> n;
  vector<pair<int, string> > v(n + 1);
  v[0] = make_pair(0, s);
  char next;
  while (cin.get(next))
    if (next == '\n') break;
  for (int i = 1; i <= n; ++i) {
    getline(cin, ss);
    if (ss.length() > 3)
      v[i] = make_pair(ss[0] - '0', ss.substr(3));
    else
      v[i] = make_pair(ss[0] - '0', "");
  }
  long long val[10], len[10];
  for (int i = 0; i < 10; ++i) val[i] = i, len[i] = 10;
  for (int i = n; i >= 0; --i) {
    int cu = v[i].first;
    string s = v[i].second;
    if (s.empty()) {
      val[cu] = 0;
      len[cu] = 1;
      continue;
    }
    long long pow = 1;
    long long va = 0;
    for (int j = s.length() - 1; j >= 0; --j) {
      va += ((val[(s[j] - '0')]) * pow) % MOD;
      va %= MOD;
      pow = (pow * len[(s[j] - '0')]) % MOD;
    }
    len[cu] = pow;
    val[cu] = va;
  }
  cout << val[0] << endl;
  return 0;
}
