#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const int MOD = 1e9 + 7;
char s[MAXN], d[MAXN];
string t[MAXN];
int v[10], b[10];
char buf[MAXN];
int main() {
  while (~scanf("%s", s)) {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < (n); i++) {
      t[i] = "";
      scanf("%s", buf);
      d[i] = buf[0];
      for (int j = 3; buf[j]; ++j) {
        t[i] += buf[j];
      }
    }
    for (int i = 0; i < (10); i++) v[i] = i, b[i] = 10;
    for (int i = n - 1; i >= 0; --i) {
      int x = d[i] - '0', nv = 0, nb = 1;
      for (__typeof(t[i].begin()) it = t[i].begin(); it != t[i].end(); it++) {
        int y = *it - '0';
        nv = (1ll * nv * b[y] + v[y]) % MOD;
        nb = 1ll * nb * b[y] % MOD;
      }
      v[x] = nv;
      b[x] = nb;
    }
    int ans = 0;
    for (int i = 0; s[i]; ++i) {
      int x = s[i] - '0';
      ans = (1ll * ans * b[x] + v[x]) % MOD;
    }
    cout << ans << endl;
  }
  return 0;
}
