#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const int maxn = 100000 + 10;
string s, str[maxn];
int n;
long long bit[10], len[10];
bool used[10];
long long Qpow(int x, long long b) {
  long long tmp = x, ret = 1;
  while (b) {
    if (b & 1) {
      ret *= tmp;
      ret %= mod;
    }
    tmp *= tmp;
    tmp %= mod;
    b >>= 1;
  }
  return ret;
}
void cal(string ss) {
  int sz = ss.length(), x = ss[0] - '0';
  long long tmp = 1, ans = 0;
  for (int i = sz - 1; i > 2; i--) {
    int now = ss[i] - '0';
    ans += tmp * bit[now];
    ans %= mod;
    tmp *= len[now];
    tmp %= mod;
  }
  bit[x] = ans;
  len[x] = tmp;
  used[x] = true;
}
int main() {
  cin >> s >> n;
  for (int i = 0; i < n; i++) cin >> str[i];
  for (int i = 0; i < 10; i++) bit[i] = i, len[i] = 10, used[i] = false;
  for (int i = n - 1; i >= 0; i--) cal(str[i]);
  int sz = s.length();
  long long ans = 0, tmp = 1;
  for (int i = sz - 1; i >= 0; i--) {
    int now = s[i] - '0';
    ans += bit[now] * tmp % mod;
    ans %= mod;
    tmp *= len[now];
    tmp %= mod;
  }
  cout << ans << endl;
  return 0;
}
