#include <bits/stdc++.h>
using namespace std;
const int Mod = 1e9 + 7;
const int N = 1111111;
char s[N];
long long d[10], pw[10];
int n;
string t[N];
int main() {
  scanf("%s", s);
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    char p[111111];
    scanf("%s", p);
    t[i] = p;
  }
  for (int i = 0; i < 10; i++) d[i] = i, pw[i] = 10;
  for (int i = n - 1; i >= 0; i--) {
    int pos = t[i][0] - '0';
    long long cur = 0, ten = 1;
    for (int j = t[i].length() - 1; j >= 3; j--) {
      int k = t[i][j] - '0';
      cur = cur + d[k] * ten;
      cur %= Mod;
      ten = ten * pw[k] % Mod;
    }
    d[pos] = cur;
    pw[pos] = ten;
  }
  long long ans = 0, p = 1;
  for (int i = strlen(s) - 1; i >= 0; i--) {
    ans = (ans + d[s[i] - '0'] * p) % Mod;
    p = (p * pw[s[i] - '0']) % Mod;
  }
  printf("%d\n", ans);
}
