#include<bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;

const double PI = acos(-1);
const int inf = 2e9;
const long long INF = 2e18;
const long long MOD = 1e9+7;

#define sx(s) (s).size()
#define pb push_back
#define fi first
#define se second
#define REP(i,n) for (int i = 0; i < n; i++)
#define ALL(a) begin(a),end(a)

int main(void) {
    int n;
    while (cin >> n, n) {
        vector<pair<int,ll>> s(4000, {inf, 0});
        vector<bool> ip(4000,false);
        REP(i,n) {
            ll e,p,q;
            cin >> e >> p >> q;
            e--;
            if (ip[e]) {
                s[e].se += p*q;
            }
            else {
                s[e] = make_pair(i,p*q);
                ip[e] = true;
            }
        }
        vector<pair<int,int>> ans;
        REP(i, 4000) {
            if (s[i].se >= 1000000) {
                ans.pb({s[i].fi, i+1});
            }
        }
        sort(ALL(ans));
        REP(i,ans.size()) cout << ans[i].se << endl;
        if (ans.size() == 0) cout << "NA" << endl;
    }
    return 0;
}

#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define REP(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) std::cout << x << '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define sz(x) ((int)(x).size())
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; }
const int dx[4] = { 1, 0, -1,  0 };
const int dy[4] = { 0, 1,  0, -1 };
const int INF = INT_MAX / 2;
const ll  inf = LLONG_MAX / 2;
const int mod = 1000000007;
const int MAX_N = 101010;
const long double PI = acos(-1);

using Edge = pair<int, ll>;
using Graph = vector<vector<Edge>>;

Graph G;
vector<ll> dist;

void dfs(int cur, int pre, ll sum) {
	dist[cur] = sum;
	
	for (auto to : G[cur]) {
		if (to.first != pre) {
			dfs(to.first, cur, sum + to.second);
		}
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(10);
	/*------------------------------*/

	int n;
	cin >> n;
	
	G.resize(n);
	
	rep(i, n - 1) {
		int a, b, c;
		cin >> a >> b >> c;
		a--; b--;
		G[a].push_back(make_pair(b, c));
		G[b].push_back(make_pair(a, c));
	}

	int q, k;
	cin >> q >> k;
	k--;

	dist.resize(n, 0);

	dfs(k, -1, 0);

	rep(i, q) {
		int x, y;
		cin >> x >> y;
		x--; y--;
		out(dist[x] + dist[y]);
	}

	return 0;
}