#include <bits/stdc++.h>
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
using namespace std;
string str, tmp;
vector<pair<int, string> > a;
vector<pair<long long, long long> > b;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> str >> m;
  n = str.length();
  for (int i = 0; i < m; i++) {
    cin >> tmp;
    a.push_back({tmp[0] - '0', tmp.substr(3, tmp.length() - 2)});
  }
  for (int i = 0; i < 10; i++) b.push_back({10, i});
  for (int i = m - 1; i >= 0; i--) {
    pair<long long, long long> nx = {1, 0};
    for (int j = 0; j < a[i].second.length(); j++) {
      nx.first = (nx.first * b[a[i].second[j] - '0'].first) % MOD;
      nx.second = (nx.second * b[a[i].second[j] - '0'].first +
                   b[a[i].second[j] - '0'].second) %
                  MOD;
    }
    b[a[i].first] = nx;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++)
    ans = (ans * b[str[i] - '0'].first + b[str[i] - '0'].second) % MOD;
  cout << ans << "\n";
  return 0;
}
