In the middle of a nightmare, Maxine suddenly finds herself in a mysterious room with the following items: 

A piece of paper with the word score and the integer $\mbox{0}$ written on it.
A map of the castle where the room is located.
There are $N$ rooms uniquely labeled from $\mbox{1}$ to $N$.  
There are $N$ bidirectional corridors connecting pairs of rooms. The value of score changes every time she travels up or down a corridor, and this value differs depending on her direction of travel along the corridor. Each corridor can be traveled any number of times in either direction.
Every room is reachable from every other room.  
Maxine is located in the room labeled $\mbox{S}$.  
The exit is located in the room labeled $\boldsymbol{E}$. Once this room is reached, score is reduced modulo $\mbox{M}$ and Maxine can (but is not required to) exit that level! 

Assume some corridor $\boldsymbol{i}$ (where $1\leq i\leq N$) is associated with an integer, $x_i$, and connects rooms $a_i$ and $b_i$. Then:

Traveling corridor $\boldsymbol{i}$ from room $a_i$ to room $b_i$ increases score by $x_i$.
Traveling corridor $\boldsymbol{i}$ from room $b_i$ to room $a_i$ decreases score by $x_i$. 

There are $\mbox{Q}$ levels to Maxine's nightmare castle, and each one has a different set of values for $\mbox{S}$, $\boldsymbol{E}$, and $\mbox{M}$. Given the above information, help Maxine by finding and printing her maximum possible score for each level. Only you can help her wake up from this nightmare!

Note: Recall that the result of a modulo operation is always non-negative.  For example, $(-8)\:\text{mod}\:5=2$.  

Input Format

The first line contains a single integer, $N$, denoting the number of rooms. 

Each of the $N$ subsequent lines describes a corridor in the form of three space-separated integers denoting the respective values for $a_i$, $b_i$, and $x_i$. 

The next line contains a single integer, $\mbox{Q}$, denoting the number of queries. 

Each of the $\mbox{Q}$ subsequent lines describes a level in the form of three space-separated integers denoting its respective $\mbox{S}$, $\boldsymbol{E}$, and $\mbox{M}$ values. 

Constraints

$1\leq N\leq10^5$  
$1\leq a_i,b_i\leq N$, $a_i\neq b_i$  
$1\leq x_i\leq10^9$  
$1\leq Q\leq10^5$  

For each level:

The room layout is the same
$1\leq S,E\leq N$  
$1\leq M\leq10^9$  

Subtask

$1\leq N,Q,M\leq300$ for $30\%$ of max score.

Output Format

For each of the $\mbox{Q}$ levels, print the maximum possible score for that level on a new line.

Sample Input
3
1 3 5
2 3 8
2 1 31
1
1 2 13

Sample Output
12

Explanation

The Sample Input represents the following setup:  

We want to travel from room $\mbox{1}$ to room $2$ while maximizing the value of score. There are at least two ways to achieve the maximum score value of $12$:  

Travel through corridors $5$ times: $1\rightarrow3\rightarrow2\rightarrow1\rightarrow3\rightarrow2$  

$score=(5-8+31+5-8)\ \text{mod}\ 13=25\ \text{mod}\ 13=12$.  

Travel through corridors $\textbf{34}$ times: $1\rightarrow2\rightarrow3\rightarrow1\rightarrow2\rightarrow3\rightarrow1\rightarrow2\rightarrow\text{...}\rightarrow3\rightarrow1\rightarrow2\rightarrow3\rightarrow1\rightarrow2$ 

$score=-339\ \text{mod}\ 13=12$, because $12$ is the smallest non-negative integer $\boldsymbol{x}$ such that $13$ divides $(-339-x)$.

Difficulty: MEDIUM_HARD
Tags: Dynamic programming
Skill types: Dynamic programming

Starter code:

Solutions:
from fractions import gcd
n = int(input())
adjlst = [[] for i in range(n)]
for j in range(0, n):
	arr = list(map(int, input().split()))
	a = arr[0] - 1
	b = arr[1] - 1
	c = arr[2]
	adjlst[a].append((b, c))
	adjlst[b].append((a, -c))
dist = [None] * n
parents = [set() for i in range(n)]
dist[0] = 0
stack = [0]
while stack:
	i = stack.pop()
	for (j, c) in adjlst[i]:
		if j in parents[i]:
			continue
		dist1 = dist[i] + c
		parents[j].add(i)
		if dist[j] is None:
			dist[j] = dist1
			stack.append(j)
		else:
			cycle = abs(dist[j] - dist1)
q = int(input())
for j in range(0, q):
	arr1 = list(map(int, input().split()))
	s = arr1[0]
	e = arr1[1]
	m = arr1[2]
	a = gcd(cycle, m)
	print(m - a + (dist[e - 1] - dist[s - 1]) % a)


from fractions import gcd
n = int(input())
adjlst = [[] for i in range(n)]
for j in range(0, n):
	arr = list(map(int, input().split()))
	a = arr[0] - 1
	b = arr[1] - 1
	c = arr[2]
	adjlst[a].append((b, c))
	adjlst[b].append((a, -c))
dist = [None] * n
parents = [set() for i in range(n)]
dist[0] = 0
stack = [0]
while stack:
	i = stack.pop()
	for (j, c) in adjlst[i]:
		if j in parents[i]:
			continue
		dist1 = dist[i] + c
		parents[j].add(i)
		if dist[j] is None:
			dist[j] = dist1
			stack.append(j)
		else:
			cycle = abs(dist[j] - dist1)
q = int(input())
for j in range(0, q):
	arr1 = list(map(int, input().split()))
	s = arr1[0]
	e = arr1[1]
	m = arr1[2]
	a = gcd(cycle, m)
	print(m - a + (dist[e - 1] - dist[s - 1]) % a)


from fractions import gcd
n = int(input())
adj = [[] for i in range(n)]
for i in range(n):
	(a, b, c) = map(int, input().strip().split())
	a -= 1
	b -= 1
	adj[a].append((b, c))
	adj[b].append((a, -c))
dist = [None] * n
parents = [set() for i in range(n)]
dist[0] = 0
stack = [0]
while stack:
	i = stack.pop()
	for (j, c) in adj[i]:
		if j in parents[i]:
			continue
		ndist = dist[i] + c
		parents[j].add(i)
		if dist[j] is None:
			dist[j] = ndist
			stack.append(j)
		else:
			cycle = abs(dist[j] - ndist)
for qq in range(int(input())):
	(s, e, m) = map(int, input().strip().split())
	a = gcd(cycle, m)
	print(m - a + (dist[e - 1] - dist[s - 1]) % a)


from math import gcd
n = int(input())
adjlst = [[] for i in range(n)]
for j in range(0, n):
	arr = list(map(int, input().split()))
	a = arr[0] - 1
	b = arr[1] - 1
	c = arr[2]
	adjlst[a].append((b, c))
	adjlst[b].append((a, -c))
dist = [None] * n
parents = [set() for i in range(n)]
dist[0] = 0
stack = [0]
while stack:
	i = stack.pop()
	for (j, c) in adjlst[i]:
		if j in parents[i]:
			continue
		dist1 = dist[i] + c
		parents[j].add(i)
		if dist[j] is None:
			dist[j] = dist1
			stack.append(j)
		else:
			cycle = abs(dist[j] - dist1)
q = int(input())
for j in range(0, q):
	arr1 = list(map(int, input().split()))
	s = arr1[0]
	e = arr1[1]
	m = arr1[2]
	a = gcd(cycle, m)
	print(m - a + (dist[e - 1] - dist[s - 1]) % a)


n = eval(input())
adj = [[] for i in range(n)]
for i in range(n):
	(a, b, c) = list(map(int, input().strip().split()))
	a -= 1
	b -= 1
	adj[a].append((b, c))
	adj[b].append((a, -c))
dist = [None] * n
parents = [set() for i in range(n)]
dist[0] = 0
stack = [0]
while stack:
	i = stack.pop()
	for (j, c) in adj[i]:
		if j in parents[i]:
			continue
		ndist = dist[i] + c
		parents[j].add(i)
		if dist[j] is None:
			dist[j] = ndist
			stack.append(j)
		else:
			cycle = abs(dist[j] - ndist)
from fractions import gcd
for qq in range(eval(input())):
	(s, e, m) = list(map(int, input().strip().split()))
	a = gcd(cycle, m)
	print(m - a + (dist[e - 1] - dist[s - 1]) % a)


import math
import os
import random
import re
import sys
from math import gcd
n = eval(input())
adj = [[] for i in range(n)]
for i in range(n):
	(a, b, c) = list(map(int, input().strip().split()))
	a -= 1
	b -= 1
	adj[a].append((b, c))
	adj[b].append((a, -c))
dist = [None] * n
parents = [set() for i in range(n)]
dist[0] = 0
stack = [0]
while stack:
	i = stack.pop()
	for (j, c) in adj[i]:
		if j in parents[i]:
			continue
		ndist = dist[i] + c
		parents[j].add(i)
		if dist[j] is None:
			dist[j] = ndist
			stack.append(j)
		else:
			cycle = abs(dist[j] - ndist)
for qq in range(eval(input())):
	(s, e, m) = list(map(int, input().strip().split()))
	a = gcd(cycle, m)
	print(m - a + (dist[e - 1] - dist[s - 1]) % a)