#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9, M = 19, Mod = 1e9 + 7;
long long d[M], l[M];
string t[N], s;
int q[N], m;
long long sl[N];
long long power(long long a, long long b) {
  long long o = 1;
  while (b) {
    if (b & 1) o = o * a % Mod;
    a = a * a % Mod;
    b >>= 1;
  }
  return o;
}
long long get(string &s, long long &L) {
  long long o = 0;
  long long ol = 0;
  for (int i = 0; i < int(s.size()); ++i) {
    o = o * power(10, l[s[i] - '0']) % Mod + d[s[i] - '0'];
    ol += l[s[i] - '0'];
    ol %= (Mod - 1);
    o %= Mod;
  }
  L = ol;
  return o;
}
int main() {
  cin >> s;
  scanf("%d", &m);
  for (int i = 1; i <= m; ++i) {
    string tp;
    cin >> tp;
    q[i] = tp[0];
    tp.erase(tp.begin(), tp.begin() + 3);
    t[i] = tp;
  }
  for (int i = 0; i <= 9; ++i) d[i] = i, l[i] = 1;
  for (int i = m; i >= 1; --i) {
    d[q[i] - '0'] = get(t[i], l[q[i] - '0']);
  }
  long long o = get(s, *new long long);
  cout << o << endl;
  return 0;
}
