#include <bits/stdc++.h>
using namespace std;
string S;
int pw[2000005];
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 dfs(int num, int cur) {
  if (memo[num][cur] >= 0) return;
  if (cur == N) {
    memo[num][cur] = num;
    dig[num][cur] = 10;
    return;
  }
  if (from[cur] != num) {
    dfs(num, cur + 1);
    memo[num][cur] = memo[num][cur + 1];
    dig[num][cur] = dig[num][cur + 1];
  } else {
    memo[num][cur] = 0;
    dig[num][cur] = 1;
    int i;
    for (i = to[cur].size() - 1; i >= 0; i--) {
      int nn = to[cur][i] - '0';
      dfs(nn, cur + 1);
      memo[num][cur] =
          (memo[num][cur] + memo[nn][cur + 1] * dig[num][cur]) % mo;
      dig[num][cur] = (dig[num][cur] * dig[nn][cur + 1]) % mo;
    }
  }
}
void solve() {
  int i, j, k, l, r, x, y;
  string s;
  pw[0] = 1;
  for (i = 0; i < 2000004; i++) pw[i + 1] = (pw[i] * 10) % mo;
  cin >> to[0];
  cin >> N;
  N++;
  for (i = 1; i < N; i++) {
    cin >> s;
    from[i] = s[0] - '0';
    to[i] = s.substr(3);
  }
  memset(memo, 0xff, sizeof(memo));
  dfs(0, 0);
  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;
}
