#include <bits/stdc++.h>
using namespace std;
long long a[10];
long long b[10];
const int MAXN = 100001;
const long long MODULE = 1000 * 1000 * 1000 + 7;
string s[MAXN];
int d[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  d[0] = 0;
  cin >> s[0];
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    char c;
    cin >> c;
    d[i] = c - '0';
    cin >> c;
    cin >> c;
    getline(cin, s[i]);
  }
  for (int i = 0; i < 10; i++) {
    a[i] = i;
    b[i] = 10;
  }
  for (int i = n; i >= 0; i--) {
    int na = 0, nb = 1;
    int m = s[i].length();
    for (int j = m - 1; j >= 0; j--) {
      int dg = s[i][j] - '0';
      na = (na + nb * a[dg]) % MODULE;
      nb = (nb * b[dg]) % MODULE;
    }
    a[d[i]] = na, b[d[i]] = nb;
  }
  cout << a[0] << endl;
  return 0;
}
