#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
template <typename T>
bool mmax(T &m, const T q) {
  if (m < q) {
    m = q;
    return true;
  } else
    return false;
}
template <typename T>
bool mmin(T &m, const T q) {
  if (m > q) {
    m = q;
    return true;
  } else
    return false;
}
void __print(long long first) { cerr << first; }
void __print(long first) { cerr << first; }
void __print(unsigned first) { cerr << first; }
void __print(unsigned long first) { cerr << first; }
void __print(unsigned long long first) { cerr << first; }
void __print(float first) { cerr << first; }
void __print(double first) { cerr << first; }
void __print(long double first) { cerr << first; }
void __print(char first) { cerr << '\'' << first << '\''; }
void __print(const char *first) { cerr << '\"' << first << '\"'; }
void __print(const string &first) { cerr << '\"' << first << '\"'; }
void __print(bool first) { cerr << (first ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &first) {
  cerr << '{';
  __print(first.first);
  cerr << ',';
  __print(first.second);
  cerr << '}';
}
template <typename T>
void __print(const T &first) {
  long long f = 0;
  cerr << '{';
  for (auto &i : first) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const long long N = 1 << 20;
void add(long long &a, long long b) {
  a += b;
  if (a >= 1000000007) a -= 1000000007;
  if (a < 0) a += 1000000007;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(12);
  string s, t;
  long long n;
  cin >> s >> n;
  vector<pair<long long, string>> q(n);
  for (long long i = 0; i < n; i++) {
    cin >> t;
    q[i].first = t[0] - '0';
    q[i].second = t.substr(3, t.size() - 3);
  }
  vector<pair<long long, long long>> dp(10);
  for (long long i = 0; i < 10; i++) {
    dp[i] = {10, i};
  }
  for (long long i = n - 1; i >= 0; i--) {
    long long a = 1, b = 0;
    for (char c : q[i].second) {
      a = (a * dp[c - '0'].first) % 1000000007;
      b = (b * dp[c - '0'].first) % 1000000007;
      add(b, dp[c - '0'].second);
    }
    dp[q[i].first] = {a, b};
  }
  long long ans = 0;
  for (char c : s) {
    ans = (ans * dp[c - '0'].first) % 1000000007;
    add(ans, dp[c - '0'].second);
  }
  cout << ans;
  return 0;
}
