F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

The Social Network

Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 3380    Accepted Submission(s): 1006


Problem Description
The social network system (SNS) helps people to keep connecting with their friends. Every user in SNS has a friends list. The user can read news posted by the users in his friends list. Friend relation is symmetric - if A is a friend of B, B is always a friend of A.

Another important function in SNS is friend recommendation. One effective way to recommend friends is recommend by mutual friends. A mutual friend between two users A and B, is a user who is a friend of both A and B. A user can not be a friend of himself. For a specific user A, the system will recommend the user who is not himself or his friend, and has mutual friends with A. If more than one such user exists, recommend the one has most mutual friends with A. If still a tie exists, output all of them.
 

Input
The first line is a integer T (T¡Ü100), the number of test case.
The beginning of each test case is two integers N and Q, the number of friend relationship and the number of query. 1 ¡Ü N, Q ¡Ü 1000
The following N lines each contain two different names separated by a single space. Each name consisted by only lowercase letters, and its length is less than or equal to 15. This means the two users are friends. No friend relationship will be given more than once.
The following Q lines each describe a query. Each line contain one user name. The data guarantee that this name appears at least once in above N lines.
 

Output
For each case, you should output one line containing ¡°Case k: ¡± first, where k indicates the case number and counts from one. Then for each query, output one line, contains one or more names of recommended friends, separate by a single space, sorted by alphabetical order. If no persons can be recommended, output one line contains ¡°-¡±.
 

Sample Input
1 10 11 hongshu digua yingying hongshu xmm hongshu huaxianzi xmm tangjiejie huaxianzi xhmz yingying digua xhmz zt tangjiejie xmm lcy notonlysuccess ljq hongshu digua yingying xmm huaxianzi tangjiejie xhmz zt lcy notonlysuccess ljq
 

Sample Output
Case 1: xhmz yingying digua digua tangjiejie yingying hongshu lcy zt xmm hongshu huaxianzi hongshu huaxianzi - -
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-22 13:07:52, Gzip enabled