#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  std::cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
template <typename T, typename U>
static inline void amin(T& x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
static inline void amax(T& x, U y) {
  if (x < y) x = y;
}
long long max(long long a, long long b) { return (a > b) ? a : b; }
long long min(long long a, long long b) { return (a < b) ? a : b; }
long long val[10];
long long len[10];
long long power(long long x, unsigned long long y, unsigned long long m) {
  if (y == 0) return 1;
  long long p = power(x, y / 2, m) % m;
  p = (p * p) % m;
  return (y % 2 == 0) ? p : (x * p) % m;
}
void add(long long& a, long long& l, long long num) {
  a += (power(10, l % (1000000007 - 1), 1000000007) * val[num]) % 1000000007;
  a %= 1000000007;
  l += len[num];
  l %= (1000000007 - 1);
}
long long solve() {
  string s;
  cin >> s;
  for (long long i = 0; i < 10; i++) val[i] = i, len[i] = 1;
  long long m;
  cin >> m;
  string tt[m];
  for (long long i = 0; i < m; ++i) cin >> tt[i];
  while (m--) {
    string t = tt[m];
    long long num = t[0] - '0';
    long long len = 0;
    long long cv = 0;
    reverse(t.begin(), t.end());
    for (auto i : t) {
      if (i == '>') break;
      add(cv, len, i - '0');
    }
    val[num] = cv;
    ::len[num] = len;
  }
  long long ans = 0;
  long long cl = 0;
  reverse(s.begin(), s.end());
  for (auto i : s) {
    add(ans, cl, i - '0');
  }
  cout << ans;
  return 0;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
