#include <bits/stdc++.h>
const int MD = (int)1e9 + 7;
using namespace std;
int n, k;
string s;
int main() {
  string s;
  cin >> s;
  vector<long long> curRem(10), len(10);
  for (int i = 0; i < 10; i++) curRem[i] = i, len[i] = 10;
  int n;
  cin >> n;
  vector<pair<char, string> > q;
  for (int it = 0; it < n; it++) {
    string inp;
    cin >> inp;
    char digit = inp[0];
    string t;
    if (inp.size() < 4) {
      q.push_back(make_pair(digit, ""));
    } else {
      q.push_back(make_pair(digit, inp.substr(3)));
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    char digit = q[i].first;
    string t = q[i].second;
    long long rem = 0, l = 1;
    for (int j = 0; j < t.size(); j++) {
      int c = t[j] - '0';
      rem *= len[c];
      rem += curRem[c];
      rem %= MD;
      l = l * len[c] % MD;
    }
    curRem[digit - '0'] = rem;
    len[digit - '0'] = l;
  }
  long long ans = 0;
  for (int i = 0; i < s.size(); i++) {
    int c = s[i] - '0';
    ans *= len[c];
    ans += curRem[c];
    ans %= MD;
  }
  cout << ans;
  return 0;
}
