#include <utility>
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <map>
#include <cstdlib>
#include <time.h>
using namespace std;

typedef deque<string> Prefix;
map<Prefix, vector<string> > statetab;  // prefix -> suffixes


const int MAXGEN = 100;
const int NPREF = 2;
const string NONWORD = "\n";

void build(Prefix& aPrefix, istream& anInStream);
void add(Prefix& aPrefix, const string& aString);
void generate(int anInt);

// markov main: markov-chain random text generation
int main(void)
{
    int nwords = MAXGEN;
    Prefix prefix;

    for (int i = 0; i < NPREF; i++)
        add(prefix, NONWORD);
    build(prefix, cin);
    add(prefix,NONWORD);
    generate(nwords);
    return 0;
}

// build: read input words, build state table
void build(Prefix& prefix, istream& in)
{
    string buf;

    while (in >> buf)
        add(prefix, buf);
}

// add: add word to suffix list, update prefix
void add(Prefix& prefix, const string& s)
{
    if (prefix.size() == NPREF) {
        statetab[prefix].push_back(s);
        prefix.pop_front();
    }
    prefix.push_back(s);
}

// generate: produce output, one word per line
void generate(int nwords)
{
    Prefix prefix;
    int i;

    srand( time(NULL) );

    for (i = 0; i < NPREF; i++)
        add(prefix, NONWORD);

    for (i = 0; i < nwords; i++) {
        vector<string>& suf = statetab[prefix];
        const string& w = suf[rand() % suf.size()];
        if (w == NONWORD)
            break;
        cout << w << "\n";
        prefix.pop_front();
        prefix.push_back(w);
    }
}

