#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long INF = 4e18;
const int inf = 2e9;
const int N = 1e5 + 5;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
int mul(int a, int b) { return (int)((long long)a * b % mod); }
int add(int a, int b) {
  int ret = a + b;
  if (ret >= mod) ret -= mod;
  return ret;
}
char s[N];
vector<char> q[N];
int dig[N], val[10], po[10];
int main(void) {
  int i, j, tt;
  scanf("%s %d", s, &tt);
  for (i = 0; i < tt; ++i) {
    char junk[N];
    scanf("%s", junk);
    int len = strlen(junk);
    dig[i] = junk[0] - '0';
    for (j = 3; j <= len - 1; ++j) q[i].push_back(junk[j]);
  }
  for (i = 0; i < 10; ++i) {
    val[i] = i;
    po[i] = 10;
  }
  for (i = tt - 1; i >= 0; --i) {
    int sz = (int)q[i].size();
    int nval = 0;
    int npow = 1;
    for (j = 0; j < sz; ++j) {
      int d = q[i][j] - '0';
      nval = mul(nval, po[d]);
      nval = add(nval, val[d]);
      npow = mul(npow, po[d]);
    }
    val[dig[i]] = nval;
    po[dig[i]] = npow;
  }
  int ans = 0, n = strlen(s);
  for (i = 0; i < n; ++i) {
    int d = s[i] - '0';
    ans = mul(ans, po[d]);
    ans = add(ans, val[d]);
  }
  cout << ans << endl;
  return 0;
}
