#include <bits/stdc++.h>
using namespace std;
int abs(int x) {
  if (x < 0) return -x;
  return x;
}
pair<long long, long long> c[10];
pair<int, string> t[100010];
pair<long long, long long> count(string s) {
  long long a = 0LL, b = 1LL;
  for (int i = 0; i < (int)s.size(); i++) {
    a = ((a * c[s[i] - '0'].second) % 1000000007 + c[s[i] - '0'].first) %
        1000000007;
    b = (b * c[s[i] - '0'].second) % 1000000007;
  }
  return (pair<long long, long long>(a, b));
}
int main() {
  ios_base::sync_with_stdio(false);
  string s, ss;
  cin >> s;
  int n;
  cin >> n;
  for (int i = 0; i < 10; i++) c[i] = pair<long long, long long>(i, 10);
  for (int i = 0; i < n; i++) {
    cin >> ss;
    t[i].first = ss[0] - '0';
    t[i].second = ss.substr(3, ss.size());
  }
  for (int i = n - 1; i > -1; i--) {
    c[t[i].first] = count(t[i].second);
  }
  cout << count(s).first << endl;
  return 0;
}
