$N, $K = gets.chomp.split(' ').map(&:to_i)

$m = []
$bunny = {}

$K.times do |i|
  tmp = gets.chomp.split(' ').map(&:to_i)
  $m << tmp[0]
  tmp[1..-1].each do |b|
    $bunny[b] = [i, false]
  end
end

$R = gets.chomp.to_i

count = 0
$R.times do
  a, b = gets.chomp.split(' ').map(&:to_i)
  ag, ab = $bunny[a]
  bg, bb = $bunny[b]

  next unless ag == bg
  if ab == false
    count += 1
    $bunny[a] = [ag, true]
  end
  if bb == false
    count += 1
    $bunny[b] = [bg, true]
  end
end

puts count

