def sed(p,g,d)
    return nil if g.length > d.length
    return 0 if g == d
    min = Float::INFINITY
    p.each do |pt|
        next if !g.include?(pt[0])
        r = sed(p,g.gsub(pt[0],pt[1]),d)
        next if !r
        min = r + 1 if r + 1 < min
    end
    return nil if min == Float::INFINITY
    return min
end
loop do
    n = gets.to_i
    break if n == 0
    p = n.times.map{gets.split}
    g = gets.chomp
    d = gets.chomp
    ret = sed(p,g,d)
    puts ret ? ret : -1
end