#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 5;
const long long linf = 1e18 + 5;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
int n, m;
pair<int, int> p[15];
string a, b[N];
pair<int, int> get(string s) {
  long long power = 1, val = 0;
  for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++) {
    int x = *it - '0';
    val = (val * p[x].first + p[x].second) % mod;
    power = power * p[x].first % mod;
  }
  return make_pair(power, val);
}
int main() {
  cin >> a;
  n = a.size();
  cin >> m;
  for (int i = 1; i <= m; i++) cin >> b[i];
  for (int i = 0; i <= 9; i++) p[i] = make_pair(10, i);
  for (int i = m; i >= 1; i--) {
    int x = b[i][0] - '0';
    p[x] = get(b[i].substr(3, b[i].size() - 3));
  }
  cout << get(a).second << '\n';
  return 0;
}
