#include <bits/stdc++.h>
using namespace std;
const long double Eps = 1e-8;
inline bool is_zero(long double x) { return (x >= -Eps) && (x <= Eps); }
const int Mod = 1000000007;
string str;
int Q;
int val[10], powten[10];
vector<string> zm;
void input() {
  cin >> str;
  cin >> Q;
  zm.resize(Q);
  for (int i = 0; i < (Q); i++) {
    cin >> zm[i];
  }
}
int cnt[10];
int main() {
  ios_base::sync_with_stdio(0);
  input();
  for (int i = 0; i < (10); i++) {
    val[i] = i;
    powten[i] = 10;
  }
  for (int i = (Q - 1); i >= (0); i--) {
    int from = (zm[i][0] - '0');
    int nval = 0;
    int ncoef = 1;
    for (int j = 3; j < zm[i].size(); j++) {
      int c = zm[i][j] - '0';
      ncoef = ((long long)ncoef * powten[c]) % Mod;
      nval = ((long long)nval * powten[c] + val[c]) % Mod;
    }
    powten[from] = ncoef;
    val[from] = nval;
  }
  int totRes = 0;
  for (int i = 0; i < (str.size()); i++) {
    int c = str[i] - '0';
    totRes = ((long long)totRes * powten[c] + val[c]) % Mod;
  }
  cout << totRes << endl;
}
