#include <bits/stdc++.h>
using namespace std;
char s[100002];
int n, ll, c;
long long a[15], len[15], x, l;
string t[100002];
int main() {
  scanf("%s", s);
  scanf("%d", &n);
  for (int i = 0; i <= 9; i++) a[i] = i, len[i] = 10;
  for (int i = 1; i <= n; i++) cin >> t[i];
  for (int i = n; i >= 1; i--) {
    x = 0, l = 1;
    ll = t[i].size();
    for (int j = ll - 1; j > 2; j--) {
      c = t[i][j] - 48;
      x = (x + a[c] * l) % 1000000007;
      l = (l * len[c]) % 1000000007;
    }
    c = t[i][0] - 48;
    a[c] = x;
    len[c] = l;
  }
  ll = strlen(s);
  l = 1;
  x = 0;
  for (int i = ll - 1; i >= 0; i--) {
    c = s[i] - 48;
    x = (x + a[c] * l) % 1000000007;
    l = (l * len[c]) % 1000000007;
  }
  cout << x;
  return 0;
}
