#include <cstdio>
#include <cstring>
#include <cmath>
#include <utility>
#include <iostream>
#include <functional>
#include <bitset>
#include <algorithm>
#include <vector>
#include <forward_list>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <numeric>
#define ll long long int
#define pb push_back
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
int mx4[] = {0,1,0,-1};
int my4[] = {1,0,-1,0};
int INF = 2e6;
ll MOD = 1e9 + 7;


int main(){
    
    int n; cin >> n;
    vector<vector<pair<int,ll>>> g(n);
    rep(i,n-1){
        int a, b; ll c;
        cin >> a >> b >> c;
        a--; b--;
        g[a].pb(make_pair(b,c));
        g[b].pb(make_pair(a,c));
    }
    
    int q, k; cin >> q >> k;
    k--;
    vector<int> x(q),y(q);
    rep(i,q) {
        cin >> x[i] >> y[i];
        x[i]--; y[i]--;
    }

    queue<int> que;
    que.push(k);
    vector<bool> vis(n,false);
    vis[k] = true;
    vector<ll> dist(n);

    while(que.size()){
        int v = que.front();
        que.pop();
        for(auto i : g[v]){
            if(!vis[i.first]){
                vis[i.first] = true;
                dist[i.first] = dist[v] + i.second;
                que.push(i.first);                
            }
        }
    }

    rep(i,q){
        cout << dist[x[i]] + dist[y[i]] << endl;
    }
}
#include <bits/stdc++.h>
using namespace std;

#define INF INT_MAX

using Weight = long long;
using Weights = vector<Weight>;
using Matrix = vector<Weights>;

bool WF(const Matrix &g, Matrix &dist, vector<vector<int>> &prev) {
	int V = g.size();
	dist = g;
	prev.assign(V, vector<int>(V, -1));
	for (int k = 0; k < V; ++k) {
		for (int i = 0; i < V; ++i) {
			for (int j = 0; j < V; ++j) {
				if (dist[i][k] == INF || dist[k][j] == INF) continue;
				if (dist[i][j] > dist[i][k] + dist[k][j]) {
					dist[i][j] = dist[i][k] + dist[k][j];
					prev[i][j] = k;
				}
			}
		}
	}
	for (int i = 0; i < V; ++i) if (dist[i][i] < 0) return true;
	return false;
}

void buildPath(const vector< vector<int> > &prev,
	int s, int t, vector<int> &path) {
	int u = prev[s][t];
	if (u < 0) path.push_back(s);
	else buildPath(prev, s, u, path), buildPath(prev, u, s, path);
}
vector<int> buildPath(
	const vector< vector<int> > &prev, int s, int t) {
	vector<int> path;
	buildPath(prev, s, t, path);
	path.push_back(t);
	return path;
}

int main(void) {
	int V, E;
	cin >> V >> E;
	Matrix g(V, vector<Weight>(V, INF));
	for (int i = 0; i < V; ++i) g[i][i] = 0;
	for (int i = 0; i < E; ++i) {
		long long s, t, w;
		cin >> s >> t >> w;
		g[s][t] = w;
	}

	Matrix dist;
	vector<vector<int>> prev;
	bool nc = WF(g, dist, prev);
	if (nc) {
		cout << "NEGATIVE CYCLE" << endl;
		return 0;
	}
	for (int i = 0; i < V; ++i) {
		for (int j = 0; j < V; ++j) {
			if (i == j) cout << "0";
			else if (dist[i][j] == INF) cout << "INF";
			else cout << dist[i][j];
			if (j != V - 1)cout << " ";
		}
		cout << endl;
	}
	return 0;
}
