#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
const long long MOD = 1e9 + 7;
string s;
string str[N];
int n;
long long digit[N], val[N], len[N];
long long power[N];
long long BigMod(long long b, long long p, long long m) {
  long long ret = 1;
  while (p > 0) {
    if (p % 2 == 1) ret = (ret * b) % m;
    p /= 2;
    b = (b * b) % m;
  }
  return ret % m;
}
long long calculate(string &S, int st) {
  long long ret = 0;
  for (int i = st; i < S.size(); i++) {
    long long d = S[i] - '0';
    ret = (ret * BigMod(10, len[d], MOD) + val[d]) % MOD;
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  power[0] = 1;
  for (int i = 1; i < N; i++) power[i] = (power[i - 1] * 10) % MOD;
  cin >> s;
  cin >> n;
  getline(cin, str[0]);
  for (int i = 1; i <= n; i++) {
    getline(cin, str[i]);
    digit[i] = str[i][0] - '0';
  }
  for (int i = 0; i < 10; i++) val[i] = i, len[i] = 1;
  for (int i = n; i >= 1; i--) {
    long long sum = calculate(str[i], 3);
    val[digit[i]] = sum;
    long long L = 0;
    for (int j = 3; j < str[i].size(); j++) L += len[str[i][j] - '0'];
    len[digit[i]] = L % (MOD - 1);
  }
  cout << calculate(s, 0) << "\n";
  return 0;
}
