stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <cstdio>

#include <cstring>

#include <cmath>

#include <bitset>

#include <algorithm>

#include <iostream>

typedef long long ll;

using namespace std;

const int maxn=300100;

int ai[maxn];

int n;



bool check(int l,int r){

    int minx=n+10,maxx=0;

    for(int i=l;i<=r;i++){

        minx=min(minx,ai[i]);

        maxx=max(maxx,ai[i]);

    }

    if(minx<l||maxx>r) return true;

    int x1=0,x2=0;

    for(int i=l;i<=r;i++){

        if(ai[i]!=i){

            if(ai[i]>i){

                if(x1<ai[i]) x1=ai[i];

                else return true;

            }else{

                if(x2<ai[i]) x2=ai[i];

                else return true;

            }

        }

    }

    return false;

}



int main(){

    scanf("%d",&n);

    for(int i=1;i<=n;i++) scanf("%d",&ai[i]);

    for(int i=1;i<=n;i++){

        if(ai[i]!=i){

            int x;

            for(x=i;ai[x+1]==x+1&&ai[x+2]!=x+2;){

                x+=2;

            }

            x=min(x,n);

            //cout<<i<<"   "<<x<<endl;

            if(check(i,x)) return puts("No"),0;

            i=x;

        }

    }

    puts("Yes");

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

int n,a,b,c,l1,l2;

int main()

{

	scanf("%d",&n);

	for(int i=1;i<=n;i++)

	{

		scanf("%d",&a);

		if((a^i)&1){printf("No\n");return 0;}

		if(a>i||(l1&&!((l1^a)&1))){if(!l1)l1=a;else if(a<l2){printf("No\n");return 0;}else if(a<l1)l2=a;else l1=a;}

		if(i&1){if(b<a)b=a;}else if(c<a)c=a;

		if(b>i&&c>i){printf("No\n");return 0;}

		if(b<=i&&c<=i)l1=l2=0;

	}

	printf("Yes\n");

}//

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



typedef long long lint;

typedef long double louble;



template<typename T1,typename T2> inline T1 max(T1 a,T2 b){return a<b?b:a;}

template<typename T1,typename T2> inline T1 min(T1 a,T2 b){return a<b?a:b;}



const char lf = '\n';



namespace ae86

{

	const int bufl = 1 << 15;



	char buf[bufl],*s=buf,*t=buf;



	inline int fetch()

	{

		if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;}

		return *s++;

	}



	inline int ty()

	{

		int a=0;int b=1,c=fetch();

		while(!isdigit(c))b^=c=='-',c=fetch();

		while(isdigit(c))a=a*10+c-48,c=fetch();

		return b?a:-a;

	}

}

using ae86::ty;



const int _ = 300007;



void failure(){cout<<"No"<<lf;exit(0);}



struct tla

{

#define lb(a) ((a)&(-(a)))

	lint t[_];

	void add(int x,lint d){while(x)t[x]+=d,x-=lb(x);}

	lint sum(int x){lint d=0;while(x<_)d+=t[x],x+=lb(x);return d;}

	void cls(){memset(t,0,sizeof(t));}

#undef lb

}t;



int n,val[_],ps[_],lps;



int main()

{

	ios::sync_with_stdio(0),cout.tie(nullptr);



	n=ty();

	for(int i=1;i<=n;i++)val[i]=ty();



	for(int i=1;i<=n;i++)if((val[i]&1)!=(i&1))failure();



	lint ce=0,co=0,cc=0;

	t.cls();

	for(int i=1;i<=n;i+=2)co+=t.sum(val[i]),t.add(val[i],1);

	t.cls();

	for(int i=2;i<=n;i+=2)ce+=t.sum(val[i]),t.add(val[i],1);

	t.cls();

	for(int i=1;i<=n;i++)cc+=t.sum(val[i]),t.add(val[i],1);

	

	if(cc!=3*(ce+co))failure();



	cout<<"Yes"<<lf;



	return 0;

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

const int N=3e5+10;



int gi() {

	int x=0,o=1;char ch=getchar();

	while(!isdigit(ch)&&ch!='-') ch=getchar();

	if(ch=='-') o=-1,ch=getchar();

	while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();

	return x*o;

}



void No() {

	cout<<"No";exit(0);

}



int n,a[N];



int main() {

	cin>>n;

	for(int i=1;i<=n;i++) a[i]=gi();

	for(int l=1;l<=n;l++)

		if(a[l]^l) {

			int r=l;

			for(;r<=n;r+=2) {

				if(r+1>n||a[r+1]!=r+1) break;

				if(r+2>n||a[r+2]==r+2) break;

			}

			for(int i=l;i<=r;i++)

				if(a[i]<l||a[i]>r) No();

			int x=0,y=0;

			for(int i=l;i<=r;i+=2)

				if(a[i]<i) {

					if(a[i]<x) No();

					x=a[i];

				}

				else {

					if(a[i]<y) No();

					y=a[i];

				}

			l=r;

		}

	cout<<"Yes";

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<vector>

#include<cstring>

using i64=long long;

int n,flg=1,t[300007];std::vector<int>p[3];

int read(){int x;scanf("%d",&x);return x;}

void add(int p){while(p)++t[p],p^=p&-p;}

int ask(int p){int r=0;while(p<=n)r+=t[p],p+=p&-p;return r;}

i64 tao(std::vector<int>p){i64 r=0;memset(t,0,sizeof t);for(int x:p)r+=ask(x),add(x);return r;}

int main()

{

    n=read();

    for(int i=1,x;i<=n;++i) p[2].push_back(x=read()),p[i&1].push_back(x),flg&=!((i^x)&1);

    puts(flg&(tao(p[2])==3*(tao(p[0])+tao(p[1])))? "Yes":"No");

}


// Optimized solution to the same problem (faster):
#include <iostream>

#include <cstdio>

#include <cstring>

#include <algorithm>

#define ll long long

using namespace std;

inline int read()

{

	int x = 0, f = 1; char ch = getchar();

	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}

	while(ch >= '0' && ch <= '9') {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();}

	return x * f;

}

const int N = 1e6 + 1000;

int n,a[N];

ll tr[N];

ll A,P,Q;

int lowbit(int x){return x & -x;}

ll ask(int pos){ll res = 0; for(;pos;pos -= lowbit(pos)) res += tr[pos]; return res;}

void add(int pos,int v){for(;pos <= n;pos += lowbit(pos)) tr[pos] += v; return;}



int main()

{

	n = read(); for(int i = 1;i <= n;i ++) if((i & 1) != ((a[i] = read()) & 1)) {puts("No"); return 0;}

	for(int i = n;i >= 1;i --){A += ask(a[i]); add(a[i],1);}

	if(A % 3) {puts("No"); return 0;}

	for(int i = 1;i <= n;i ++) tr[i] = 0;

	for(int i = n;i >= 1;i -= 2){P += ask(a[i]); add(a[i],1);}

	for(int i = 1;i <= n;i ++) tr[i] = 0;

	for(int i = n - 1;i >= 1;i -= 2){Q += ask(a[i]); add(a[i],1);}

	if(P + Q != A / 3) {puts("No"); return 0;}

	else {puts("Yes"); return 0;}

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



#define lb(x) x & -x

const int _ = 3e5 + 7; int N , arr[_];

void add(int x){while(x <= N){++arr[x]; x += lb(x);}}

int qry(int x){int sum = 0; while(x){sum += arr[x]; x -= lb(x);} return sum;}



#define ll long long

ll c(vector < int > p){

	ll sum = 0; memset(arr , 0 , sizeof(arr)); while(!p.empty()){sum += qry(p.back()); add(p.back()); p.pop_back();} return sum;

}



int main(){

	bool flg = 1; vector < int > p1 , p2 , p3; cin >> N;

	for(int x , i = 1 ; i <= N ; ++i){cin >> x; p1.push_back(x); (i & 1 ? p2 : p3).push_back(x); flg &= !((i ^ x) & 1);}

	puts(flg & (c(p1) == 3 * (c(p2) + c(p3))) ? "Yes" : "No"); return 0;

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

#define LL long long

#define uLL unsigned long long



using namespace std;

const int N=3e5+10;

int rd()

{

    int x=0,w=1;char ch=0;

    while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}

    while(ch>='0'&&ch<='9'){x=x*10+(ch^48);ch=getchar();}

    return x*w;

}

int n,a[N],bt[N],b[N],c1[N],c2[N];

void add(int x,int y){while(x<=n) bt[x]+=y,x+=x&(-x);}

int gsm(int x){int an=0;while(x) an+=bt[x],x-=x&(-x);return an;}

void wa(){puts("No");exit(0);}

void ac(){puts("Yes");}



int main()

{

////

    n=rd();

    for(int i=1;i<=n;++i) a[i]=rd();

    for(int i=1;i<=n;++i)

	if((a[i]^i)&1) wa();

    for(int o=0;o<=1;++o)

    {

	memset(bt,0,sizeof(bt));

	for(int i=1+((o^1)&1);i<=n;i+=2)

	{

	    b[i]+=(bool)(gsm(n)-gsm(a[i]));

	    add(a[i],1);

	}

	memset(bt,0,sizeof(bt));

	for(int i=n-((o^n)&1);i>=1;i-=2)

	{

	    b[i]+=(bool)gsm(a[i]);

	    add(a[i],1);

	    if(b[i]==2) wa();

	}

    }

    for(int i=1;i<=n;i+=2)

	if(i<a[i]) ++c1[i],--c1[a[i]+1];

    for(int i=2;i<=n;i+=2)

	if(i<a[i]) ++c2[i],--c2[a[i]+1];

    for(int i=1;i<=n;++i)

    {

	c1[i]+=c1[i-1],c2[i]+=c2[i-1];

	if(c1[i]&&c2[i]) wa();

    }

    ac();

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <stdio.h>

#include <string.h>

#include <algorithm>

#define MN 300000

typedef long long LL;

int n,a[MN+5],b[MN+5],c[MN+5];

bool check1(){

	for(int i=1;i<=n;i++)

		if((a[i]-i)&1) return false;

	return true;

}

LL merge_sort(int l,int r){

	if(l==r) return 0;

	int i,j,k,mid=(l+r)>>1;

	LL ret=merge_sort(l,mid)+merge_sort(mid+1,r);

	for(i=l,j=mid+1,k=l;i<=mid&&j<=r;){

		if(b[i]>b[j]){

			ret+=mid-i+1;

			c[k++]=b[j++];

		}else{

			c[k++]=b[i++];

		}

	}

	while(i<=mid) c[k++]=b[i++];

	while(j<=r) c[k++]=b[j++];

	for(int i=l;i<=r;i++) b[i]=c[i];

	return ret;

}

bool check2(){

	for(int i=1;i<=n;i++) b[i]=a[i];

	if(merge_sort(1,n)%3!=0) return false;

	return true;

}

bool check3(){

	for(int i=1;i<=n-2;i++)

		if(a[i]!=i&&a[i+1]!=i+1&&a[i+2]!=i+2) return false;

	return true;

}

bool check4(){

	memset(c,0x7f,sizeof(c));

	for(int i=n;i>=1;i-=2)

		c[std::lower_bound(c+1,c+1+n,a[i])-c]=a[i];

	if(c[3]!=0x7f7f7f7f) return false;

	memset(c,0x7f,sizeof(c));

	for(int i=n-1;i>=1;i-=2)

		c[std::lower_bound(c+1,c+1+n,a[i])-c]=a[i];

	if(c[3]!=0x7f7f7f7f) return false;

	return true;

}

int main(){

	scanf("%d",&n);

	for(int i=1;i<=n;i++)

		scanf("%d",&a[i]);

	if(!check1()) return 0*puts("No");

	if(!check2()) return 0*puts("No");

	if(!check3()) return 0*puts("No");

	if(!check4()) return 0*puts("No");

	return 0*puts("Yes");

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <string.h>

#include <algorithm>

#include <cmath>

typedef long long ll;

using namespace std;

#define rep(i,st,ed) for (int i=st;i<=ed;++i)



const int INF=0x3f3f3f3f;

const int N=2000005;



int a[N];



int read() {

    int x=0,v=1; char ch=getchar();

    for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):v,ch=getchar());

    for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());

    return x*v;

}



bool check(int l,int r) {

    int mx = 0, mn = INF;

    for (int i = l; i <= r; i++) {

        mx = max(mx, a[i]);

        mn = min(mn, a[i]);

    }

    if (mn < l || mx > r) return true;

    int r1 = 0, r2 = 0;

    for(int i=l;i<=r;i++){

        if(a[i] != i){

            if(a[i]<i){//那么他是从小的换过来的

                if(r1<a[i]) r1=a[i];

                else return true;

            }else{//那么他是从大的换过来的

                if(r2<a[i]) r2=a[i];

                else return true;

            }

        }

    }

    return false;

}



int main(void) {

    int n=read(),x;

    for(int i=1;i<=n;i++) a[i]=read();

    for(int i=1;i<=n;i++){

        if(a[i] != i){

            for(x=i;a[x+1]==x+1&&a[x+2]!=x+2;) x+= 2;

            x=min(x,n);// 得到交换的段，也就是这个是分段的，有些段是跨不过去的

            if(check(i, x)) return puts("No"), 0;

            i=x;

        }

    }

    return puts("Yes"),0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <iostream>

#include <algorithm>

#include <vector>

#include <set>

#include <map>

#define rep(i,l,r) for(int i=(l);i<=(r);++i)

#define per(i,r,l) for(int i=(r);i>=(l);--i)

#define fo(i,l,r,d) for(int i=(l);i<=(r);i+=(d))

using namespace std;

const int maxn=3e5+10;

int a[maxn],n;

int main(){

    scanf("%d",&n);

    rep(i,1,n){

        scanf("%d",&a[i]);

        if((i^a[i])&1) return puts("No"),0;

    }

    for(int i=1;i<=n;++i){

        if(a[i]==i) continue;

        int p;

        for(p=i;p<=n-2;p+=2)

            if(a[p+2]==p+2||a[p+1]!=p+1) break;

        int mx=0,mn=n+1;

        rep(j,i,p) mx=max(mx,a[j]),mn=min(mn,a[j]);

        if(mx!=p||mn!=i) return puts("No"),0;

        int f1=0,f2=0;

        fo(j,i,p,2)

            if(a[j]>f1) f1=a[j];

            else if(a[j]>f2) f2=a[j];

            else return puts("No"),0;

        i=p;

    }

    puts("Yes");

    return 0;

}

//DSYTXDY

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <string.h>

#include <algorithm>

#include <cmath>

typedef long long ll;

using namespace std;

#define rep(i,st,ed) for (int i=st;i<=ed;++i)



const int INF=0x3f3f3f3f;

const int N=2000005;



int a[N];



int read() {

    int x=0,v=1; char ch=getchar();

    for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):v,ch=getchar());

    for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());

    return x*v;

}



bool check(int l,int r) {

    int mx = 0, mn = INF;

    for (int i = l; i <= r; i++) {

        mx = max(mx, a[i]);

        mn = min(mn, a[i]);

    }

    if (mn < l || mx > r) return true;

    int r1 = 0, r2 = 0;

    for(int i=l;i<=r;i++){

        if(a[i] != i){

            if(a[i]<i){//那么他是从小的换过来的

                if(r1<a[i]) r1=a[i];

                else return true;

            }else{//那么他是从大的换过来的

                if(r2<a[i]) r2=a[i];

                else return true;

            }

        }

    }

    return false;

}



int main(void) {

    int n=read(),x;

    for(int i=1;i<=n;i++) a[i]=read();

    for(int i=1;i<=n;i++){

        if(a[i] != i){

            for(x=i;a[x+1]==x+1&&a[x+2]!=x+2;) x+= 2;

            x=min(x,n);// 得到交换的段，也就是这个是分段的，有些段是跨不过去的

            if(check(i, x)) return puts("No"), 0;

            i=x;

        }

    }

    return puts("Yes"),0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <stdio.h>

#include <string.h>

#include <vector>

#include <queue>

using namespace std;



const int maxn = 1e5 + 10;

const long long inf = 0x7fffffffffffffff;



struct Edge {

	int to;

	long long len;

	Edge(int to, long long len) : to(to), len(len) {}

	bool operator < (const Edge &r) const

	{

		return len > r.len;

	}

};



vector<Edge> edge[maxn];

int n;

long long dist[maxn];



void dij(int k)

{

	bool vis[maxn] = {};

	for (int i = 0; i <= n; i++)

		dist[i] = inf;

	priority_queue<Edge> que;

	

	dist[k] = 0;

	que.push(Edge(k, 0));



	while (!que.empty()) {

		Edge tmp = que.top();

		que.pop();

		int u = tmp.to;

		if (vis[u])

			continue;

		vis[u] = true;

		for (int i = 0; i < edge[u].size(); i++) {

			int to = edge[u][i].to;

			long long len = edge[u][i].len;

			if (!vis[to] && dist[to] > dist[u] + len) {

				dist[to] = dist[u] + len;

				que.push(Edge(to, dist[to]));

			}

		}

	}



}



int main(void)

{

	scanf("%d", &n);

	for (int i = 1; i < n; i++) {

		int a, b, c;

		scanf("%d %d %d", &a, &b, &c);

		edge[a].push_back(Edge(b, c));

		edge[b].push_back(Edge(a, c));

	}

	int q, k;

	scanf("%d %d", &q, &k);

	dij(k);

	for (int i = 0; i < q; i++) {

		int a, b;

		scanf("%d %d", &a, &b);

		printf("%lld\n", dist[a] + dist[b]);

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

#define INF 1<<30

#define LINF 1LL<<60



struct edge{

    ll u;

    ll v;

    ll cost;

    edge(){}

    edge(ll u, ll v,ll cost):u(u),v(v),cost(cost){}

};



int main(void) {

    cin.tie(0); ios::sync_with_stdio(false);

    ll N; cin >> N;

    vector<vector<edge>> G(N);

    for(int i = 0; i < N-1;i++){

        ll a,b,c; cin >> a >> b >> c;

        a--; b--;

        G[a].push_back(edge(a,b,c));

        G[b].push_back(edge(b,a,c));

    }

    ll Q,K; cin >> Q >> K;

    K--;

    vector<ll> dist(N,LINF);

    queue<ll> q; q.push(K);

    dist[K] = 0;

    while(!q.empty()){

        ll n = q.front(); q.pop();

        for(auto e:G[n]){

            if(dist[e.v] > dist[e.u] + e.cost){

                dist[e.v] = dist[e.u] + e.cost;

                q.push(e.v);

            }

        }

    }

    for(int i = 0; i < Q;i++){

        ll x,y; cin >> x >> y;

        x--; y--;

        cout << dist[x] + dist[y] << endl;

    }

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

 

using namespace std;

 

#define lli unsigned long long int

#define MOD 1000000007

#define DEBUG 0

#define REP(i,n) for(int i=0;i<n;i++)



#define TATE 0

#define YOKO 1



/* 文字列→形式に変換*/





lli hash2(lli a,lli b){

	return a*10000000 + b;

}

int main(){



	lli n;



	cin>>n;

	map <lli,lli> d;

	map<lli,vector<lli>> v;

	REP(i,n-1){

		lli a,b,c;

		cin>>a>>b>>c;

		d[hash2(a,b)] = c;

		d[hash2(b,a)] = c;

		v[a].push_back(b);

		v[b].push_back(a);

	}



	lli Q,k;

	cin>>Q>>k;



	/* 準備 kをから各点までの距離をさんしゅつしておけばよい*/

	/* これは単なる幅優先探索でOK*/



	lli flag[100010];

	REP(i,100010)flag[i]=0;



	queue<lli> q;

	queue<lli> length;

	q.push(k);

	length.push(0);

	flag[k]=0;



	while(q.size()){

		lli now = q.front();

		lli nowLength = length.front();

		q.pop();

		length.pop();

		d[hash2(k,now)] = nowLength;

		d[hash2(now,k)] = nowLength;



		for(auto e:v[now]){

			if(flag[e])continue;



			lli nextLength = nowLength + d[hash2(now,e)];

			flag[e]=1;

			q.push(e);

			length.push(nextLength);

		}

	}



	REP(i,Q){

		lli x,y;

		cin >> x>>y;

		cout<<d[hash2(x,k)]+d[hash2(k,y)]<<endl;

	}





	return 0;

}

 

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>



using namespace std;



#define lli long long int

#define REP(i,s,n) for(int i=s;i<n;i++)

#define MOD 1000000007

#define NUM 2520

#define INF (1LL<<50)

#define DEBUG 0

#define mp(a,b) make_pair(a,b)

#define SORT(V) sort(V.begin(),V.end())

#define PI (3.141592653589794)



struct edge{

	lli to,cost;

};

#define P pair<lli,lli>



lli n;

lli dist[100100];

vector<vector<edge>> g;



void func(lli now,lli from){

	for(auto &e:g[now]){

		if(e.to==from)continue;

		dist[e.to]=dist[now]+e.cost;

		func(e.to,now);

	}

}



signed main(){



	cin>>n;

	g.resize(n);



	REP(i,0,n-1){

		lli a,b,c;

		cin>>a>>b>>c;

		a--,b--;

		g[a].push_back({b,c});

		g[b].push_back({a,c});

	}



	lli q,k;



	cin>>q>>k;

	k--;

	dist[k]=0;

	func(k,-1);



	/*

	REP(i,0,n){

		cout<<"i="<<i<<" dist="<<dist[i]<<endl;

	}*/



	REP(i,0,q){

		lli x,y;

		cin>>x>>y;

		x--,y--;

		cout<<dist[x]+dist[y]<<endl;

	}







	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <algorithm>

#include <vector>

#include <cstdio>



using namespace std;



typedef unsigned long long ll;



int N;

vector<int> T[100003];

vector<int> C[100003];



ll depth[100003];



void dfs(int n, int pre, ll c) {

	//printf("%2d", n);

	depth[n] = c;

	for (int i = 0; i < T[n].size(); ++i) {

		if (pre == T[n][i])

			continue;

		dfs(T[n][i], n, c + C[n][i]);

	}

}



int main() {

	cin >> N;

	for (int i = 0; i < N-1; ++i)

	{

		int a,b,c;

		cin >> a >> b >> c;

		T[a].push_back(b);

		T[b].push_back(a);

		C[a].push_back(c);

		C[b].push_back(c);

	}

	int Q, K;

	cin >> Q >> K;

	dfs(K, 0, 0);

	for (int j = 0; j < Q; ++j) {

		int s, g;

		cin >> s >> g;

		cout << depth[s] + depth[g] << endl;

	}



}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>



using namespace std;



#define REP(i,n)	for(int i=0, i##_len=(n); i<i##_len; ++i)

#define REPS(i,s,n)	for(int i=(s), i##_len=(n); i<i##_len; ++i)



#define ALL(obj)	(obj).begin(),(obj).end()



typedef unsigned int 			uint;

typedef unsigned long long int	ull;

typedef long long int 			ll;



typedef pair<int,int>			P;



#define INF 		2000000007

#define MINF		-2000000007

#define MAX 		100005



const int MOD = 1e9+7;

// テンプレ終了

//====================================================================//



int K;

ll dp[MAX];

vector<vector<pair<int,int>>> tree(MAX);



void dfs(int s,int p,ll suml){

	for(auto itr : tree[s]){

		if(itr.first == p)continue;

		dp[itr.first] = suml + itr.second;

		dfs(itr.first,s,suml + itr.second);

	}

}



// agc070

int main(){

	int N;cin>>N;

	REP(i,N-1){

		int a,b,c;

		cin >> a >> b >> c;

		tree[a-1].push_back(make_pair(b-1,c));

		tree[b-1].push_back(make_pair(a-1,c));

	}

	int Q;

	cin>>Q>>K;



	dfs(K-1,K-1,0);



	REP(i,Q){

		int x,y;cin>>x>>y;

		cout << (dp[x-1]+dp[y-1]) << endl;

	}



	//REP(i,N)cout << dp[i] << " ";

	//cout << endl;





	return 0;

}






// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define Would

#define you

#define please





int main() {

	cin.tie(0);

	ios::sync_with_stdio(false);



	int N;

	cin >> N;



	vector<pair<int, ll>> eda[100001];

	rep(i, N - 1) {

		int a, b, c;

		cin >> a >> b >> c;

		eda[a].push_back(make_pair(b, c));

		eda[b].push_back(make_pair(a, c));

	}



	ll kyori[100001] = {};



	int Q, K;

	cin >> Q >> K;



	vector<pair<int, ll>> que;

	que.push_back(make_pair(K, 0));



	while (que.size()) {

		vector<pair<int, ll>> que2;

		for (auto itr : que) {

			if (kyori[itr.first] == 0) {

				kyori[itr.first] = itr.second;

				for (auto itr2 : eda[itr.first]) {

					que2.push_back(make_pair(itr2.first, itr.second + itr2.second));

				}

			}

		}

		que = que2;

	}



	rep(i, Q) {

		int X, Y;

		cin >> X >> Y;

		co(kyori[X] + kyori[Y]);

	}



	Would you please return 0;

}

// Optimized solution to the same problem (faster):
//#include<bits/stdc++.h>

#include<cstdio>

using namespace std;

typedef long long ll;

#define rep(i, n) for(int i = 0; i < (n); i++)

#define rep1(i, n) for(int i = 1; i <= (n); i++)

#define co(x) cout << (x) << "\n"

#define cosp(x) cout << (x) << " "

#define ce(x) cerr << (x) << "\n"

#define cesp(x) cerr << (x) << " "

#define pb push_back

#define mp make_pair

#define Would

#define you

#define please



const int cm = 1 << 17;

char cn[cm], * ci = cn + cm, ct;

inline char getcha() {

	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }

	return *ci++;

}

inline int getint() {

	int A = 0;

	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';

	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';

	return A;

}



const int dm = 1 << 21;

char dn[dm], * di = dn, dt;

inline void putint(ll X) {

	int keta = 0;

	char C[20];

	while (X) {

		*(C + keta) = '0' + X % 10;

		X /= 10;

		keta++;

	}

	for (int i = keta - 1; i >= 0; i--)* di++ = (*(C + i));

	*di++ = '\n';

}



int N;

ll D[100001];

int E[200001], Ed[200001], E2[200001], H[100001];

void dfs(int A, ll B) {

	*(D + A) = B;

	for (int i = H[A]; i; i = E2[i]) if (!(*(D + E[i]))) dfs(E[i], B + Ed[i]);

}



int main() {

	//cin.tie(0);

	//ios::sync_with_stdio(false);





	int N = getint();

	int k = 1;

	rep(i, N - 1) {

		int a = getint();

		int b = getint();

		int c = getint();

		E[k] = b;

		Ed[k] = c;

		E2[k] = H[a];

		H[a] = k++;

		E[k] = a;

		Ed[k] = c;

		E2[k] = H[b];

		H[b] = k++;

	}

	int Q = getint();

	int K = getint();

	dfs(K, 1);

	rep(i, Q) {

		int x = getint();

		int y = getint();

		putint(*(D + x) + *(D + y) - 2);

	}



	fwrite_unlocked(dn, 1, di - dn, stdout);



	Would you please return 0;

}

// Fastest known solution for this problem: