#include <bits/stdc++.h>
using namespace std;
template <typename T>
T gcd(T a, T b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
template <typename T>
T pow(T a, T b, long long m) {
  T ans = 1;
  while (b > 0) {
    if (b % 2 == 1) ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans % m;
}
int main() {
  string s;
  cin >> s;
  int n;
  cin >> n;
  int from[n];
  char s1, s2;
  string to[n];
  long long a[10], b[10];
  for (int i = 0; i < n; i++) {
    cin >> from[i] >> to[i];
  }
  for (int i = 0; i < n; i++) {
    to[i] = to[i].substr(2);
  }
  for (int i = 0; i < 10; i++) {
    a[i] = 10;
    b[i] = i;
  }
  for (int i = n - 1; i >= 0; i--) {
    long long p = 1, q = 0;
    for (int j = 0; j < to[i].length(); j++) {
      int c = to[i][j] - '0';
      long long p2 = (a[c] * p) % 1000000007;
      long long q2 = (a[c] * q + b[c]) % 1000000007;
      p = p2;
      q = q2;
    }
    a[from[i]] = p;
    b[from[i]] = q;
  }
  long long ans = 0;
  for (int i = 0; i < s.length(); i++) {
    int c = s[i] - '0';
    ans = (ans * a[c] + b[c]) % 1000000007;
  }
  cout << ans;
}
