#include <bits/stdc++.h>
using namespace std;
long long P[10], POT[10];
vector<pair<int, string> > Q;
int n;
int main() {
  string s;
  cin >> s;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string tmp;
    cin >> tmp;
    Q.push_back((make_pair(tmp[0] - '0', tmp.substr(3))));
  }
  for (int i = 0; i < 10; i++) {
    P[i] = i;
    POT[i] = 10;
  }
  for (int i = n - 1; i >= 0; i--) {
    long long newPot = 1;
    long long newP = 0;
    for (int j = 0; j < (int)Q[i].second.size(); j++) {
      int d = Q[i].second[j] - '0';
      newP = ((newP * POT[d]) % 1000000007LL + P[d]) % 1000000007LL;
      newPot = (newPot * POT[d]) % 1000000007LL;
    }
    P[Q[i].first] = newP;
    POT[Q[i].first] = newPot;
  }
  for (int i = 0; i < 10; i++) {
  }
  long long newP = 0;
  for (int j = 0; j < (int)s.size(); j++) {
    int d = s[j] - '0';
    newP = ((newP * POT[d]) % 1000000007LL + P[d]) % 1000000007LL;
  }
  cout << newP << endl;
  return 0;
}
