#!/usr/bin/perl -w

use v5.16;
use strict;

my @rules;
while (<>) {
	chomp;
	last if /^$/;
	my ($src, $dst) = /(\w+) => (\w+)/;
	push @rules, [$src, $dst];
}

chomp (my $mol = <>);

my %seen;

for my $r (@rules) {
	my ($src, $dst) = @$r;
	my $m1 = $mol;
	say "$src => $dst";
	while ($m1 =~ /$src/g) {
		my $m2 = $m1;
		substr($m2, pos($m1)-length($src), length($src)) = $dst;
		$seen{$m2}++;
		say $m2;
	}
}

say scalar keys %seen;
