#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int M = 1000000007;
long long P10[N];
long long p10(long long k) {
  if (k < N) return P10[k];
  long long x = p10(k / 2);
  return k & 1 ? x * x % M * 10 % M : x * x % M;
}
pair<long long, long long> operator+(pair<long long, long long> a,
                                     pair<long long, long long> b) {
  return pair<long long, long long>((a.first + b.first) % (M - 1),
                                    (a.second * p10(b.first) + b.second) % M);
}
pair<long long, long long> Hash[10];
int m;
string S[N];
char a[N];
void apply(const string &s) {
  pair<long long, long long> Sum(0, 0);
  for (int i = 3; s[i]; i++) Sum = Sum + Hash[s[i] - '0'];
  Hash[s[0] - '0'] = Sum;
}
int main() {
  P10[0] = 1;
  P10[1] = 10;
  for (int i = 2; i < N; i++) P10[i] = P10[i - 1] * 10 % M;
  for (int i = 0; i < 10; i++) Hash[i] = pair<long long, long long>(1, i);
  scanf("%s", a);
  static char b[N];
  int m;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%s", b);
    S[i] = b;
  }
  for (int i = m; i >= 1; i--) apply(S[i]);
  pair<long long, long long> Sum(0, 0);
  for (int i = 0; a[i]; i++) Sum = Sum + Hash[a[i] - '0'];
  cout << Sum.second << endl;
}
