#include <bits/stdc++.h>
using namespace std;
long long s[10], m[10];
const long long md = 1e9 + 7;
string str;
int n;
vector<int> v[100010];
int main() {
  for (int i = 0; i <= 9; i++) {
    m[i] = 10;
    s[i] = i;
  }
  cin >> str;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    string st;
    cin >> st;
    v[i].push_back(st[0] - '0');
    for (int j = 3; j < st.size(); j++) v[i].push_back(st[j] - '0');
  }
  for (int i = n; i >= 1; i--) {
    int cur = v[i][0];
    long long mm = 1, ss = 0;
    for (int j = 1; j < v[i].size(); j++) {
      ss = (ss * m[v[i][j]]) % md;
      mm = (mm * m[v[i][j]]) % md;
      ss += s[v[i][j]];
      if (ss >= md) ss -= md;
    }
    s[cur] = ss;
    m[cur] = mm;
  }
  long long ans = 0;
  for (int i = 0; i < str.size(); i++) {
    ans = (ans * m[str[i] - '0']) % md;
    ans += s[str[i] - '0'];
    if (ans >= md) ans -= md;
  }
  printf("%lld", ans);
  return 0;
}
