spoj#TRYCOMP. Try to complete

Try to complete


Problem Statement :
You are given millions of words from a book.
For each query you are given a string S. Find the most occuring word in the book with S as prefix.
Input :
The first line consists of an integer n, the number of words in the text book. The next n lines consists of the words in the book. The next line consists of an integer q, the number of queries. Next q lines consists a string S.
Output:
For each query String S, print the most occuring word in the book with S as prefix. If there are many such words, print the lexicographically smallest word. If there is no such word, print -1.
Input Constraints :
1 <= n <= 2*10^6
1 <= q <= 10^6
1 <= word length <= 10
All the characters in the word are small letters of English alphabet.
Example : 
Sample Input :
5
pen
pine
apple
apple
pen
3
p
apple
pi
Sample Output :
pen
apple
pine

Problem Statement :
 

You are given hundreds of thousands of words from a book.

For each query you are given a string S. Find the most occuring word in the book with S as prefix.


Input :
 

The first line consists of an integer n, the number of words in the text book. The next n lines consists of the words in the book. The next line consists of an integer q, the number of queries. Next q lines consists a string S.


Output:
 

For each query String S, print the most occuring word in the book with S as prefix along with the number of occurances of that word. If there are many such words, print the lexicographically smallest word. If there is no such word, print -1.


Input Constraints :

1 <= n <= 5*10^5

1 <= q <= 10^5

1 <= word length <= 10

All the characters in the word are small letters of English alphabet.


Time Limit :

3 seconds


Sample Input :

10

apple

banana

orange

applet

banana

oriental

orange

oriental

applet

bangalore

8

ban

bang

app

or

oriental

apple

hobbits

oranges


Sample Output :

banana 2

bangalore 1

applet 2

orange 2

oriental 2

applet 2

-1

-1

 

Problem source: Inspired from autocomplete feature on google keyboard.