#include <bits/stdc++.h>
using namespace std;
long long pw[11], val[11];
int main() {
  string s, inp;
  cin >> inp;
  int m;
  cin >> m;
  vector<int> q(m), q2[100001];
  for (int i = 0; i < m; i++) {
    cin >> s;
    q[i] = s[0] - 48;
    int len = s.size();
    for (int j = 3; j < len; j++) q2[i].push_back(s[j] - 48);
  }
  for (int i = 0; i < 10; i++) pw[i] = 10, val[i] = i;
  for (int i = m - 1; i >= 0; i--) {
    int d = q[i];
    long long n_val = 0;
    long long n_pow = 1;
    int sz = q2[i].size();
    for (int j = 0; j < sz; j++) {
      int c = q2[i][j];
      n_val = (n_val * pw[c]) % 1000000007;
      n_val = (n_val + val[c]) % 1000000007;
      n_pow = (n_pow * pw[c]) % 1000000007;
    }
    val[d] = n_val;
    pw[d] = n_pow;
  }
  long long res = 0;
  int Len = inp.size();
  for (int i = 0; i < Len; i++) {
    int d = inp[i] - 48;
    res = (res * pw[d]) % 1000000007;
    res = (res + val[d]) % 1000000007;
  }
  cout << res << endl;
  return 0;
}
