Problem: Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type "di → ti", that means "replace all digits di in string s with substrings equal to ti". For example, if s = 123123, then query "2 → 00" transforms s to 10031003, and query "3 → " ("replace 3 by an empty string") transforms it to s = 1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109 + 7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.

Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!

Input

The first line contains string s (1 ≤ |s| ≤ 105), consisting of digits — the string before processing all the requests.

The second line contains a single integer n (0 ≤ n ≤ 105) — the number of queries.

The next n lines contain the descriptions of the queries. The i-th query is described by string "di->ti", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.

Output

Print a single integer — remainder of division of the resulting number by 1000000007 (109 + 7).

Examples

Input

123123
1
2-&gt;00


Output

10031003


Input

123123
1
3-&gt;


Output

1212


Input

222
2
2-&gt;0
0-&gt;7


Output

777


Input

1000000008
0


Output

1

Note

Note that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample).

Solution (in JAVA):

Solution:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.lang.Long;

public class Test {
	static long Mod = 1000000007;
	static long[][][] dp;
	static long[] pre;

	public static void main(String[] args) throws FileNotFoundException {
		PrintWriter out = new PrintWriter(System.out);
		Scanner in = new Scanner();
		String line = in.next();
		int m = in.nextInt();
		TreeSet<Entry>[] set = new TreeSet[10];
		for (int i = 0; i < 10; i++) {
			set[i] = new TreeSet();
		}
		dp = new long[10][m + 1][2];
		for (long[][] a : dp) {
			for (long[] b : a) {
				Arrays.fill(b, -1);
			}
		}
		pre = new long[1000001];
		pre[0] = 1;
		for (int i = 1; i < pre.length; i++) {
			pre[i] = (pre[i - 1] * 10) % Mod;
		}
		for (int i = 0; i < m; i++) {
			String v = in.next();
			int d = v.charAt(0) - '0';
			String left = v.substring(3);
			set[d].add(new Entry(left, i));
		}
		// System.out.println(Arrays.toString(set));
		out.println(cal(line, set));

		out.close();
	}

	static long modPow(long c) {
		if (c < pre.length) {
			return pre[(int) c];
		}
		long re = modPow(c / 2);
		if (c % 2 == 0) {
			return (re * re) % Mod;
		} else {
			return (((re * re) % Mod) * 10 % Mod) % Mod;
		}
	}

	static long cal(String line, TreeSet<Entry>[] set) {
		long result = 0;
		int n = line.length();
		long c = 1;
		for (int i = n - 1; i >= 0; i--) {
			long[] v = cal2(line.charAt(i) - '0', 0, set);
			if (v[0] == -2) {
			} else {

//				System.out.println(line.charAt(i) + " " + v[1] + " " + v[0]
//						+ " " + c + " " + result);
				long tmp = v[0];
				tmp *= c;
				tmp %= Mod;
				result += tmp;
				result %= Mod;
				c *= v[1];
				c %= Mod;

			}
		}
		return result;
	}

	static long[] cal2(int d, int time, TreeSet<Entry>[] set) {

		Entry e = set[d].ceiling(new Entry(null, time));
		if (e == null) {
			dp[d][time][0] = d;
			dp[d][time][1] = 10;

			return dp[d][time];
		} else if (e.pre.isEmpty()) {
			dp[d][time][0] = -2;
			return dp[d][time];
		}
		if (dp[d][time][0] != -1) {
			// System.out.println(Arrays.toString(dp[d][time]) + " " + d + " " +
			// time);
			return dp[d][time];
		}
		int n = e.pre.length();
		long c = 1;
		long re = 0;
		int count = 0;
		for (int i = n - 1; i >= 0; i--) {
			long[] v = cal2(e.pre.charAt(i) - '0', e.time + 1, set);
			if (v[0] == -2) {
			} else {

				long tmp = v[0];
				tmp *= c;
				tmp %= Mod;
				re += tmp;
				re %= Mod;
				c *= v[1];
				c %= Mod;
				count++;
			}

		}
		// System.out.println(result + " " + d + " " + time + " " + s);
		dp[d][time][0] = re;
		dp[d][time][1] = c;
		if (count == 0) {
			dp[d][time][0] = -2;
		}
		// System.out.println(Arrays.toString(result) + " " + d + " " + time);

		return dp[d][time];
	}

	static class Entry implements Comparable<Entry> {

		String pre;
		int time;

		public Entry(String pre, int time) {
			this.pre = pre;
			this.time = time;
		}

		@Override
		public int compareTo(Entry o) {
			return time - o.time;
		}

		@Override
		public String toString() {
			return "Entry{" + "pre=" + pre + ", time=" + time + '}';
		}
	}

	public static int dist(int index, String line) {
		int mul = 1;
		int result = 0;
		for (int i = index; i < line.length(); i++) {
			if (line.charAt(i) == 'T') {
				mul *= -1;
			} else {
				result += mul;
			}
		}
		return result;
	}

	static class Name implements Comparable<Name> {
		String name;
		int h;

		public Name(String name, int h) {
			super();
			this.name = name;
			this.h = h;
		}

		public int compareTo(Name o) {

			return h - o.h;
		}

	}

	static int find(int index, int[] u) {
		if (u[index] != index) {
			return u[index] = find(u[index], u);
		}
		return index;
	}

	static int crossProduct(Point a, Point b) {
		return a.x * b.y + a.y * b.x;
	}

	static long squareDist(Point a) {
		long x = a.x;
		long y = a.y;
		return x * x + y * y;
	}

	static Point minus(Point a, Point b) {
		return new Point(a.x - b.x, a.y - b.y);
	}

	public static boolean nextPer(int[] data) {
		int i = data.length - 1;
		while (i > 0 && data[i] < data[i - 1]) {
			i--;
		}
		if (i == 0) {
			return false;
		}
		int j = data.length - 1;
		while (data[j] < data[i - 1]) {
			j--;
		}
		int temp = data[i - 1];
		data[i - 1] = data[j];
		data[j] = temp;
		Arrays.sort(data, i, data.length);
		return true;
	}

	static class Point {
		int x, y;

		public Point(int x, int y) {
			super();
			this.x = x;
			this.y = y;
		}

		public String toString() {
			return "{" + x + " " + y + "}";
		}

	}

	static int gcd(int a, int b) {
		if (b == 0) {
			return a;
		}
		return gcd(b, a % b);
	}

	static class Scanner {

		BufferedReader br;
		StringTokenizer st;

		public Scanner() throws FileNotFoundException {
			// System.setOut(new PrintStream(new
			// BufferedOutputStream(System.out), true));
			br = new BufferedReader(new InputStreamReader(System.in));
			// br = new BufferedReader(new InputStreamReader(new
			// FileInputStream(new File("B-large.in"))));
		}

		public String next() {

			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					throw new RuntimeException();
				}
			}
			return st.nextToken();
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

		public String nextLine() {
			st = null;
			try {
				return br.readLine();
			} catch (Exception e) {
				throw new RuntimeException();
			}
		}

		public boolean endLine() {
			try {
				String next = br.readLine();
				while (next != null && next.trim().isEmpty()) {
					next = br.readLine();
				}
				if (next == null) {
					return true;
				}
				st = new StringTokenizer(next);
				return st.hasMoreTokens();
			} catch (Exception e) {
				throw new RuntimeException();
			}
		}
	}
}