#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const double PI = atan(1.0) * 4;
const int inf = 0x3f3f3f3f;
template <class T>
bool chmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool chmax(T& a, const T& b) {
  return b > a ? a = b, 1 : 0;
}
template <class T>
int sgn(T first) {
  return (first > eps) - (first < -eps);
}
const int maxn = 100000 + 10;
const int mod = 1000000007;
char str[maxn], str2[maxn];
string pp[maxn];
int n;
int weishu[10], num[10], zhuanyi[maxn];
void init() {
  scanf("%s", str);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", str2);
    zhuanyi[i] = str2[0] - '0';
    pp[i] = str2 + 3;
  }
  for (int i = 0; i < 10; i++) {
    weishu[i] = 10;
    num[i] = i;
  }
}
void solve() {
  for (int i = n; i >= 1; i--) {
    long long t1 = 1, t2 = 0;
    for (int j = 0; j < pp[i].size(); j++) {
      int temp = pp[i][j] - '0';
      t1 = (t1 * weishu[temp]) % mod;
      t2 = (t2 * weishu[temp] % mod + num[temp]) % mod;
    }
    weishu[zhuanyi[i]] = t1;
    num[zhuanyi[i]] = t2;
  }
  long long ans = 0;
  for (int i = 0; i < strlen(str); i++) {
    int temp = str[i] - '0';
    ans = (ans * weishu[temp] % mod + num[temp]) % mod;
  }
  printf("%I64d\n", ans);
}
int main() {
  init();
  solve();
  return 0;
}
