#include <bits/stdc++.h>
using namespace std;
static long long m1[10], m2[10], m3[200000], m4[10];
static char s[1000000], s1[1000000];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  vector<string> vc;
  string str;
  cin >> str;
  vc.push_back(str);
  long long n, m = 0, a, b, c;
  char ch, ch1;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> ch >> ch1 >> ch1;
    m3[i + 1] = ch - '0';
    cin.getline(s, 200000);
    str.clear();
    for (int j = 0; s[j]; ++j) str.push_back(s[j]);
    vc.push_back(str);
  }
  for (int i = 0; i < 10; ++i) {
    m1[i] = 1;
    m2[i] = i;
    m4[i] = 10;
  }
  for (int i = n; i >= 0; --i) {
    a = 0;
    b = 0;
    c = 1;
    for (int j = vc[i].size() - 1; j >= 0; --j) {
      b = (b + c * m2[vc[i][j] - '0']) % (long long)1000000007;
      c = (c * m4[vc[i][j] - '0']) % (long long)1000000007;
    }
    m2[m3[i]] = b;
    m4[m3[i]] = c;
  }
  cout << b << endl;
  return 0;
}
