2011 Зимняя Школа, Харьков, День 8, День Виталия Гольдштейна

Colored Wizards

A fairy-tale country represents itself a number of cities connected by roads with a two-way traffic. From any city in the country you can get to any other city either directly or through other cities. It is known that in a fairytale country there are no roads connecting the city with itself and between any two different cities there is no more than one road.

Yellow and blue wizards live in the fairy-tale country. The yellow wizard, passing along the road, repaints it in yellow, blue wizard repaints the road in blue. As you know, after applying yellow color on blue, or blue paint on yellow, the paints mix and turn into green color, which is the most unloved color of both wizards.

This year a conference of wizards is held in the capital of the country (city F) . Therefore, yellow and blue wizards want to know what minimal number of roads they will have to recolour in green to get to the capital. Initially, all roads are not painted.

The initial position of the yellow and blue wizards is not known in advance. Therefore, it is necessary to solve this problem for k possible cases of their initial locations.


First line contains integers n (1n105) and m (1m500000) - the number of cities and roads in fairy-tale country. Third line contains an integers F (1Fn) - number of the city that is a capital of the country. Next m lines contain the description of roads in the country. Each of m lines contains two integers ai and bi meaning that there exist a road between ai and bi. Next line contains integer k (1k105) - the number of possible initial locations for wizards. Each of the next k lines contains two integers - the city numbers where initially located yellow and blue wizards.


For each of the k tests print the minimum number of roads that will have to be painted green by wizards in order to get to the capital.

Time limit 1 second
Memory limit 122.17 MiB
Input example #1
6 6
1 2
2 3
3 4
4 2
4 5
3 6
5 6
6 6
Output example #1
Author Vitaly Goldstein
Source 2011 Kharkiv Winter School, Vitaliy Goldstein, Problem L