Which intersections did I pass?
Time limit2sMemory limit256 MB
Given a connected undirected graph, each query asks how many vertices lie on some walk from a to b that never revisits the endpoints in the middle.
Problem
Seunghyun lives in a town with intersections and two-way roads. Each road joins two different intersections, and you can travel between any two intersections along one or more roads. At most one road directly joins a given pair of intersections. For the convenience of the residents, the intersections are numbered to .
On Sunday, Seunghyun left his own house and went to visit his friend Minsu, who lives in the same town. He had moved in only recently, so he knew nothing about the road network and could recognize only his own house and Minsu's house. He wandered for a long time before he reached Minsu's house, and he passed through some intersections several times on the way.
Seunghyun remembers two things. After he set out he never stopped by the intersection where his own house is again, and the moment he reached the intersection where Minsu's house is he walked straight in. So the route he walked starts at the intersection of his own house and ends at the intersection of Minsu's house, and in between it passes through neither of those two intersections. Every other intersection may be passed any number of times.
Seunghyun wants to know how many intersections lie on at least one route that agrees with this memory. The starting intersection of his own house and the final intersection of Minsu's house both count as passed. He then went one step further and wondered what that count would be if his house were at intersection and Minsu's house at intersection .
Write a program that answers his questions.
Input
The first line contains the number of test cases ().
The first line of each test case contains the number of intersections () and the number of roads (), separated by a space. Each of the next lines contains two integers and (, ) separated by a space, meaning that a road directly joins intersection and intersection . The next line contains the number of questions (). The -th of the next lines contains two integers and (, ) separated by a space, asking about the case where Seunghyun's house is at intersection and Minsu's house is at intersection .
Over all test cases the sum of is at most , and the sum of and the sum of are each at most .
Output
For each test case, print lines. The -th of them contains the answer to the -th question, that is, the number of intersections Seunghyun could have passed when his house is at intersection and Minsu's house is at intersection . Do not print a blank line between different test cases.