#include <bits/stdc++.h>
using namespace std;
template <class T>
void dbgarr(T a, long long s, long long len) {
  for (long long i = s; i < s + len; i++) cerr << a[i] << ' ';
  cout << endl;
}
const long long MOD = 1e9 + 7;
long long a[11], b[11];
char buffer[200000];
long long s[200000];
vector<int> vec[200000];
long long t[200000];
long long po(long long k) {
  if (k == 0) return 1;
  long long tmp = po(k / 2);
  tmp = (tmp * tmp) % MOD;
  if (k % 2 == 1) tmp = tmp * 10 % MOD;
  return tmp;
}
int main() {
  scanf("%s", buffer);
  long long n = strlen(buffer);
  for (long long i = 0; i < n; i++) s[i] = buffer[i] - '0';
  long long m;
  scanf("%lld", &m);
  for (long long i = 1; i <= m; i++) {
    scanf("%s", buffer);
    t[i] = buffer[0] - '0';
    vec[i].clear();
    long long len = strlen(buffer);
    for (long long j = 3; j < len; j++) vec[i].push_back(buffer[j] - '0');
  }
  for (long long i = 0; i <= 9; i++) {
    a[i] = i;
    b[i] = 1;
  }
  for (long long i = m; i >= 1; i--) {
    long long tmp = 0;
    long long len = vec[i].size();
    long long newB = 0;
    long long newA = 0;
    for (long long j = len - 1; j >= 0; j--) {
      newA = (newA + a[vec[i][j]] * po(newB)) % MOD;
      newB = (newB + b[vec[i][j]]) % (MOD - 1);
    }
    a[t[i]] = newA;
    b[t[i]] = newB;
  }
  long long newA = 0;
  long long newB = 0;
  for (long long i = n - 1; i >= 0; i--) {
    newA = (newA + a[s[i]] * po(newB)) % MOD;
    newB = (newB + b[s[i]]) % (MOD - 1);
  }
  printf("%lld\n", newA);
}
