#include <bits/stdc++.h>
using namespace std;
long long int solve(const string& number,
                    const vector<pair<char, string>>& queries) {
  vector<pair<long long int, long long int>> replacements;
  for (int i = 0; i <= 9; i++) replacements.push_back(make_pair(i, 10));
  for (int i = queries.size() - 1; i >= 0; i--) {
    int digit = queries[i].first - '0';
    string nstr = queries[i].second;
    pair<long long int, long long int> new_rep = make_pair(0, 1);
    for (int j = 0; j < nstr.size(); j++) {
      pair<long long int, long long int> current = replacements[nstr[j] - '0'];
      new_rep.first =
          (new_rep.first * current.second + current.first) % 1000000007;
      new_rep.second = (new_rep.second * current.second) % 1000000007;
    }
    replacements[digit] = new_rep;
  }
  long long int ans = 0;
  for (int i = 0; i < number.size(); i++) {
    pair<long long int, long long int> current = replacements[number[i] - '0'];
    ans = (ans * current.second) % 1000000007;
    ans = (ans + current.first) % 1000000007;
  }
  return ans;
}
int main() {
  string number;
  int nqueries;
  while (cin >> number) {
    cin >> nqueries;
    vector<pair<char, string>> queries;
    string tmp;
    for (int i = 0; i < nqueries; i++) {
      cin >> tmp;
      queries.push_back(make_pair(tmp[0], tmp.substr(3)));
    }
    long long int ans = solve(number, queries);
    cout << ans << endl;
  }
}
