#include <bits/stdc++.h>
using std::pair;
using std::vector;
template <class T>
inline vector<T> &operator<<(vector<T> &x, const T &y) {
  x.push_back(y);
  return x;
}
using std::map;
using std::set;
template <class T>
inline set<T> &operator<<(set<T> &x, const T &y) {
  x.insert(y);
  return x;
}
using std::list;
template <class T>
inline list<T> &operator<<(list<T> &x, const T &y) {
  x.push_back(y);
  return x;
}
template <class T>
inline list<T> &operator>>(list<T> &x, const T &y) {
  x.push_front(y);
  return x;
}
inline void read(int &x) {
  char c = getchar();
  while (c < '0' && c != '-') c = getchar();
  bool neg = false;
  if (c == '-') {
    neg = true;
    c = getchar();
  }
  x = 0;
  while (c >= '0') x = (x << 3) + (x << 1) + c - '0', c = getchar();
  if (neg) x = -x;
}
inline void read(long long &x) {
  char c = getchar();
  while (c < '0' && c != '-') c = getchar();
  bool neg = false;
  if (c == '-') {
    neg = true;
    c = getchar();
  }
  x = 0;
  while (c >= '0') x = (x << 3) + (x << 1) + c - '0', c = getchar();
  if (neg) x = -x;
}
inline void read(double &x) { scanf("%lf", &x); }
inline void read(long double &x) {
  double tmp;
  scanf("%lf", &tmp);
  x = tmp;
}
inline void write(int x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x >= 1000000000)
    goto x9;
  else if (x >= 100000000)
    goto x8;
  else if (x >= 10000000)
    goto x7;
  else if (x >= 1000000)
    goto x6;
  else if (x >= 100000)
    goto x5;
  else if (x >= 10000)
    goto x4;
  else if (x >= 1000)
    goto x3;
  else if (x >= 100)
    goto x2;
  else if (x >= 10)
    goto x1;
  else
    goto x0;
x9:
  putchar(x / 1000000000 + '0');
  x %= 1000000000;
x8:
  putchar(x / 100000000 + '0');
  x %= 100000000;
x7:
  putchar(x / 10000000 + '0');
  x %= 10000000;
x6:
  putchar(x / 1000000 + '0');
  x %= 1000000;
x5:
  putchar(x / 100000 + '0');
  x %= 100000;
x4:
  putchar(x / 10000 + '0');
  x %= 10000;
x3:
  putchar(x / 1000 + '0');
  x %= 1000;
x2:
  putchar(x / 100 + '0');
  x %= 100;
x1:
  putchar(x / 10 + '0');
  x %= 10;
x0:
  putchar(x + '0');
}
inline void write(const long long x) { printf("%I64d", x); }
template <class T>
inline void writeln(const T x) {
  write(x);
  putchar('\n');
}
template <class T>
inline void read(T *first, int n) {
  for (int i = (0); i < (n); ++i) read(first[i]);
}
template <class T>
inline void writeln(const T *first, int n, char spc = ' ') {
  for (int i = (0); i < (n); ++i) {
    write(first[i]);
    putchar(spc);
  }
  putchar('\n');
}
template <class T>
inline void writelnln(const T *first, int n) {
  for (int i = (0); i < (n); ++i) {
    write(first[i]);
    putchar('\n');
  }
}
template <class T>
inline T min(const T x, const T y) {
  if (x < y)
    return x;
  else
    return y;
}
template <class T>
inline T &min2(const T &x, const T &y) {
  if (x < y)
    return x;
  else
    return y;
}
template <class T>
inline T max(const T x, const T y) {
  if (x > y)
    return x;
  else
    return y;
}
template <class T>
inline T &max2(const T &x, const T &y) {
  if (x > y)
    return x;
  else
    return y;
}
template <class T>
inline T sqr(const T x) {
  return x * x;
}
template <class T>
inline T sqr2(const T &x) {
  return x * x;
}
template <class T>
inline T &gmax(T &x, const T y) {
  if (x < y) x = y;
}
template <class T>
inline T &gmin(T &x, const T y) {
  if (x > y) x = y;
}
struct modint {
  int first;
  inline modint(int _a = 0) : first(_a) {
    if (first >= 1) first -= 1;
    if (first < 0) first += 1;
  }
};
inline modint operator+(const modint x, const modint y) {
  return modint(x.first + y.first);
}
inline modint operator-(const modint x, const modint y) {
  return modint(x.first - y.first + 1);
}
inline modint operator*(const modint x, const modint y) {
  return modint((long long)x.first * (long long)y.first % 1LL);
}
inline modint &operator+=(modint &x, const modint y) { x = x + y; }
inline void write(const modint x) { write(x.first); }
inline long long pow(int x) {
  long long ans = 1;
  for (int i = (30); i >= (0); --i) {
    ans = ans * ans % 1000000007LL;
    if (x & (1 << i)) ans = ans * 10LL % 1000000007LL;
  }
  return ans;
}
struct data {
  long long val, len;
  inline data(long long v = 0LL, long long l = 1LL)
      : val(v % 1000000007LL), len(l % 1000000007LL) {}
} d[10];
inline data operator*(const data x, const data y) {
  return data(x.val * y.len + y.val, x.len * y.len);
}
char buf[10000086], *cb = buf;
char b2[100086], s[100086];
int main() {
  for (int i = (0); i < (10); ++i) d[i] = data(i, 10);
  int n;
  scanf("%s%d", s, &n);
  while ((*cb = getchar()) != EOF) ++cb;
  --cb;
  while (n--) {
    while (*cb != '>') --cb;
    int l = *(cb - 2) - '0';
    data tmp;
    for (char *c = cb + 1; *c >= '0'; ++c) tmp = tmp * d[*c - '0'];
    d[l] = tmp;
    --cb;
  }
  data ans;
  int sl = strlen(s);
  for (int i = (0); i < (sl); ++i) ans = ans * d[s[i] - '0'];
  printf("%I64d\n", ans.val);
  return 0;
}
