#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000LL;
void scanInt(int &x) {
  register int c = getchar();
  x = 0;
  int neg = 0;
  for (; ((c < 48 || c > 57) && c != '-'); c = getchar())
    ;
  if (c == '-') {
    neg = 1;
    c = getchar();
  }
  for (; c > 47 && c < 58; c = getchar()) {
    x = (x << 1) + (x << 3) + c - 48;
  }
  if (neg) x = -x;
}
char inp[100010];
vector<string> que;
int main(int argc, char const *argv[]) {
  int n, len, d;
  long long ans, mult;
  scanf("%s", inp);
  scanInt(n);
  string query;
  int digit[10], mul[10];
  for (int i = 0; i < 10; i++) {
    digit[i] = i;
    mul[i] = 10;
  }
  for (int i = 0; i < n; i++) {
    cin >> query;
    que.push_back(query);
  }
  for (int i = n - 1; i >= 0; i--) {
    len = que[i].size();
    d = (que[i][0] - '0');
    ans = 0;
    mult = 1;
    for (int j = len - 1; j > 2; j--) {
      ans += digit[que[i][j] - '0'] * mult;
      ans %= 1000000007;
      mult = (mult * mul[que[i][j] - '0']) % 1000000007;
    }
    digit[d] = ans;
    mul[d] = mult;
  }
  ans = 0;
  mult = 1;
  for (int i = strlen(inp) - 1; i >= 0; i--) {
    ans += digit[inp[i] - '0'] * mult;
    ans %= 1000000007;
    mult = (mult * mul[inp[i] - '0']) % 1000000007;
  }
  cout << ans << endl;
  return 0;
}
