#include <bits/stdc++.h>
const long long MOD = 1000000007;
const long long maxn = 100010;
using namespace std;
string s, S[maxn];
struct node {
  long long num, len;
} a[20];
long long n;
int main() {
  while (cin >> s) {
    long long i, j, u, v, w, flag = -1, len, num, cnt = 0, slen;
    cin >> n;
    for (i = 0; i < n; i++) {
      cin >> S[i];
    }
    for (i = 0; i <= 9; i++) {
      a[i].num = -1;
      a[i].len = 1;
    }
    for (i = n - 1; i >= 0; i--) {
      u = S[i][0] - '0';
      flag = -1;
      num = 0, slen = len = 1;
      for (j = 3; j < S[i].size(); j++) {
        flag = 1;
        v = S[i][j] - '0';
        if (a[v].len == 0) continue;
        if (a[v].num != -1)
          len = a[v].len, v = a[v].num;
        else
          len = 10LL;
        len %= MOD;
        slen *= len;
        slen %= MOD;
        num = (num * len + v) % MOD;
      }
      a[u].len = slen;
      a[u].num = num;
      if (flag == -1) a[u].len = 0;
    }
    len = 10;
    for (i = 0; i < s.size(); i++) {
      u = s[i] - '0';
      len = 10;
      if (a[u].len == 0) continue;
      num = u;
      if (a[u].num != -1) num = a[u].num, len = a[u].len;
      cnt = (cnt * len + num) % MOD;
    }
    cout << cnt << endl;
  }
  return 0;
}
