#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const long long B = 1e9 + 7;
char s[N], o[N];
int n;
long long v[20], l[20];
long long mypow(long long a, long long n) {
  if (n == 0) return 1;
  if (n & 1) return mypow(a, n - 1) * a % B;
  return mypow(a * a % B, n >> 1);
}
struct state {
  int r;
  vector<int> vi;
  void set(char *str) {
    int len = strlen(str);
    r = str[0] - '0';
    vi.clear();
    for (int i = 3; i < len; ++i) {
      if (str[i] >= '0' && str[i] <= '9') vi.push_back(str[i] - '0');
    }
  }
  void solve() {
    long long val = 0, len = 0;
    for (int i = 0; i < vi.size(); ++i) {
      int u = vi[i];
      len += l[u];
      val = (val * mypow(10, l[u]) + v[u]) % B;
      len %= (B - 1);
    }
    v[r] = val;
    l[r] = len;
  }
} com[N];
void init() {
  for (int i = 0; i < 10; ++i) {
    v[i] = i;
    l[i] = 1;
  }
  for (int i = 0; i < n; ++i) {
    scanf("%s", o);
    com[i].set(o);
  }
  for (int i = n - 1; i >= 0; --i) {
    com[i].solve();
  }
}
int main() {
  scanf("%s", s);
  scanf("%d", &n);
  init();
  int len = strlen(s);
  long long ans = 0;
  for (int i = 0; i < len; ++i) {
    int u = s[i] - '0';
    ans = (ans * mypow(10, l[u]) + v[u]) % B;
  }
  printf("%I64d\n", ans);
  return 0;
}
