hash = Hash.new(0)
print_clients = []
this_month_clients = []
last_month_clients = []

while line = gets
  if line.chomp.empty?
    print_clients.push(*(this_month_clients & last_month_clients))
    last_month_clients = this_month_clients.dup
    this_month_clients.clear
  else
    c, _ = line.chomp.split(',')
    this_month_clients << c
    hash[c] += 1
  end
end

print_clients.push(*(this_month_clients & last_month_clients))

result = []
hash.each do |key, value|
  if print_clients.include?(key)
    result << [key, value]
  end
end

result.sort_by { |_, count| count }.each do |client, count|
  puts "#{client} #{count}"
end