#include <bits/stdc++.h>
using namespace std;
string S;
signed long long mo = 1000000007;
int N;
int from[100005];
string to[100005];
signed long long memo[20][100005];
signed long long dig[20][100005];
void solve() {
  int i, j, k, l, r, x, y;
  string s;
  cin >> to[0];
  cin >> N;
  N++;
  for (i = 1; i < N; i++) {
    cin >> s;
    from[i] = s[0] - '0';
    to[i] = s.substr(3);
  }
  for (i = 0; i < 10; i++) memo[i][N] = i, dig[i][N] = 10;
  for (i = N - 1; i >= 0; i--) {
    for (j = 0; j < 10; j++)
      memo[j][i] = memo[j][i + 1], dig[j][i] = dig[j][i + 1];
    memo[from[i]][i] = 0;
    dig[from[i]][i] = 1;
    for (j = to[i].size() - 1; j >= 0; j--) {
      int nn = to[i][j] - '0';
      memo[from[i]][i] =
          (memo[from[i]][i] + memo[nn][i + 1] * dig[from[i]][i]) % mo;
      dig[from[i]][i] = (dig[from[i]][i] * dig[nn][i + 1]) % mo;
    }
  }
  cout << memo[0][0] << endl;
}
int main(int argc, char** argv) {
  string s;
  int i;
  if (argc == 1) ios::sync_with_stdio(false);
  for (i = 0; i < argc - 1; i++) s += argv[i + 1], s += '\n';
  for (i = 0; i < s.size(); i++) ungetc(s[s.size() - 1 - i], stdin);
  solve();
  return 0;
}
