#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long q;
pair<long long, long long> c[11];
pair<long long, long long> count(const string &s) {
  long long x = 0, y = 1;
  for (long long i = 0; i < s.size(); i++) {
    x = (x * c[s[i] - '0'].second % mod + c[s[i] - '0'].first) % mod;
    y = y * c[s[i] - '0'].second % mod;
  }
  return make_pair(x, y);
}
string s, buf;
pair<long long, string> t[110000];
signed main() {
  cin >> s >> q;
  for (long long i = 0; i < 10; i++) c[i] = make_pair(i, 10);
  for (long long i = 0; i < q; i++) {
    cin >> buf;
    t[i] = make_pair(buf[0] - '0', buf.substr(3));
  }
  for (long long i = q - 1; i >= 0; i--) c[t[i].first] = count(t[i].second);
  cout << count(s).first << endl;
  return 0;
}
