#include <bits/stdc++.h>
using namespace std;
char s[400010];
char qs[400010];
int pw[11];
int val[11];
int b[400010];
int n;
vector<char> q[400010];
inline void add(int &a, int b) {
  a += b;
  if (a >= 1000000007) a -= 1000000007;
}
inline void mul(int &a, int b) { a = (long long)a * b % 1000000007; }
int main() {
  int i;
  while (scanf("%s", s) != EOF) {
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
      scanf("%s", qs);
      int d = qs[0] - '0';
      b[i] = d;
      q[i].clear();
      for (int j = 3; qs[j]; ++j) q[i].push_back(qs[j]);
    }
    for (i = 0; i < 10; i++) {
      pw[i] = 10;
      val[i] = i;
    }
    for (i = n - 1; i >= 0; i--) {
      int d = b[i];
      int l = q[i].size();
      int ans = 0;
      int ml = 1;
      for (int j = 0; j < l; j++) {
        int d = q[i][j] - '0';
        mul(ans, pw[d]);
        add(ans, val[d]);
        mul(ml, pw[d]);
      }
      pw[d] = ml;
      val[d] = ans;
    }
    int ans = 0;
    for (i = 0; s[i]; i++) {
      int d = s[i] - '0';
      mul(ans, pw[d]);
      add(ans, val[d]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
