#include <bits/stdc++.h>
using namespace std;
const long long M = (long long)1e9 + 7;
int main() {
  string s;
  cin >> s;
  int n;
  cin >> n;
  vector<string> a(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  long long v[10], d[10];
  for (int i = 0; i < 10; ++i) v[i] = i, d[i] = 10;
  reverse((a).begin(), (a).end());
  for (int i = 0; i < n; ++i) {
    int t = a[i][0] - '0';
    a[i] = a[i].substr(3);
    reverse((a[i]).begin(), (a[i]).end());
    long long dec = 1, value = 0;
    for (int j = 0; j < (int)(a[i]).size(); ++j) {
      value += dec * v[a[i][j] - '0'] % M;
      value %= M;
      dec *= d[a[i][j] - '0'];
      dec %= M;
    }
    v[t] = value;
    d[t] = dec;
  }
  long long dec = 1;
  reverse((s).begin(), (s).end());
  long long ans = 0;
  for (int i = 0; i < (int)(s).size(); ++i) {
    ans += v[s[i] - '0'] * dec;
    ans %= M;
    dec *= d[s[i] - '0'];
    dec %= M;
  }
  cout << ans;
  return 0;
}
