Problem: Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type "di → ti", that means "replace all digits di in string s with substrings equal to ti". For example, if s = 123123, then query "2 → 00" transforms s to 10031003, and query "3 → " ("replace 3 by an empty string") transforms it to s = 1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109 + 7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.

Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!

Input

The first line contains string s (1 ≤ |s| ≤ 105), consisting of digits — the string before processing all the requests.

The second line contains a single integer n (0 ≤ n ≤ 105) — the number of queries.

The next n lines contain the descriptions of the queries. The i-th query is described by string "di->ti", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.

Output

Print a single integer — remainder of division of the resulting number by 1000000007 (109 + 7).

Examples

Input

123123
1
2-&gt;00


Output

10031003


Input

123123
1
3-&gt;


Output

1212


Input

222
2
2-&gt;0
0-&gt;7


Output

777


Input

1000000008
0


Output

1

Note

Note that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample).

Solution (in CPP):

Solution:
#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;
}