Life is a game.
The world can be regarded as an undirected connected graph of

cities and

undirected roads between the cities. Now you, the life game player, are going to play the life game on the world graph.
Initially, you are at the

-th city and of

social ability points. You can earn social ability points by living and working. Specifically, you can earn

social ability points by living and working in the

-th city. But in this problem, you cannot earn social ability points duplicatedly in one city, so you want to travel the world and earn more social ability points. However, the roads are not easy. Specifically, there is an ability threshold

for the

-th road, you should be of at least

social ability points to go through the road. Moreover, Your social ability point will not decrease when passing roads but just need to be at least

if you want to go through the

-th road.
So as you can see, the life game is just living, working and traveling repeatedly. There are

game saves. For each game save, the initial city and social ability point is given and the player has not lived or worked in any city. Now you, the real life game player, need to determine the maximum possible number of social ability points you can have in the end of the game and output it for each given game save.