#include <bits/stdc++.h>
using namespace std;
void enable_comma() {}
string tostring(char c) {
  string s = "";
  s += c;
  return s;
}
string tostring(string s) { return "\"" + s + "\""; }
string tostring(const char *c) { return tostring((string)c); }
string tostring(long long x) {
  if (x < 0) return "-" + tostring(-x);
  if (x > 9)
    return tostring(x / 10) + tostring(char('0' + x % 10));
  else
    return tostring(char('0' + x));
}
string tostring(int x) { return tostring((long long)x); }
string tostring(unsigned long long x) {
  if (x > 9)
    return tostring((long long)(x / 10)) + tostring(char('0' + x % 10));
  else
    return tostring(char('0' + x));
}
string tostring(unsigned x) { return tostring((long long)x); }
string tostring(double x) {
  static char res[114];
  sprintf(res, "%lf", x);
  string s = tostring(res);
  return s.substr(1, (int)s.size() - 2);
}
string tostring(long double x) { return tostring((double)x); }
template <class A, class B>
string tostring(pair<A, B> p) {
  return "(" + tostring(p.first) + "," + tostring(p.second) + ")";
}
template <class T>
string tostring(T v) {
  string res = "";
  for (auto p : v) res += (res.size() ? "," : "{") + tostring(p);
  return res.size() ? res + "}" : "{}";
}
template <class A>
string tostring(A *a, int L, int R) {
  return tostring(vector<A>(a + L, a + R + 1));
};
template <class A>
string tostring(A a, int L, int R) {
  return tostring(a.data(), L, R);
}
string tostrings() { return ""; }
template <typename Head, typename... Tail>
string tostrings(Head H, Tail... T) {
  return tostring(H) + " " + tostrings(T...);
}
long long read() {
  long long x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch)) f = ch == '-', ch = getchar();
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return f ? -x : x;
}
template <class T>
void ckmax(T &x, const T y) {
  if (x < y) x = y;
}
template <class T>
void ckmin(T &x, const T y) {
  if (x > y) x = y;
}
const int mod = 1e9 + 7;
int Pow(int x, long long y) {
  y = (y % (mod - 1) + (mod - 1)) % (mod - 1);
  int ans = 1;
  for (; y; y >>= 1, x = (long long)x * x % mod)
    if (y & 1) ans = (long long)ans * x % mod;
  return ans;
}
void Add(int &x, int y) {
  if ((x += y) >= mod) x -= mod;
}
void Del(int &x, int y) {
  if ((x -= y) < 0) x += mod;
}
int Add(int x) { return x >= mod ? x - mod : x; }
int Del(int x) { return x < 0 ? x + mod : x; }
int md(long long x) { return (x % mod + mod) % mod; }
const int N = 1000005;
int n, m;
char s[N];
int a[N];
int id[10], cnt = 0;
vector<int> e[N];
int v10[N], l10[N], f[N];
vector<int> r[N];
int dd[N];
void solve() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  m = read();
  for (int i = (0); i <= (9); i++) id[i] = ++cnt;
  for (int i = (1); i <= (n); i++) a[i] = id[s[i] - '0'];
  while (m--) {
    scanf("%s", s + 1);
    int len = strlen(s + 1);
    int v = s[1] - '0';
    int i = id[v];
    id[v] = ++cnt;
    for (int j = (4); j <= (len); j++) {
      e[i].push_back(id[s[j] - '0']);
      r[id[s[j] - '0']].push_back(i);
      dd[i]++;
    }
  }
  queue<int> Q;
  for (int i = (1); i <= (cnt); i++)
    if (!dd[i]) Q.push(i);
  for (int i = (0); i <= (9); i++) {
    f[id[i]] = 1;
    v10[id[i]] = i;
    l10[id[i]] = 10;
  }
  while (!Q.empty()) {
    int i = Q.front();
    Q.pop();
    for (auto j : r[i])
      if (!--dd[j]) Q.push(j);
    if (f[i]) continue;
    l10[i] = 1;
    v10[i] = 0;
    for (auto j : e[i]) {
      v10[i] = ((long long)v10[i] * l10[j] + v10[j]) % mod;
      l10[i] = (long long)l10[i] * l10[j] % mod;
    }
  }
  enable_comma();
  enable_comma();
  enable_comma();
  enable_comma();
  enable_comma();
  int ans = 0;
  for (int i = (1); i <= (n); i++) {
    ans = ((long long)ans * l10[a[i]] + v10[a[i]]) % mod;
  }
  cout << ans << endl;
}
int main() {
  int T = 1;
  while (T--) solve();
  return 0;
}
