#include <bits/stdc++.h>
using namespace std;
enum { base = int(1e9 + 7), INF = int(1e9), N = int(1e5 + 10) };
int main() {
  string s;
  cin >> s;
  int n;
  scanf("\n%d\n", &n);
  int val[10], len[10];
  for (int i = 0; i < 10; i++) val[i] = i, len[i] = 10;
  static string t[N];
  static int num[N] = {0};
  num[0] = 0;
  t[0] = "->" + s;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &num[i]);
    cin >> t[i];
    scanf("\n");
  }
  n++;
  for (int i = n - 1; i >= 0; i--) {
    long long int v = 0, l = 1;
    for (int j = 2; j < t[i].size(); j++) {
      int d = t[i][j] - '0';
      v = (((v % base) * len[d]) % base + val[d]) % base;
      l = (l * len[d]) % base;
    }
    val[num[i]] = v;
    len[num[i]] = l;
  }
  cout << val[0];
  return 0;
}
