#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int N_MAX=100001;
long long INF = 123456789123456789;
typedef pair<long long,int> P;
void dijkstra(int s, int V, vector<P>* g, long long* d){
  if(d[s]==0)
    return;
  int i,v;
  P p,e;
  priority_queue<P, vector<P>, greater<P>> que; // 最短距離，頂点番号
  fill(d,d+V,INF);
  d[s]=0;
  que.push(P(0,s));
  while(!que.empty()){
    p=que.top(); que.pop();
    v=p.second;
    if(d[v]<p.first) continue;
    for(i=0;i<g[v].size();i++){
      e=g[v][i];
      if(d[e.second]>d[v]+e.first){
        d[e.second]=d[v]+e.first;
        que.push(P(d[e.second],e.second));
      }
    }
  }
  return;
}
int main()
{
  int n,q,k,x,y,a,b,i;
  long long c;
  long long d[N_MAX];
  vector<P> g[N_MAX];
  cin >> n;
  fill(d+1,d+n+1,INF);
  for(i=0;i<n-1;i++){
    cin >> a >> b >> c;
    g[a].emplace_back(c,b);
    g[b].emplace_back(c,a);
  }
  cin >> q >> k;
  dijkstra(k,n,g,d);
  for(i=0;i<q;i++){
    cin >> x >> y;
    cout << d[x]+d[y] << endl;
  }
  return 0;
}
#include "bits/stdc++.h"

using namespace std;

int dp(int curr, int T, const vector<int>& A, const vector<int>& B, vector<vector<int>>& memo) {
    int N = A.size();
    if (curr == N) {
        return 0;
    }
    if (T <= 0) {
        return 0;
    }
    if (memo[curr][T] >= 0) {
        return memo[curr][T];
    }

    int skip = dp(curr + 1, T, A, B, memo);
    int take = dp(curr + 1, T - A[curr], A, B, memo) + B[curr];
    int ret = max(skip, take);
    memo[curr][T] = ret;
    return ret;
}

void Main() {
    int N, T;
    cin >> N >> T;
    set<pair<int, pair<int, int>>> prep;
    for (int i = 0; i < N; ++i) {
        int a, b;
        cin >> a >> b;
        prep.insert(make_pair(a, make_pair(i, b)));
    }
    vector<int> A(N, 0), B(N, 0);
    int cnt = 0;
    for (auto e : prep) {
        A[cnt] = e.first;
        B[cnt] = e.second.second;
        ++cnt;
    }

    vector<vector<int>> memo(N + 10, vector<int>(T + 10, -1));
    int ans = dp(0, T, A, B, memo);
    cout << ans << endl;
}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}
