#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, md = 1e9 + 7;
char str[N], foo[N];
char digit[N];
vector<char> v[N];
int m;
inline void add(int &a, int b) {
  a += b;
  if (a >= md) a -= md;
}
inline int mul(int a, int b) { return (long long)a * b % md; }
int main(void) {
  scanf("%s%d", str, &m);
  int n = strlen(str);
  for (int i = 0; i < m; ++i) {
    scanf("%s", foo);
    digit[i] = foo[0];
    for (int j = 3; foo[j]; ++j) {
      v[i].push_back(foo[j]);
    }
  }
  int pw[10], val[10];
  for (int i = 0; i < 10; ++i) pw[i] = 10, val[i] = i;
  for (int i = m - 1; i >= 0; --i) {
    int sum = 0, pot = 1;
    for (char c : v[i]) {
      c -= '0';
      sum = mul(sum, pw[c]);
      add(sum, val[c]);
      pot = mul(pot, pw[c]);
    }
    val[digit[i] - '0'] = sum;
    pw[digit[i] - '0'] = pot;
  }
  int ret = 0;
  for (int i = 0; str[i]; ++i)
    ret = mul(ret, pw[str[i] - '0']), add(ret, val[str[i] - '0']);
  printf("%d\n", ret);
  return 0;
}
