#include <bits/stdc++.h>
using namespace std;
long long mod_plus(long long a, long long b) { return (a + b) % 1000000007; }
long long mod_mul(long long a, long long b) { return (a * b) % 1000000007; }
long long m[10], g[10];
long long go(char *second, int now) {
  if (now < 0) return 0;
  return mod_plus(m[second[now] - '0'],
                  mod_mul(g[second[now] - '0'], go(second, now - 1)));
}
void process(int dig, char *second) {
  int len = strlen(second);
  m[dig] = go(second, len - 1);
  long long temp = 1;
  for (len = 0; second[len]; len++) temp = mod_mul(temp, g[second[len] - '0']);
  g[dig] = temp;
  return;
}
char str[100005], tt[100005];
vector<string> all;
int main() {
  scanf("%s", str);
  int n, i;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%s", tt);
    string now = tt;
    all.push_back(now);
  }
  for (i = 0; i < 10; i++) {
    m[i] = i;
    g[i] = 10;
  }
  for (i = n - 1; i >= 0; i--) {
    strcpy(tt, all[i].c_str());
    process(tt[0] - '0', tt + 3);
  }
  cout << go(str, strlen(str) - 1) << endl;
  return 0;
}
