#include <bits/stdc++.h>
using namespace std;
long long dp[2][10];
long long len[2][10];
long long p[100010];
long long pow1(long long a, long long b) {
  if (a == 0) return 0;
  long long x = 1, y = a;
  while (b > 0) {
    if (b % 2 == 1) {
      x = (x * y);
      if (x > 1000000007) x %= 1000000007;
    }
    y = (y * y);
    if (y > 1000000007) y %= 1000000007;
    b /= 2;
  }
  return x % 1000000007;
}
void reset(long long c) {
  for (long long i = 0; i < 10; i++) {
    dp[c][i] = len[c][i] = 0;
  }
}
int main() {
  string s;
  cin >> s;
  long long n;
  cin >> n;
  stack<pair<long long, string> > st;
  while (n--) {
    long long d;
    char c1, c2;
    string temp1;
    cin >> d;
    cin >> c1;
    cin >> c2;
    getline(cin, temp1);
    st.push({d, temp1});
  }
  memset(dp, 0, sizeof(dp));
  memset(len, 0, sizeof(len));
  for (long long i = 0; i < 10; i++) {
    dp[0][i] = i;
    len[0][i] = 1;
  }
  long long c = 1;
  while (!st.empty()) {
    reset(c);
    long long temp = st.top().first;
    string s = st.top().second;
    st.pop();
    long long rem = 0;
    if (s == "")
      dp[c][temp] = len[c][temp] = 0;
    else {
      for (long long i = s.size() - 1; i >= 0; i--) {
        dp[c][temp] += (dp[c ^ 1][s[i] - '0'] * pow1(10, rem)) % 1000000007;
        dp[c][temp] %= 1000000007;
        rem += len[c ^ 1][s[i] - '0'];
        rem %= 1000000007 - 1;
      }
      len[c][temp] = rem;
    }
    for (long long i = 0; i < 10; i++) {
      if (i != temp) {
        dp[c][i] = dp[c ^ 1][i];
        len[c][i] = len[c ^ 1][i];
      }
    }
    c = c ^ 1;
  }
  long long ans = 0;
  long long rem = 0;
  for (long long i = s.length() - 1; i >= 0; i--) {
    ans += dp[c ^ 1][s[i] - '0'] * pow1(10, rem);
    rem += len[c ^ 1][s[i] - '0'];
    rem %= 1000000007 - 1;
    ans %= 1000000007;
  }
  cout << ans << endl;
}
