#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const long long mdl = 1000 * 1000 * 1000 + 7;
long long msk[10];
long long len[10];
string str;
vector<pair<int, string> > ops;
int n;
long long fpw(long long a, long long b) {
  if (b == 0) return 1;
  long long d = fpw(a, b / 2);
  d = (d * d) % mdl;
  if (b & 1) {
    d = (d * a) % mdl;
  }
  return d;
}
int main() {
  cin >> str;
  cin >> n;
  string cur;
  ops.push_back(make_pair(0, str));
  for (int i = 0; i < n; i++) {
    cin >> cur;
    ops.push_back(make_pair(cur[0] - '0', string(cur.begin() + 3, cur.end())));
  }
  for (int i = 0; i < 10; i++) {
    msk[i] = i;
    len[i] = 1;
  }
  for (int i = n; i >= 0; i--) {
    long long res = 0;
    long long h = 0;
    for (int j = (int)ops[i].second.size() - 1; j >= 0; j--) {
      res = (res + msk[ops[i].second[j] - '0'] * fpw(10, h)) % mdl;
      h = (h + len[ops[i].second[j] - '0']) % (mdl - 1);
    }
    msk[ops[i].first] = res;
    len[ops[i].first] = h;
  }
  cout << msk[0] << endl;
  return 0;
}
