Flipping Networks

Time limit1sMemory limit128 MB

Summary
Maintain an undirected graph under edge toggle operations and count, after every flip, how many hosts are reachable from host 1 but only by paths longer than 10 hops.
Level

Medium7 of 10

Topics
Graph, BFS, Dynamic programming, Implementation
Solved
No attempts yet

Problem

The Dean of the Unseen University has decided to modernise his communication by installing a computer network of bidirectionally connected hosts, numbered consecutively from 11 to hh. Because the environment is intensely magical, the structure of the network changes at random, and often.

It is therefore valuable to know which hosts can be reached from the main host and which cannot. These structural changes can be monitored without disturbing the network, so the current state of the network is known at any moment.

By convention, any host that can be reached from the main host (host 11) in 1010 hops or fewer is called online. Some hosts may be reachable from host 11 and still not be online, because every path connecting them to host 11 is longer than 1010 hops. The Dean wants to know how many such hosts exist.

Input

The first line contains a single integer: the number of test cases. Each test case has the following format:

  • One line with an integer hh, the number of hosts (1≤h≤30001 \le h \le 3000).
  • One line with an integer cc, the number of initial connections (1≤c≤15001 \le c \le 1500).
  • cc lines, each with two integers pp and qq: a connection that initially exists between hosts pp and qq.
  • One line with an integer ll, the number of connection changes (1≤l≤15001 \le l \le 1500).
  • ll lines, each with two integers rr and ss: the connection between hosts rr and ss is flipped. If it is currently present it disappears; if it is currently absent it appears.

Because of the magical environment, the only guarantee about pp, qq, rr and ss is that each lies in the range 1…h1 \dots h.

Output

For each test case, print a single line with one integer: the number of hosts that are reachable from host 11 but are not online — that is, hosts whose shortest path from host 11 is longer than 1010 hops.

Examples1

  1. Example 1

    Input
    2
    4
    4
    1 2
    2 3
    3 4
    4 1
    4
    1 3
    1 3
    2 3
    3 4
    12
    5
    10 11
    3 6
    9 8
    1 4
    4 7
    8
    11 3
    5 6
    7 10
    6 5
    5 9
    8 2
    5 6
    2 12
    
    Expected output
    0
    1