#include <map>
#include <set>
#include <iostream>
#include <string>
using namespace std;

struct _Key
{
  string State;
  char c;
  bool operator< (const _Key& k) const
  { 
    if(this->State==k.State)
      return this->c<k.c;
    else
      return this->State<k.State;
  }
};

int main(void)
{
  map<_Key,string> dfa;
  set<string> end;
  string begin;
  string s;
  while(true)
  {
    _Key key;
    string newstate;
    cin>>key.State;
    if(key.State=="END")
      break;
    cin>>key.c>>newstate;
    dfa[key]=newstate;
  }
  while(true)
  {
    string state;
    cin>>state;
    if(state=="END")
      break;
    end.insert(state);
  }
  cin>>begin;
  cin>>s;
  _Key key;
  key.State=begin;
  for(int i=0; i<s.size(); i++)
  {
    key.c=s[i];
    if(dfa.find(key)!=dfa.end())
      key.State=dfa[key];
    else
    {
      cout<<0<<'\n'<<i<<'\n'<<key.State;
      return 0;
    }
  }
  if(end.find(key.State)==end.end())
    cout<<0<<'\n'<<s.size()<<'\n'<<key.State;
  else
    cout<<1<<'\n'<<s.size()<<'\n'<<key.State;     
  return 0;
}
