#include <bits/stdc++.h>
using namespace std;
string st;
pair<long long, long long> rule[1000];
long rules;
string st1;
vector<pair<long, string> > v;
long long bas, res, val;
pair<long, string> parse(string st) {
  pair<long, string> r;
  r.first = st[0] - '0';
  for (int i = 3; i < st.size(); i++) r.second += st[i];
  return r;
}
void update(pair<long, string> p) {
  long long res, bas;
  res = bas = 0;
  bas = 1;
  string st = p.second;
  if (st.size()) {
    for (int i = st.size() - 1; i + 1; --i) {
      res += 1ll * rule[st[i] - '0'].first * bas;
      bas *= rule[st[i] - '0'].second;
      res %= 1000000007;
      bas %= 1000000007;
    }
  }
  rule[p.first] = make_pair(res, bas);
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> st;
  for (int i = 0; i < 10; i++) rule[i] = make_pair(i, 10);
  cin >> rules;
  for (; rules; --rules) {
    cin >> st1;
    v.push_back(parse(st1));
  }
  reverse(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) update(v[i]);
  res = 0;
  bas = 1;
  for (int i = st.size() - 1; i + 1; --i) {
    res += 1ll * rule[st[i] - '0'].first * bas;
    bas *= rule[st[i] - '0'].second;
    res %= 1000000007;
    bas %= 1000000007;
  }
  cout << res << endl;
  cin.get();
  cin.get();
  return 0;
}
