Problem:
Problem B: Repeated Substitution with Sed

Do you know "sed," a tool provided with Unix? Its most popular use is to substitute every
occurrence of a string contained in the input string (actually each input line) with another
string β. More precisely, it proceeds as follows.

Within the input string, every non-overlapping (but possibly adjacent) occurrences of α are
marked. If there is more than one possibility for non-overlapping matching, the leftmost
one is chosen.

Each of the marked occurrences is substituted with β to obtain the output string; other
parts of the input string remain intact.

For example, when α is "aa" and β is "bca", an input string "aaxaaa" will produce "bcaxbcaa",
but not "aaxbcaa" nor "bcaxabca". Further application of the same substitution to the string
"bcaxbcaa" will result in "bcaxbcbca", but this is another substitution, which is counted as the
second one.

In this problem, a set of substitution pairs (αi, βi) (i = 1, 2, ... , n), an initial string γ, and a
final string δ are given, and you must investigate how to produce δ from γ with a minimum
number of substitutions. A single substitution (αi, βi) here means simultaneously substituting
all the non-overlapping occurrences of αi, in the sense described above, with βi.

You may use a specific substitution (αi, βi ) multiple times, including zero times.

Input

The input consists of multiple datasets, each in the following format.

n

α1 β1

α2 β2

.

.

.

αn βn

γ

δ

n is a positive integer indicating the number of pairs. αi and βi are separated by a single space.
You may assume that 1 ≤ |αi| < |βi| ≤ 10 for any i (|s| means the length of the string s),

αi ≠ αj for any i ≠ j, n ≤ 10 and 1 ≤ |γ| < |δ| ≤ 10. All the strings consist solely of lowercase
letters. The end of the input is indicated by a line containing a single zero.

Output

For each dataset, output the minimum number of substitutions to obtain δ from γ. If δ cannot
be produced from γ with the given set of substitutions, output -1.

Sample Input

2
a bb
b aa
a
bbbbbbbb
1
a aa
a
aaaaa
3
ab aab
abc aadc
ad dee
abc
deeeeeeeec
10
a abc
b bai
c acf
d bed
e abh
f fag
g abe
h bag
i aaj
j bbb
a
abacfaabe
0

Output for the Sample Input

3
-1
7
4

Solution in C++ (judge verdict: Accepted)

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <cstdio>
#include <cstring>
using namespace std;

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define DEB 1
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back

pair<string,string> pss[12];
map<string,int> msi;

string subplace(string &s, int i){
  int p = 0;
  string ret = s;
  while( (p=ret.find(pss[i].first,p))!=string::npos ){
    ret.replace(p, pss[i].first.length(), pss[i].second);
    p += pss[i].second.size();
  }
  return ret;
}

int main(){
  int n;
  while(cin>>n,n){
    msi.clear();
    rep(i,n){
      cin >> pss[i].first >> pss[i].second;
    }
    string a,b;
    cin>>a>>b;
    
    int ans = -1;
    queue<pair<string,int> > q;
    q.push(mp(a,0));
    msi[a] = 0;
    while( !q.empty() ){
      string s = q.front().first;
      int c = q.front().second;
      q.pop();
      if( s.length() > 1000 )break;
      if( s==b ){
	ans = c;
	break;
      }
      rep(i,n){
	string tmp = subplace(s,i);
	if( msi.find(tmp)==msi.end() ){
	  q.push(mp(tmp,c+1));
	  msi[tmp] = c+1;
	}
      }
    }
    
    printf("%d\n",ans);
  }
  return 0;
}