#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int INF = 0x3f3f3f3f;
const int MOD = 7 + 1e9;
inline int IN() {
  int x;
  scanf("%d", &x);
  return x;
}
inline void OUT(int x) { printf("%d\n", x); }
int fst[N], nxt[N], to[N], w[N], h;
void jiabian(int u, int v) { nxt[h] = fst[u], to[h] = v, fst[u] = h++; }
char s[N], s1[N];
int a[N];
int r[10], l[10];
string str[N];
int main() {
  scanf("%s", s);
  str[0] = string(s);
  a[0] = 0;
  int n = IN();
  for (int i = 0; i < (10); ++i) r[i] = i, l[i] = 10;
  for (int i = 1; i <= (n); ++i) {
    scanf("%s", s1);
    a[i] = s1[0] - '0';
    str[i] = string(s1 + 3);
  }
  for (int i = n; ~i; --i) {
    int len = str[i].size();
    int _r = 0, _l = 1;
    for (int j = 0; j < (len); ++j) {
      int x = str[i][j] - '0';
      _r = (1LL * _r * l[x] + r[x]) % MOD;
      _l = 1LL * _l * l[x] % MOD;
    }
    r[a[i]] = _r;
    l[a[i]] = _l;
  }
  OUT(r[0]);
  return 0;
}
