#include <bits/stdc++.h>
using namespace std;
long long j, x, y, n, m, cnt, i, sixs[10], b[30];
long long val[300], len[300];
vector<string> s;
string k, r;
long long x10(long long x) {
  if (x == 0) return 1;
  long long tmp = x10(x / 2);
  if (x % 2) return (((10LL * tmp) % 1000000007) * tmp) % 1000000007;
  return (tmp * tmp) % 1000000007;
}
int main() {
  cin >> k;
  r = "0->";
  r.append(k);
  s.push_back(r);
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> k;
    s.push_back(k);
  }
  for (i = '0'; i <= '9'; i++) {
    val[i] = i - '0';
    len[i] = 1;
  }
  for (i = n; i >= 0; i--) {
    long long l = 0, v = 0;
    for (j = 3; j < s[i].size(); j++) {
      l = (l + len[s[i][j]]) % (1000000007 - 1);
      if (j > 3)
        v = ((x10(len[s[i][j]]) * v) % 1000000007 + val[s[i][j]]) % 1000000007;
      else
        v = val[s[i][j]];
    }
    len[s[i][0]] = l;
    val[s[i][0]] = v;
  }
  cout << val['0'] << endl;
  return 0;
}
