#include <bits/stdc++.h>
using namespace std;
long long dp[100005][10];
long long tens[100005][10];
int n;
int mod = 1e9 + 7;
int frm[100005];
string to[100005];
void go() {
  for (int j = 0; j < 10; ++j) {
    dp[n][j] = j;
    tens[n][j] = 10;
  }
  for (int i = n - 1; i >= 0; --i) {
    for (int j = 0; j < 10; ++j) {
      if (j != frm[i]) {
        dp[i][j] = dp[i + 1][j];
        tens[i][j] = tens[i + 1][j];
      }
    }
    int slen = to[i].size();
    long long t = 1;
    long long ans = 0;
    long long tn = 1;
    for (int pos = 0; pos < slen; ++pos) {
      int dg = to[i][pos] - '0';
      ans = ans * tens[i + 1][dg] + dp[i + 1][dg];
      ans %= mod;
      tn = tens[i + 1][dg] * tn % mod;
    }
    tens[i][frm[i]] = tn;
    dp[i][frm[i]] = ans;
  }
}
char buffer[100005];
int main() {
  frm[0] = 0;
  scanf("%s", buffer);
  to[0] = buffer;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    scanf("%s", buffer);
    frm[i] = buffer[0] - '0';
    to[i] = buffer + 3;
  }
  ++n;
  go();
  cout << dp[0][0] << endl;
  return 0;
}
