#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
string s;
char tmp[maxn];
int n;
struct node {
  int x;
  string to;
} p[maxn];
long long tenpower[10];
long long val[10];
int main() {
  cin >> s;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%s", &tmp);
    p[i].to = tmp + 3;
    p[i].x = tmp[0] - '0';
  }
  for (int i = 0; i < 10; i++) {
    val[i] = i;
    tenpower[i] = 10;
  }
  for (int i = n - 1; i >= 0; i--) {
    long long t = 1LL;
    long long v = 0LL;
    int sz = p[i].to.length();
    for (int j = 0; j < sz; j++) {
      int now = p[i].to[j] - '0';
      t = (t * tenpower[now]) % mod;
      v = (v * tenpower[now] + val[now]) % mod;
    }
    int x = p[i].x;
    val[x] = v;
    tenpower[x] = t;
  }
  long long ans = 0;
  for (int i = 0; i < s.length(); i++) {
    int now = s[i] - '0';
    ans = (ans * tenpower[now] + val[now]) % mod;
  }
  printf("%d\n", (int)ans);
  return 0;
}
