#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
const int mod = 1e9 + 7;
long long powe(long long x, long long e) {
  long long res = 1;
  while (e) {
    if (e & 1) res = res * x % mod;
    e >>= 1;
    x = x * x % mod;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s, a;
  cin >> s;
  int q;
  cin >> q;
  vector<long long> transform_val(10), transform_len(10);
  for (int i = 0; i < 10; i++) transform_val[i] = i, transform_len[i] = 1;
  vector<string> queries;
  queries.push_back("0->" + s);
  while (q--) {
    cin >> a;
    queries.push_back(a);
  }
  for (auto it = queries.rbegin(); it != queries.rend(); it++) {
    a = *it;
    int v = a[0] - '0';
    long long to_len = 0, to_val = 0;
    for (int i = a.size() - 1; i >= 3; i--) {
      int idx = a[i] - '0';
      to_val = (to_val + transform_val[idx] * powe(10, to_len) % mod) % mod;
      to_len += transform_len[idx];
    }
    transform_val[v] = to_val;
    transform_len[v] = to_len % (mod - 1);
  }
  cout << transform_val[0];
}
