#include <bits/stdc++.h>
using namespace std;
int n;
long long M = 1000000007;
struct change {
  int d;
  string s;
} p[100005];
long long wei[10], val[10];
void compute() {
  for (int i = 0; i < 10; i++) wei[i] = 10, val[i] = i;
  for (int i = n - 1; i >= 0; i--) {
    int d = p[i].d, l = p[i].s.length();
    string s = p[i].s;
    long long v = 0, w = 1;
    for (int j = 0; j < l; j++) {
      v *= wei[s[j] - '0'];
      v += val[s[j] - '0'];
      v %= M;
      w *= wei[s[j] - '0'];
      w %= M;
    }
    val[d] = v;
    wei[d] = w;
  }
}
int main() {
  string s;
  cin >> s >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d->", &p[i].d);
    getline(cin, p[i].s);
  }
  compute();
  int l = s.length();
  long long ans = 0;
  for (int i = 0; i < l; i++) {
    ans *= wei[s[i] - '0'];
    ans += val[s[i] - '0'];
    ans %= M;
  }
  cout << ans << endl;
  return 0;
}
