#include <bits/stdc++.h>
using namespace std;
void RI(int& x) {
  x = 0;
  char c = getchar();
  while (!(c >= '0' && c <= '9' || c == '-')) c = getchar();
  bool flag = 1;
  if (c == '-') {
    flag = 0;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  if (!flag) x = -x;
}
void RII(int& x, int& y) { RI(x), RI(y); }
void RIII(int& x, int& y, int& z) { RI(x), RI(y), RI(z); }
void RC(char& c) {
  c = getchar();
  while (c == ' ' || c == '\n') c = getchar();
}
char RC() {
  char c = getchar();
  while (c == ' ' || c == '\n') c = getchar();
  return c;
}
const long long mod = 1e9 + 7;
const long long LINF = 1e18;
const int INF = 1e9;
const double EPS = 1e-8;
long long a[255];
long long b[255];
char s[1000005];
char t[1000005];
long long POW(int n, int m) {
  if (m == 0) return 1;
  if (m == 1) return n;
  long long tmp = POW(n, m / 2);
  if (m & 1) return tmp * tmp % mod * n % mod;
  return tmp * tmp % mod;
}
int main() {
  gets(s);
  int len = strlen(s);
  int m;
  RI(m);
  vector<pair<int, string> > vec;
  while (m--) {
    int k = (RC() - '0');
    RC();
    RC();
    gets(t);
    vec.push_back(make_pair(k, t));
  }
  for (int i = 0; i <= 9; i++) {
    a[i] = i;
    b[i] = 10;
  }
  for (int i = (int)vec.size() - 1; i >= 0; i--) {
    const string& s = vec[i].second;
    int num = vec[i].first;
    long long tx = 0, ty = 1;
    for (int j = 0; j <= (int)s.size() - 1; j++) {
      tx = (tx * b[s[j] - '0'] + a[s[j] - '0']) % mod;
      ty = (ty * b[s[j] - '0'] % mod);
    }
    a[num] = tx, b[num] = ty;
  }
  long long ans = 0;
  for (int i = 0; i <= len - 1; i++) {
    ans = (ans * b[s[i] - '0'] + a[s[i] - '0']) % mod;
  }
  cout << ans << endl;
}
