#include <algorithm>
#include <iostream>
#include <cstdio>
#include <vector>
#define pb push_back
using namespace std;
typedef long long ll;
int month_day[][12] =
{
	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
	{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
#ifdef ONLINE_JUDGE
#define read_ll(x) do {scanf("%I64d", &x); } while (false)
#define print_ll(x)  do {printf("%I64d\n", x); } while (false)
#else
#define read_ll(x) do {scanf("%lld", &x); } while (false)
#define print_ll(x) do {printf("%lld\n", x); } while (false)
#endif
vector<ll> ok1, ok2;
int main()
{
	freopen("698E.in", "r", stdin);
	int s, m, h, day, date, month;
	scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
	if (day != -1)
		day--;
	if (date != -1)
		date--;
	if (month != -1)
		month--;
	ll x1 = 0;
	for (int h2 = 0; h2 < 24; h2++)
	{
		for (int m2 = 0; m2 < 60; m2++)
		{
			for (int s2 = 0; s2 < 60; s2++, x1++)
			{
				if ((s == -1 || s == s2) && (m == -1 || m == m2) && (h == -1 || h == h2))
					ok1.pb(x1);
			}
		}
	}
	// cout << "ok" << endl;
	int day2 = 3;
	ll x2 = 0;
	for (int year = 1970; year <= 2500; year++)
	{
		bool leap = !(year % 400) || ((year % 100) && !(year % 4));
		for (int month2 = 0; month2 < 12; month2++)
		{
			for (int date2 = 0; date2 < month_day[leap][month2]; date2++, x2++)
			{
				if (-1 == month || month == month2)
				{
					if ((date != -1 && date == date2) || (day != -1 && day == day2) || (date == -1 && day == -1))
						ok2.pb(x2);
				}
				day2 = (day2 + 1) % 7;
			}
		}
	}
	// cout << "ok" << endl;
	int n;
	scanf("%d", &n);
	while (n--)
	{
		ll x;
		read_ll(x);
		ll whole_year = x / x1 / x2;
		ll remain_time = x % (x1 * x2);
		ll whole_day = remain_time / x1;
		remain_time %= x1;
		// print_ll(remain_time);
		// printf("%d\n", binary_search(ok2.begin(), ok2.end(), whole_day));
		// print_ll(ok1.back());
		if (whole_day > ok2.back() || (whole_day == ok2.back() && remain_time >= ok1.back()))
		{
			// cout << "case 1" << endl;
			print_ll((whole_year + 1) * x1 * x2 + ok2[0] * x1 + ok1[0]);
		}
		else if (remain_time >= ok1.back() || !binary_search(ok2.begin(), ok2.end(), whole_day))
		{
			// cout << "case 2" << endl;
			print_ll(whole_year * x1 * x2 + *upper_bound(ok2.begin(), ok2.end(), whole_day) * x1 + ok1[0]);
		}
		else
		{
			// cout << "case 3" << endl;
			print_ll(whole_year * x1 * x2 + whole_day * x1 + *upper_bound(ok1.begin(), ok1.end(), remain_time));
		}
	}
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdio>
#include <vector>
#define pb push_back
using namespace std;
typedef long long ll;
int month_day[][12] =
{
	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
	{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
#ifdef ONLINE_JUDGE
#define read_ll(x) do {scanf("%I64d", &x); } while (false)
#define print_ll(x)  do {printf("%I64d\n", x); } while (false)
#else
#define read_ll(x) do {scanf("%lld", &x); } while (false)
#define print_ll(x) do {printf("%lld\n", x); } while (false)
#endif
vector<ll> ok1, ok2;
int main()
{
	// freopen("698E.in", "r", stdin);
	int s, m, h, day, date, month;
	scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
	if (day != -1)
		day--;
	if (date != -1)
		date--;
	if (month != -1)
		month--;
	ll x1 = 0;
	for (int h2 = 0; h2 < 24; h2++)
	{
		for (int m2 = 0; m2 < 60; m2++)
		{
			for (int s2 = 0; s2 < 60; s2++, x1++)
			{
				if ((s == -1 || s == s2) && (m == -1 || m == m2) && (h == -1 || h == h2))
					ok1.pb(x1);
			}
		}
	}
	// cout << "ok" << endl;
	int day2 = 3;
	ll x2 = 0;
	for (int year = 1970; year < 2370; year++)
	{
		bool leap = !(year % 400) || ((year % 100) && !(year % 4));
		for (int month2 = 0; month2 < 12; month2++)
		{
			for (int date2 = 0; date2 < month_day[leap][month2]; date2++, x2++)
			{
				if (-1 == month || month == month2)
				{
					if ((date != -1 && date == date2) || (day != -1 && day == day2) || (date == -1 && day == -1))
						ok2.pb(x2);
				}
				day2 = (day2 + 1) % 7;
			}
		}
	}
	// cout << "ok" << endl;
	int n;
	scanf("%d", &n);
	while (n--)
	{
		ll x;
		read_ll(x);
		ll whole_year = x / x1 / x2;
		ll remain_time = x % (x1 * x2);
		ll whole_day = remain_time / x1;
		remain_time %= x1;
		// print_ll(remain_time);
		// printf("%d\n", binary_search(ok2.begin(), ok2.end(), whole_day));
		// print_ll(ok1.back());
		if (whole_day > ok2.back() || (whole_day == ok2.back() && remain_time >= ok1.back()))
		{
			// cout << "case 1" << endl;
			print_ll((whole_year + 1) * x1 * x2 + ok2[0] * x1 + ok1[0]);
		}
		else if (remain_time >= ok1.back() || !binary_search(ok2.begin(), ok2.end(), whole_day))
		{
			// cout << "case 2" << endl;
			print_ll(whole_year * x1 * x2 + *upper_bound(ok2.begin(), ok2.end(), whole_day) * x1 + ok1[0]);
		}
		else
		{
			// cout << "case 3" << endl;
			print_ll(whole_year * x1 * x2 + whole_day * x1 + *upper_bound(ok1.begin(), ok1.end(), remain_time));
		}
	}
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdio>
#include <vector>
#define pb push_back
using namespace std;
typedef long long ll;
int month_day[][12] =
{
	{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
	{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
#ifdef ONLINE_JUDGE
#define read_ll(x) do {scanf("%I64d", &x); } while (false)
#define print_ll(x)  do {printf("%I64d\n", x); } while (false)
#else
#define read_ll(x) do {scanf("%lld", &x); } while (false)
#define print_ll(x) do {printf("%lld\n", x); } while (false)
#endif
vector<ll> ok1, ok2;
int main()
{
	// freopen("698E.in", "r", stdin);
	int s, m, h, day, date, month;
	scanf("%d%d%d%d%d%d", &s, &m, &h, &day, &date, &month);
	if (day != -1)
		day--;
	if (date != -1)
		date--;
	if (month != -1)
		month--;
	ll x1 = 0;
	for (int h2 = 0; h2 < 24; h2++)
	{
		for (int m2 = 0; m2 < 60; m2++)
		{
			for (int s2 = 0; s2 < 60; s2++, x1++)
			{
				if ((s == -1 || s == s2) && (m == -1 || m == m2) && (h == -1 || h == h2))
					ok1.pb(x1);
			}
		}
	}
	// cout << "ok" << endl;
	int day2 = 3;
	ll x2 = 0;
	for (int year = 1970; year <= 2500; year++)
	{
		bool leap = !(year % 400) || ((year % 100) && !(year % 4));
		for (int month2 = 0; month2 < 12; month2++)
		{
			for (int date2 = 0; date2 < month_day[leap][month2]; date2++, x2++)
			{
				if (-1 == month || month == month2)
				{
					if ((date != -1 && date == date2) || (day != -1 && day == day2) || (date == -1 && day == -1))
						ok2.pb(x2);
				}
				day2 = (day2 + 1) % 7;
			}
		}
	}
	// cout << "ok" << endl;
	int n;
	scanf("%d", &n);
	while (n--)
	{
		ll x;
		read_ll(x);
		ll whole_year = x / x1 / x2;
		ll remain_time = x % (x1 * x2);
		ll whole_day = remain_time / x1;
		remain_time %= x1;
		// print_ll(remain_time);
		// printf("%d\n", binary_search(ok2.begin(), ok2.end(), whole_day));
		// print_ll(ok1.back());
		if (whole_day > ok2.back() || (whole_day == ok2.back() && remain_time >= ok1.back()))
		{
			// cout << "case 1" << endl;
			print_ll((whole_year + 1) * x1 * x2 + ok2[0] * x1 + ok1[0]);
		}
		else if (remain_time >= ok1.back() || !binary_search(ok2.begin(), ok2.end(), whole_day))
		{
			// cout << "case 2" << endl;
			print_ll(whole_year * x1 * x2 + *upper_bound(ok2.begin(), ok2.end(), whole_day) * x1 + ok1[0]);
		}
		else
		{
			// cout << "case 3" << endl;
			print_ll(whole_year * x1 * x2 + whole_day * x1 + *upper_bound(ok1.begin(), ok1.end(), remain_time));
		}
	}
	return 0;
}


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

typedef long long LL;

int sec[86500], cs, day[150000], ds, cur;
int s, m, h, d, dt, mt, T;
LL perioid, dd, ss, x, ans;

bool leap(int y){
	if(y % 4) return false;
	if(y % 100) return true;
	if(y % 400) return false;
	return true;
}

int leap_month(int y, int m){
	return (leap(y) && m == 1);
}

bool valid(int _mt, int _dt, int _d){
	if(mt != -1 && mt != _mt) return false;
	if( (dt == -1 && d == -1) || dt == _dt || d == _d) return true;
	return false;
}

const int dmon[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

void prepare(){
	for(int i = 0; i < 24; i ++) if(h == -1 || h == i)
		for(int j = 0; j < 60; j ++) if(m == -1 || m == j)
			for(int k = 0; k < 60; k ++) if(s == -1 || s == k)
				sec[cs ++] = i * 3600 + j * 60 + k;
	for(int i = 0; i < 400; i ++){
		for(int j = 0; j < 12; j ++){
			int tot = dmon[j] + leap_month(i + 1970, j);
			for(int k = 0; k < tot; k ++){
				if(valid(j, k, (cur + 3) % 7) ) day[ds ++] = cur;
				cur ++;
			}
		}
	}
	perioid = cur * 86400ll;
}

int main(){
	scanf("%d %d %d %d %d %d", &s, &m, &h, &d, &dt, &mt);
   if(d > 0) d --;
   if(dt > 0) dt --;
   if(mt > 0) mt --;
	prepare();
	for(scanf("%d", &T); T --; ){
		scanf("%I64d", &x);
		ans = x - x % perioid;
		x = x - ans;
		dd = x / 86400;
		ss = x % 86400;
		int it = lower_bound(day, day + ds, dd) - day;
		if(it == ds) ans += perioid + day[0] * 86400ll + sec[0];
		else if(day[it] > dd) ans += day[it] * 86400ll + sec[0];
		else{
			int id = upper_bound(sec, sec + cs, ss) - sec;
			if(id < cs) ans += day[it] * 86400ll + sec[id];
			else{
				it ++;
				if(it == ds) ans += perioid + day[0] * 86400ll + sec[0];
				else ans += day[it] * 86400ll + sec[0];
			}
		}
		printf("%I64d\n", ans);
	}
}
//hellobig_tq//hello


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

typedef long long LL;

int sec[86500], cs, day[150000], ds, cur;
int s, m, h, d, dt, mt, T;
LL perioid, dd, ss, x, ans;

bool leap(int y){
	if(y % 4) return false;
	if(y % 100) return true;
	if(y % 400) return false;
	return true;
}

int leap_month(int y, int m){
	return (leap(y) && m == 1);
}

bool valid(int _mt, int _dt, int _d){
	if(mt != -1 && mt != _mt) return false;
	if( (dt == -1 && d == -1) || dt == _dt || d == _d) return true;
	return false;
}

const int dmon[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

void prepare(){
	for(int i = 0; i < 24; i ++) if(h == -1 || h == i)
		for(int j = 0; j < 60; j ++) if(m == -1 || m == j)
			for(int k = 0; k < 60; k ++) if(s == -1 || s == k)
				sec[cs ++] = i * 3600 + j * 60 + k;
	for(int i = 0; i < 400; i ++){
		for(int j = 0; j < 12; j ++){
			int tot = dmon[j] + leap_month(i + 1970, j);
			for(int k = 0; k < tot; k ++){
				if(valid(j, k, (cur + 3) % 7) ) day[ds ++] = cur;
				cur ++;
			}
		}
	}
	perioid = cur * 86400ll;
}

int main(){
//	freopen("e.in", "r", stdin);
	scanf("%d %d %d %d %d %d", &s, &m, &h, &d, &dt, &mt);
   if(d > 0) d --;
   if(dt > 0) dt --;
   if(mt > 0) mt --;
//   cout << s << " " << m <<  " " << h << " " << d << " " << dt << " " << mt << endl;
	prepare();
	for(scanf("%d", &T); T --; ){
		scanf("%I64d", &x);
		ans = x - x % perioid;
		x = x - ans;
		dd = x / 86400;
		ss = x % 86400;
///		cout << dd << " " << ss << endl;
		int it = lower_bound(day, day + ds, dd) - day;
///		cout << day[it] << endl;
		if(it == ds) ans += perioid + day[0] * 86400ll + sec[0];
		else if(day[it] > dd) ans += day[it] * 86400ll + sec[0];
		else{
			int id = upper_bound(sec, sec + cs, ss) - sec;
			if(id < cs) ans += day[it] * 86400ll + sec[id];
			else{
				it ++;
				if(it == ds) ans += perioid + day[0] * 86400ll + sec[0];
				else ans += day[it] * 86400ll + sec[0];
			}
		}
		printf("%I64d\n", ans);
	}
}


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

typedef long long LL;

int sec[86500], cs, day[150000], ds, cur;
int s, m, h, d, dt, mt, T;
LL perioid, dd, ss, x, ans;

bool leap(int y){
	if(y % 4) return false;
	if(y % 100) return true;
	if(y % 400) return false;
	return true;
}

bool leap_month(int y, int m){
	return leap(y) && m == 1;
}

bool valid(int _mt, int _dt, int _d){
	if(mt != -1 && mt != _mt) return false;
	//if(dt != -1 && dt != _dt && mt != -1 && mt != _mt) return false;
	//return true;
	if(dt == -1 && d == -1) return true;
	if(dt == -1 && d == _d) return true;
	if(d == -1 && dt == _dt) return true;
	if(dt == _dt && d == _d) return true;
	return false;
}

const int dmon[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

void prepare(){
	for(int i = 0; i < 24; i ++) if(h == -1 || h == i)
		for(int j = 0; j < 60; j ++) if(m == -1 || m == j)
			for(int k = 0; k < 60; k ++) if(s == -1 || s == k)
				sec[cs ++] = i * 3600 + j * 60 + k;

	for(int i = 0; i < 400; i ++){
		for(int j = 0; j < 12; j ++){
			int tot = dmon[j] + leap_month(i + 1970, j);
			for(int k = 0; k < tot; k ++){
				if(valid(j, k, (cur + 3) % 7) ) day[ds ++] = cur;
				cur ++;
			}
		}
	}
	perioid = cur * 86400ll;
//	cout << cur << endl;
//	cout << perioid << endl;
}

int main(){
//	freopen("e.in", "r", stdin);
	scanf("%d %d %d %d %d %d", &s, &m, &h, &d, &dt, &mt);
   if(d > 0) d --;
   if(dt > 0) dt --;
   if(mt > 0) mt --;
///   cout << s << " " << m <<  " " << h << " " << d << " " << dt << " " << mt << endl;
	prepare();
	for(scanf("%d", &T); T --; ){
		scanf("%I64d", &x);
		ans = x - x % perioid;
		x = x - ans;
		dd = x / 86400;
		ss = x % 86400;
///		cout << dd << " " << ss << endl;
		int it = lower_bound(day, day + ds, dd) - day;
///		cout << day[it] << endl;
		if(it == ds) ans += perioid + day[0] * 86400ll + sec[0];
		else if(day[it] > dd) ans += day[it] * 86400ll + sec[0];
		else{
			int id = upper_bound(sec, sec + cs, ss) - sec;
			if(id < cs) ans += day[it] * 86400ll + sec[id];
			else{
				it ++;
				if(it == ds) ans += perioid + day[0] * 86400ll + sec[0];
				else ans += day[it] * 86400ll + sec[0];
			}
		}
		printf("%I64d\n", ans);
	}
}


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

typedef long long LL;

int dm[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int sec[86500], cs, cur;
int days[150000], ds;
int s, m, h, d, dt, mt, T;
LL ans, x, ss, dd, it, id;

inline bool leap(int y){
	if(y % 4) return false;
	if(y % 100) return true;
	if(y % 400) return false;
	return true;
}

inline bool leap_m(int y, int _mt){
	return (leap(y) && _mt == 1);
}

bool OK(int _mt, int _dt, int _d){
	if(mt != -1 && mt != _mt) return false;
	if((dt != -1 && dt != _dt) && (d != -1 && d != _d)) return false;
	return true;
}

int main(){
//	freopen("e.in", "r", stdin);
	scanf("%d %d %d %d %d %d", &s, &m, &h, &d, &dt, &mt);
	if(d > 0) d --;
	if(mt > 0) mt --;
	if(dt > 0) dt --;

	for(int i = -12; i < 12; i ++) if(h == -1 || h == (i+24)%24){
		for(int j = 0; j < 60; j ++) if(m == -1 || m == j){
			for(int k = 0; k < 60; k ++) if(s == -1 || s == k){
				sec[cs ++] = i * 3600 + j * 60 + k;
			}
		}
	}
	if(sec[0] == -43200) sec[cs ++] = 43200;

	for(int i = 0; i < 400; i ++){
		for(int j = 0; j < 12; j ++){
			int tot = leap_m(i + 1970, j) + dm[j];
			for(int k = 0; k < tot; k ++){
				if(OK(j, k, (cur + 2) % 7)){
					days[ds ++] = cur;
				}
				cur ++;
			}
		}
	}

	LL len = (LL)cur * 86400;

	for(scanf("%d", &T); T --; ){
		ans = 0;
		scanf("%I64d", &x);
		int tmp = (x + 43200) / len;
		ans = tmp * len;
		x = x - ans;
		dd = (x + 43200) / 86400;
		ss = x - 86400ll * dd;
		it = lower_bound(days, days + ds, dd) - days;
		if(it == ds){
			ans += len + days[0] * 86400ll + sec[0];
		}
		else if(days[it] > dd){
			ans += days[it] * 86400ll + sec[0];
		}
		else{
			id = upper_bound(sec, sec + cs, ss) - sec;
			if(id == cs){
				it ++;
				if(it == ds) ans += len + days[0] * 86400ll + sec[0];
				else ans += days[it] * 86400ll + sec[0];
			}
			else ans += days[it] * 86400ll + sec[id];
		}
		printf("%I64d\n", ans);
	}
}


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

typedef long long LL;

int sec[86500], cs, day[150000], ds, cur;
int s, m, h, d, dt, mt, T;
LL perioid, dd, ss, x, ans;

bool leap(int y){
	if(y % 4) return false;
	if(y % 100) return true;
	if(y % 400) return false;
	return true;
}

int leap_month(int y, int m){
	return (leap(y) && m == 1);
}

bool valid(int _mt, int _dt, int _d){
	if(mt != -1 && mt != _mt) return false;
	if( (dt == -1 && d == -1) || dt == _dt || d == _d) return true;
	return false;
}

const int dmon[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

void prepare(){
	for(int i = 0; i < 24; i ++) if(h == -1 || h == i)
		for(int j = 0; j < 60; j ++) if(m == -1 || m == j)
			for(int k = 0; k < 60; k ++) if(s == -1 || s == k)
				sec[cs ++] = i * 3600 + j * 60 + k;
	for(int i = 0; i < 400; i ++){
		for(int j = 0; j < 12; j ++){
			int tot = dmon[j] + leap_month(i + 1970, j);
			for(int k = 0; k < tot; k ++){
				if(valid(j, k, (cur + 3) % 7) ) day[ds ++] = cur;
				cur ++;
			}
		}
	}
	perioid = cur * 86400ll;
}

int main(){
	scanf("%d %d %d %d %d %d", &s, &m, &h, &d, &dt, &mt);
   if(d > 0) d --;
   if(dt > 0) dt --;
   if(mt > 0) mt --;
	prepare();
	for(scanf("%d", &T); T --; ){
		scanf("%I64d", &x);
		ans = x - x % perioid;
		x = x - ans;
		dd = x / 86400;
		ss = x % 86400;
		int it = lower_bound(day, day + ds, dd) - day;
		if(it == ds) ans += perioid + day[0] * 86400ll + sec[0];
		else if(day[it] > dd) ans += day[it] * 86400ll + sec[0];
		else{
			int id = upper_bound(sec, sec + cs, ss) - sec;
			if(id < cs) ans += day[it] * 86400ll + sec[id];
			else{
				it ++;
				if(it == ds) ans += perioid + day[0] * 86400ll + sec[0];
				else ans += day[it] * 86400ll + sec[0];
			}
		}
		printf("%I64d\n", ans);
	}
}


#pragma comment(linker, "/STACK:3200000")
#include <cstdio>
#include <iostream>
#include <ctime>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <bitset>
#include <deque>
#include <stack>
#include <climits>
#include <string>
#include <memory.h>

#define ll long long
#define ld double
#define pii pair <int, int>
#define pll pair <ll, ll>
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define mp make_pair

using namespace std;

int s, m, h, day, date, month;

bool vis(int year_) {
	return year_ % 400 == 0 || (year_ % 4 == 0 && year_ % 100 != 0);
}

int get_cnt(int m, int y) {
	if (m == 2) {
		if (vis(y)) {
			return 29;
		}

		return 28;
	}

	if (m == 1 || m == 3 || m == 5 || m == 7) {
		return 31;
	}

	if (m == 8 || m == 10 || m == 12) {
		return 31;
	}

	return 30;
}

bool check_corr(int day_, int date_) {
	if (date != -1 && day != -1) {
		if (date != date_ && day_ != day) {
			return false;
		}
	} else {
		if (date != -1 && date != date_) {
			return false;
		}

		if (day != -1 && day_ != day) {
			return false;
		}
	}

	return true;
}

bool conf(int month_, int day_, int date_, int h_, int m_, int s_) {
	if (month != -1 && month_ != month) {
		return false;
	}

	if (h != -1 && h != h_) {
		return false;
	}

	if (m != -1 && m != m_) {
		return false;
	}

	if (s != -1 && s != s_) {
		return false;
	}

	if (date != -1 && day != -1) {
		if (date != date_ && day_ != day) {
			return false;
		}
	} else {
		if (date != -1 && date != date_) {
			return false;
		}

		if (day != -1 && day_ != day) {
			return false;
		}
	}

	return true;
}

ll go(ll t) {
	int year_ = 1970;
	int month_ = 1;
	int date_ = 1;
	int day_ = 3;
	int s_ = 0;
	int m_ = 0;
	int h_ = 0;

	ll tt = t;

	while (true) {
		ll cnt = 365;
		int c = 365;

		if (vis(year_)) {
			cnt++;
			c++;
		}

		cnt *= 24;
		cnt *= 60;
		cnt *= 60;

		if (t >= cnt) {
			t -= cnt;
			year_++;
			day_ += c;
			day_ %= 7;
		} else {
			break;
		}
	}

	while (true) {
		ll cnt = get_cnt(month_, year_);
		int c = cnt % 7;

		cnt *= 24;
		cnt *= 60;
		cnt *= 60;

		if (t >= cnt) {
			t -= cnt;
			month_++;
			day_ += c;
			day_ %= 7;
		} else {
			break;
		}
	}

	while (true) {
		ll cnt = 24 * 60 * 60;

		if (t >= cnt) {
			t -= cnt;
			date_++;
			day_++;
			day_ %= 7;
		} else {
			break;
		}
	}

	while (true) {
		ll cnt = 60 * 60;

		if (t >= cnt) {
			t -= cnt;
			h_++;
		} else {
			break;
		}
	}

	while (true) {
		ll cnt = 60;

		if (t >= cnt) {
			t -= cnt;
			m_++;
		} else {
			break;
		}
	}

	while (true) {
		ll cnt = 1;

		if (t >= cnt) {
			t -= cnt;
			s_++;
		} else {
			break;
		}
	}

	ll count = 0;

	while (!(date_ == 1 && s_ == 0 && m_ == 0 && h_ == 0)) {
		count++;
		s_++;

		if (s_ == 60) {
			s_ = 0;
			m_++;
		}

		if (m_ == 60) {
			h_++;
			m_ = 0;
		}

		if (h_ == 24) {
			date_++;
			day_++;

			if (day_ == 7) {
				day_ = 0;
			}

			h_ = 0;
		}

		if (date_ == get_cnt(month_, year_) + 1) {
			date_ = 1;
			month_++;
		}

		if (month_ == 13) {
			month_ = 1;
			year_++;
		}

		if (conf(month_, day_, date_, h_, m_, s_)) {
			return tt + count;
		}
	}

	while (month != -1 && month != month_) {
		count += (ll)get_cnt(month_, year_) * 60 * 60 * 24;
		day_ += get_cnt(month_, year_);
		day_ %= 7;
		month_++;

		if (month_ == 13) {
			month_ = 1;
			year_++;
		}
	}

	while (!check_corr(day_, date)) {
		date_++;
		day_++;
		if (day_ == 7) {
			day_ = 0;
		}

		count += 24 * 60 * 60;
	}

	while (true) {
		count++;
		s_++;

		if (s_ == 60) {
			s_ = 0;
			m_++;
		}

		if (m_ == 60) {
			h_++;
			m_ = 0;
		}

		if (h_ == 24) {
			date_++;
			day_++;

			if (day_ == 7) {
				day_ = 0;
			}

			h_ = 0;
		}

		if (date_ == get_cnt(month_, year_) + 1) {
			date_ = 1;
			month_++;
		}

		if (month_ == 13) {
			month_ = 1;
			year_++;
		}

		if (conf(month_, day_, date_, h_, m_, s_)) {
			return tt + count;
		}
	}
}

int main() {
	cin >> s >> m >> h >> day >> date >> month;
	if (day != -1) {
		day--;
	}

	int n;

	cin >> n;

	for (int i = 0; i < n; i++) {
		ll t;

		cin >> t;
		if (i >= 300) {
			break;
		}
		//t = (ll)1e12;

		cout << go(t) << endl;
	}

	return 0;
}


#pragma comment(linker, "/STACK:3200000")
#include <cstdio>
#include <iostream>
#include <ctime>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <bitset>
#include <deque>
#include <stack>
#include <climits>
#include <string>
#include <memory.h>

#define ll long long
#define ld double
#define pii pair <int, int>
#define pll pair <ll, ll>
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define mp make_pair

using namespace std;

int s, m, h, day, date, month;

bool vis(int year_) {
	return year_ % 400 == 0 || (year_ % 4 == 0 && year_ % 100 != 0);
}

int get_cnt(int m, int y) {
	if (m == 2) {
		if (vis(y)) {
			return 29;
		}

		return 28;
	}

	if (m == 1 || m == 3 || m == 5 || m == 7) {
		return 31;
	}

	if (m == 8 || m == 10 || m == 12) {
		return 31;
	}

	return 30;
}

bool check_corr(int day_, int date_) {
	if (date != -1 && day != -1) {
		if (date != date_ && day_ != day) {
			return false;
		}
	} else {
		if (date != -1 && date != date_) {
			return false;
		}

		if (day != -1 && day_ != day) {
			return false;
		}
	}

	return true;
}

bool conf(int month_, int day_, int date_, int h_, int m_, int s_) {
	if (month != -1 && month_ != month) {
		return false;
	}

	if (h != -1 && h != h_) {
		return false;
	}

	if (m != -1 && m != m_) {
		return false;
	}

	if (s != -1 && s != s_) {
		return false;
	}

	if (date != -1 && day != -1) {
		if (date != date_ && day_ != day) {
			return false;
		}
	} else {
		if (date != -1 && date != date_) {
			return false;
		}

		if (day != -1 && day_ != day) {
			return false;
		}
	}

	return true;
}

ll go(ll t) {
	int year_ = 1970;
	int month_ = 1;
	int date_ = 1;
	int day_ = 3;
	int s_ = 0;
	int m_ = 0;
	int h_ = 0;

	ll tt = t;

	while (true) {
		ll cnt = 365;
		int c = 365;

		if (vis(year_)) {
			cnt++;
			c++;
		}

		cnt *= 24;
		cnt *= 60;
		cnt *= 60;

		if (t >= cnt) {
			t -= cnt;
			year_++;
			day_ += c;
			day_ %= 7;
		} else {
			break;
		}
	}

	while (true) {
		ll cnt = get_cnt(month_, year_);
		int c = cnt % 7;

		cnt *= 24;
		cnt *= 60;
		cnt *= 60;

		if (t >= cnt) {
			t -= cnt;
			month_++;
			day_ += c;
			day_ %= 7;
		} else {
			break;
		}
	}

	while (true) {
		ll cnt = 24 * 60 * 60;

		if (t >= cnt) {
			t -= cnt;
			date_++;
			day_++;
			day_ %= 7;
		} else {
			break;
		}
	}

	while (true) {
		ll cnt = 60 * 60;

		if (t >= cnt) {
			t -= cnt;
			h_++;
		} else {
			break;
		}
	}

	while (true) {
		ll cnt = 60;

		if (t >= cnt) {
			t -= cnt;
			m_++;
		} else {
			break;
		}
	}

	while (true) {
		ll cnt = 1;

		if (t >= cnt) {
			t -= cnt;
			s_++;
		} else {
			break;
		}
	}

	ll count = 0;

	while (!(date_ == 1 && s_ == 0 && m_ == 0 && h_ == 0)) {
		count++;
		s_++;

		if (s_ == 60) {
			s_ = 0;
			m_++;
		}

		if (m_ == 60) {
			h_++;
			m_ = 0;
		}

		if (h_ == 24) {
			date_++;
			day_++;

			if (day_ == 7) {
				day_ = 0;
			}

			h_ = 0;
		}

		if (date_ == get_cnt(month_, year_) + 1) {
			date_ = 1;
			month_++;
		}

		if (month_ == 13) {
			month_ = 1;
			year_++;
		}

		if (conf(month_, day_, date_, h_, m_, s_)) {
			return tt + count;
		}
	}

	while (month != -1 && month != month_) {
		count += (ll)get_cnt(month_, year_) * 60 * 60 * 24;
		day_ += get_cnt(month_, year_);
		day_ %= 7;
		month_++;

		if (month_ == 13) {
			month_ = 1;
			year_++;
		}
	}

	while (!check_corr(day_, date)) {
		date_++;
		day_++;
		if (day_ == 7) {
			day_ = 0;
		}

		count += 24 * 60 * 60;
	}

	while (true) {
		count++;
		s_++;

		if (s_ == 60) {
			s_ = 0;
			m_++;
		}

		if (m_ == 60) {
			h_++;
			m_ = 0;
		}

		if (h_ == 24) {
			date_++;
			day_++;

			if (day_ == 7) {
				day_ = 0;
			}

			h_ = 0;
		}

		if (date_ == get_cnt(month_, year_) + 1) {
			date_ = 1;
			month_++;
		}

		if (month_ == 13) {
			month_ = 1;
			year_++;
		}

		if (conf(month_, day_, date_, h_, m_, s_)) {
			return tt + count;
		}
	}
}

int main() {
	cin >> s >> m >> h >> day >> date >> month;
	if (day != -1) {
		day--;
	}

	int n;

	cin >> n;

	for (int i = 0; i < n; i++) {
		ll t;

		cin >> t;
		if (n >= 1000) {
			break;
		}
		//t = (ll)1e12;

		cout << go(t) << endl;
	}

	return 0;
}


#include <iostream>
using namespace std;

int main()
{
int n, t, sum=0; cin>>n;
for(int i=0; i<n; i++){
cin >> t;
sum+=t;
}
if(sum==n-1 || n==1) cout << "YES\n";
else cout << "NO\n";
}


#include <iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int c=0;
    for(int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        if(a==0)
        c++;
        if(a==1&&n==1)
        {cout<<"YES";
        return 0;
        }
        if(a==0&&n==1)
        {cout<<"NO";
        return 0;
        }

        if(c>1)
        {
            cout<<"NO";

            return 0;
        }
    }
    if(c==0)
    cout<<"NO";
    else
    cout<<"YES";
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int c=0;
    for(int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        if(a==0)
        c++;
        if(a==0&&n==1)
        {cout<<"NO";
        return 0;
        }
        if(c>1)
        {
            cout<<"NO";

            return 0;
        }
    }
    if(c==0)
    cout<<"NO";
    else
    cout<<"YES";
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int c=0;
    for(int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        if(a==0)
        c++;
        if(c>1)
        {
            cout<<"NO";

            return 0;
        }
    }
    cout<<"YES";
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int c=0;
    for(int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        if(a==0)
        c++;
        if(a>1)
        {
            cout<<"NO";

            return 0;
        }
    }
    cout<<"YES";
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int c=0;
    for(int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        if(a==0)
        c++;
        if(a==1&&n==1)
        {cout<<"YES";
        return 0;
        }
        if(c>1)
        {
            cout<<"NO";

            return 0;
        }
    }
    if(c==0)
    cout<<"NO";
    else
    cout<<"YES";
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int c=0;
    for(int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        if(a==0)
        c++;
        if(c>1)
        {
            cout<<"NO";

            return 0;
        }
    }
    if(c==0)
    cout<<"NO";
    else
    cout<<"YES";
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int c=0;
    for(int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        if(a==0)
        c++;
        if(a==0&&n==1)
        {cout<<"YES";
        return 0;
        }
        if(c>1)
        {
            cout<<"NO";

            return 0;
        }
    }
    if(c==0)
    cout<<"NO";
    else
    cout<<"YES";
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int c=0;
    for(int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        if(a==0)
        c++;
        if(a==0&&n==1)
        {cout<<"NO";
        return 0;
        }
        if(c>1)
        {
            cout<<"NO";

            return 0;
        }
    }
    if(c==0)
    cout<<"NO";
    cout<<"YES";
    return 0;
}


/*
	* Lecture 01 : Vector
	* 24/05/2017
*/

#include <iostream>
#include <vector>
#include <string>
using namespace std;

char* test(vector<int> a)
{
	int count = 0;
	int n = a.size();

	if (n == 1)
	{
		if (a[0] == 1)
			return "YES";
		return "NO";
	}

	for (int i = 0; i < n; i++)
	{
		if (a[i] == 0)
		{
			count++;
		}

		if (count > 1)
			return "NO";
	}

	if (count == 1)
		return "YES";

	return "NO";
}

int main()
{
	vector<int> v;
	int  n;
	cin >> n;
	v.resize(n);
	for (int i = 0; i < n; i++)
	{
		cin >> v[i];
	}

	cout << test(v);

	return 0;
}


/*
	* Lecture 01 : Vector
	* 24/05/2017
*/

#include <iostream>
#include <vector>
#include <string>
using namespace std;

char* test(vector<int> a)
{
	int count = 0;
	int n = a.size();

	if (n == 1)
	{
		if (a[0] == 1)
			return "NO";
		return "YES";
	}

	for (int i = 0; i < n; i++)
	{
		if (a[i] == 0)
		{
			count++;
		}

		if (count > 1)
			return "NO";
	}

	if (count == 1)
		return "YES";

	return "NO";
}

int main()
{
	vector<int> v;
	int  n;
	cin >> n;
	v.resize(n);
	for (int i = 0; i < n; i++)
	{
		cin >> v[i];
	}

	cout << test(v);

	return 0;
}


/*
	* Lecture 01 : Vector
	* 24/05/2017
*/

#include <iostream>
#include <vector>
#include <string>
using namespace std;

char* test(vector<int> a)
{
	int count = 0;
	int n = a.size();

	if (n == 1 && a[0] == 1)
		return "YES";

	for (int i = 0; i < n; i++)
	{
		if (a[i] == 0)
		{
			count++;
		}

		if (count > 1)
			return "NO";
	}

	if (count == 1)
		return "YES";

	return "NO";
}

int main()
{
	vector<int> v;
	int  n;
	cin >> n;
	v.resize(n);
	for (int i = 0; i < n; i++)
	{
		cin >> v[i];
	}

	cout << test(v);

	return 0;
}


/*
	* Lecture 01 : Vector
	* 24/05/2017
*/

#include <iostream>
#include <vector>
#include <string>
using namespace std;

char* test(vector<int> a)
{
	int count = 0;
	int n = a.size();

	if (n == 1 && a[0] == 0)
		return "NO";

	for (int i = 0; i < n; i++)
	{
		if (a[i] == 0)
		{
			count++;
		}

		if (count > 1)
			return "NO";
	}

	if (count == 1)
		return "NO";
	return "YES";
}

int main()
{
	vector<int> v;
	int  n;
	cin >> n;
	v.resize(n);
	for (int i = 0; i < n; i++)
	{
		cin >> v[i];
	}

	cout << test(v);

	return 0;
}


#include <iostream>
#include <vector>
using namespace std;

char* test(vector<int> a)
{
	int count = 0;
	int n = a.size();

	if (n == 1 && a[0] == 0)
		return "NO";

	for (int i = 0; i < n; i++)
	{
		if (a[i] == 0)
		{
			count++;
		}

		if (count > 1)
			return "NO";
	}

	return "YES";
}

int main()
{
	vector<int> v;
	int  n;
	cin >> n;
	v.resize(n);
	for (int i = 0; i < n; i++)
	{
		cin >> v[i];
	}

	cout << test(v);

	return 0;
}


/*
	* Lecture 01 : Vector
	* 24/05/2017
*/

#include <iostream>
#include <vector>
#include <string>
using namespace std;

char* test(vector<int> a)
{
	int count = 0;
	int n = a.size();

	if (n == 1 && a[0] == 0)
		return "NO";

	for (int i = 0; i < n; i++)
	{
		if (a[i] == 0)
		{
			count++;
		}

		if (count > 1)
			return "NO";
	}

	if (count == 1)
		return "YES";

	return "NO";
}

int main()
{
	vector<int> v;
	int  n;
	cin >> n;
	v.resize(n);
	for (int i = 0; i < n; i++)
	{
		cin >> v[i];
	}

	cout << test(v);

	return 0;
}


/*** 	short int -> 2 bytes
	for scanning short int use %hd format specifier
	for printing short int use %i or %d format specifier
	The diagonals of parallelogram intersect each other in the middle.
***/
/***
	for taking input from file use ./a.out < input.txt
	for printing output to file use ./a.out > output.txt
***/
/***
	Always use printf/scanf for I/O
***/
/***
	Think twice code once..
	check the result by developing test cases on your own before submitting the code.
***/
#include<bits/stdc++.h>
using namespace std;
#define all(points) points.begin(),points.end()
#define S(points) sort(points.begin(),points.end())
#define pb push_back
#define ppb pop_back
#define MOD (ll)(1e9+7)
#define f first
#define s second
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
#define INF LLONG_MAX
#define NINF LLONG_MIN
#define sc(n) scanf("%lld",&n)
#define pr(n) printf("%lld ",n)
#define endl '\n'
typedef long long int ll;
typedef long long unsigned llu;
typedef long double ld;
int main(void)
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	ll n;
	cin>>n;ll cnt=0;
	for(ll i=0;i<n;i++)
	{
		bool x;
		cin>>x;
		x==0?cnt++:cnt+=0;
	}
	(cnt==1||n==1)?cout<<"YES\n":cout<<"NO\n";
	return 0;
}


/*** 	short int -> 2 bytes
	for scanning short int use %hd format specifier
	for printing short int use %i or %d format specifier
	The diagonals of parallelogram intersect each other in the middle.
***/
/***
	for taking input from file use ./a.out < input.txt
	for printing output to file use ./a.out > output.txt
***/
/***
	Always use printf/scanf for I/O
***/
/***
	Think twice code once..
	check the result by developing test cases on your own before submitting the code.
***/
#include<bits/stdc++.h>
using namespace std;
#define all(points) points.begin(),points.end()
#define S(points) sort(points.begin(),points.end())
#define pb push_back
#define ppb pop_back
#define MOD (ll)(1e9+7)
#define f first
#define s second
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
#define INF LLONG_MAX
#define NINF LLONG_MIN
#define sc(n) scanf("%lld",&n)
#define pr(n) printf("%lld ",n)
#define endl '\n'
typedef long long int ll;
typedef long long unsigned llu;
typedef long double ld;
int main(void)
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	ll n;
	cin>>n;ll cnt=0;
	for(ll i=0;i<n;i++)
	{
		bool x;
		cin>>x;
		x==1?cnt++:cnt+=0;
	}
	n==1?cnt==1?cout<<"YES\n":cout<<"NO\n":cnt+1==n?cout<<"YES\n":cout<<"NO\n";
	return 0;
}


/*** 	short int -> 2 bytes
	for scanning short int use %hd format specifier
	for printing short int use %i or %d format specifier
	The diagonals of parallelogram intersect each other in the middle.
***/
/***
	for taking input from file use ./a.out < input.txt
	for printing output to file use ./a.out > output.txt
***/
/***
	Always use printf/scanf for I/O
***/
/***
	Think twice code once..
	check the result by developing test cases on your own before submitting the code.
***/
#include<bits/stdc++.h>
using namespace std;
#define all(points) points.begin(),points.end()
#define S(points) sort(points.begin(),points.end())
#define pb push_back
#define ppb pop_back
#define MOD (ll)(1e9+7)
#define f first
#define s second
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
#define INF LLONG_MAX
#define NINF LLONG_MIN
#define sc(n) scanf("%lld",&n)
#define pr(n) printf("%lld ",n)
#define endl '\n'
typedef long long int ll;
typedef long long unsigned llu;
typedef long double ld;
int main(void)
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	ll n;
	cin>>n;ll cnt=0;
	for(ll i=0;i<n;i++)
	{
		bool x;
		cin>>x;
		x==1?cnt++:cnt+=0;
	}
	if(n==1||cnt==n-1)
		cout<<"YES\n";
	else
		cout<<"NO\n";
	return 0;
}


/*** 	short int -> 2 bytes
	for scanning short int use %hd format specifier
	for printing short int use %i or %d format specifier
	The diagonals of parallelogram intersect each other in the middle.
***/
/***
	for taking input from file use ./a.out < input.txt
	for printing output to file use ./a.out > output.txt
***/
/***
	Always use printf/scanf for I/O
***/
/***
	Think twice code once..
	check the result by developing test cases on your own before submitting the code.
***/
#include<bits/stdc++.h>
using namespace std;
#define all(points) points.begin(),points.end()
#define S(points) sort(points.begin(),points.end())
#define pb push_back
#define ppb pop_back
#define MOD (ll)(1e9+7)
#define f first
#define s second
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
#define INF LLONG_MAX
#define NINF LLONG_MIN
#define sc(n) scanf("%lld",&n)
#define pr(n) printf("%lld ",n)
#define endl '\n'
typedef long long int ll;
typedef long long unsigned llu;
typedef long double ld;
int main(void)
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	ll n;
	cin>>n;ll cnt=0;
	for(ll i=0;i<n;i++)
	{
		bool x;
		cin>>x;
		x==0?cnt++:cnt+=0;
	}
	cnt==1?cout<<"YES\n":cout<<"NO\n";
}


/*** 	short int -> 2 bytes
	for scanning short int use %hd format specifier
	for printing short int use %i or %d format specifier
	The diagonals of parallelogram intersect each other in the middle.
***/
/***
	for taking input from file use ./a.out < input.txt
	for printing output to file use ./a.out > output.txt
***/
/***
	Always use printf/scanf for I/O
***/
/***
	Think twice code once..
	check the result by developing test cases on your own before submitting the code.
***/
#include<bits/stdc++.h>
using namespace std;
#define all(points) points.begin(),points.end()
#define S(points) sort(points.begin(),points.end())
#define pb push_back
#define ppb pop_back
#define MOD (ll)(1e9+7)
#define f first
#define s second
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
#define INF LLONG_MAX
#define NINF LLONG_MIN
#define sc(n) scanf("%lld",&n)
#define pr(n) printf("%lld ",n)
#define endl '\n'
typedef long long int ll;
typedef long long unsigned llu;
typedef long double ld;
int main(void)
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	ll n;
	cin>>n;ll cnt=0;
	for(ll i=0;i<n;i++)
	{
		bool x;
		cin>>x;
		x==0?cnt++:cnt+=0;
	}
	(cnt+1==n||n==1)?cout<<"YES\n":cout<<"NO\n";
	return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define maxn 2010
#define ll long long
using namespace std;

int n , e , ans;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << setprecision(20) << fixed;

    for(int i = 0; i < n; i++)
    {
        cin >> e;

        if(e == 0)
        {
            ans++;
        }
    }

    if(ans == 1)
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define maxn 2010
#define ll long long
using namespace std;

int n , e , ans;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << setprecision(20) << fixed;

    cin >> n;

    int start = -1;

    for(int i = 0; i < n; i++)
    {
        cin >> e;

        if(start == -1)
        {
            start = e;
        }
        else
        {
            if(e == (start ^ 1))
            {
                start ^= 1;
            }
            else
            {
                cout << "NO" << endl;
                return 0;
            }
        }
    }

    cout << "YES" << endl;

    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define maxn 2010
#define ll long long
using namespace std;

int n , e , ans;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << setprecision(20) << fixed;

    cin >> n;

    int start = -1;

    for(int i = 0; i < n; i++)
    {
        cin >> e;

        if(e == 1)
        {
            ans++;
        }
    }

    if(n == 1 && e == 0)
    {
        cout << "NO" << endl;
        return 0;
    }

    if(ans >= (n - 1))
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define maxn 2010
#define ll long long
using namespace std;

int n , e , ans;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << setprecision(20) << fixed;

    cin >> n;

    for(int i = 0; i < n; i++)
    {
        cin >> e;

        if(e == 1)
        {
            ans++;
        }
    }

    if(n == 1 && e == 1)
    {
        cout << "YES" << endl;
    }

    if(ans >= (n - 1))
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define maxn 2010
#define ll long long
using namespace std;

int n , e , ans;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << setprecision(20) << fixed;

    cin >> n;

    for(int i = 0; i < n; i++)
    {
        cin >> e;

        if(e == 0)
        {
            ans++;
        }
    }

    if(n == 1 && ans == 0)
    {
        cout << "YES" << endl;
        return 0;
    }

    if(ans == 1)
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define maxn 2010
#define ll long long
using namespace std;

int n , e , ans;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << setprecision(20) << fixed;

    cin >> n;

    for(int i = 0; i < n; i++)
    {
        cin >> e;

        if(e == 0)
        {
            ans++;
        }
    }

    if(ans == 1)
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define maxn 2010
#define ll long long
using namespace std;

int n , e , ans;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << setprecision(20) << fixed;

    cin >> n;

    for(int i = 0; i < n; i++)
    {
        cin >> e;

        if(e == 1)
        {
            ans++;
        }
    }

    if(ans >= n - 1)
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define maxn 2010
#define ll long long
using namespace std;

int n , e , ans;

string x;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << setprecision(20) << fixed;

    cin >> n;

    for(int i = 0; i < n; i++)
    {
        cin >> e;

        if(e == 1)
        {
            ans++;
        }
    }

    if(n == 1)
    {
        if(e == 0)
        {
            cout << "NO" << endl;
        }
        else
        {
            cout << "YES" << endl;
        }

        return 0;
    }

    if(ans == (n - 1))
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }

    /*cin >> x;

    int low = 0;

    while(low < x.length() && x[low] != '0')
    {
        low++;
    }

    int high = x.length() - 1;

    if(high >= 0 && x[high] != '0')
    {
        high--;
    }

    if(low == high)
    {
        cout << x[low];
        return 0;
    }

    string before , after;

    bool point = false;

    for(int i = low; i <= high; i++)
    {
        if(x[i] == '.')
        {
            point = true;
        }
        else
        {
            if(point)
            {

            }
        }
    }*/

    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define maxn 2010
#define ll long long
using namespace std;

int n , e , ans;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << setprecision(20) << fixed;

    cin >> n;

    for(int i = 0; i < n; i++)
    {
        cin >> e;

        if(e == 0)
        {
            ans++;
        }
    }

    if(n == 1 && ans == 0)
    {
        cout << "YES" << endl;
        return 0;
    }

    if(ans <= 1)
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define maxn 2010
#define ll long long
using namespace std;

int n , e , ans;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << setprecision(20) << fixed;

    cin >> n;

    for(int i = 0; i < n; i++)
    {
        cin >> e;

        if(e == 0)
        {
            ans++;
        }
    }

    if(ans == 1 || (n == 1 && e == 1))
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#define endl '\n'
#define maxn 2010
#define ll long long
using namespace std;

int n , e , ans;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << setprecision(20) << fixed;

    cin >> n;

    for(int i = 0; i < n; i++)
    {
        cin >> e;

        if(e == 0)
        {
            ans++;
        }
    }

    if(ans == 1 || n == 1)
    {
        cout << "YES" << endl;
    }
    else
    {
        cout << "NO" << endl;
    }

    return 0;
}


#include<iostream>
using namespace std;

int main()
{
	int n, c, i, m;
	cin >> n;
	c = 0;
	for (i = 1; i <= n; i++)
	{
		cin >> m;
		if (m == 0)
		{
			c++;
		}
		if (c == 2)
		{
			cout << "NO";
			return 0;
		}
	}

	if (n == 1)
	{
		if (c == 0)
		{
			cout << "YES";
			return 0;
		}
		else
		{
			cout << "NO";
		}
	}

	if (c == 1 )
		cout << "YES";
	else cout << "NO";

	return 0;
}


#include<iostream>
using namespace std;

int main()
{
	int n, c, i, m;
	cin >> n;
	c = 0;
	for (i = 1; i <= n; i++)
	{
		cin >> m;
		if (m == 0)
		{
			c++;
		}
		if (c == 2)
		{
			cout << "NO";
			return 0;
		}
	}

	if (c == 1 && n == 1)
		cout << "YES";
	else if (c == 1)
		cout << "YES";
	else cout << "NO";

	return 0;
}


#include<iostream>
using namespace std;

int main()
{
	int n, i, h, m, c, a;
	cin >> n;
	a = 0;
	for (i = 1; i <= n; i++)
	{
		c = 0;
			cin >> m;
			if (m == 1)
				++c;
		if (c >= 2)
			++a;
	}

	cout << a;

	return 0;
}


#include<iostream>
using namespace std;

int main()
{
	int n, c, i, m;
	cin >> n;
	c = 0;
	for (i = 1; i <= n; i++)
	{
		cin >> m;
		if (m == 0)
		{
			c++;
		}
		if (c == 2)
		{
			cout << "NO";
			return 0;
		}
	}

	if (n == 1)
	{
		if (c == 0)
		{
			cout << "YES";
			return 0;
		}
		else
		{
			cout << "NO";
			return 0;
		}
	}

	if (c == 1 )
		cout << "YES";
	else cout << "NO";

	return 0;
}


#include<iostream>
using namespace std;

int main()
{
	int n, c, i, m;
	cin >> n;
	c = 0;
	for (i = 1; i <= n; i++)
	{
		cin >> m;
		if (m == 0)
		{
			c++;
		}
		if (c == 2)
		{
			cout << "NO";
			return 0;
		}
	}

	if (c == 0 && n == 1)
		cout << "YES";
	else if (c == 1)
		cout << "YES";
	else cout << "NO";

	return 0;
}


#include<iostream>
using namespace std;

int main()
{
	int n, c, i, m;
	cin >> n;
	c = 0;
	for (i = 1; i <= n; i++)
	{
		cin >> m;
		if (m == 0)
		{
			c++;
		}
		if (c == 2)
		{
			cout << "NO";
			return 0;
		}
	}

	if (n == 1)
	{
		if (c == 0)
			cout << "YES";
		else cout << "NO";
	}

	if (c == 1 )
		cout << "YES";
	else cout << "NO";

	return 0;
}


#include<iostream>
using namespace std;

int main()
{
	int n, c, i, m;
	cin >> n;
	c = 0;
	for (i = 1; i <= n; i++)
	{
		cin >> m;
		if (m == 0)
		{
			c++;
		}
		if (c == 2)
		{
			cout << "NO";
			return 0;
		}
	}

	if (c == 1 && n == 1)
		cout << "NO";
	else if (c == 1)
		cout << "YES";
	else cout << "NO";

	return 0;
}


#include<iostream>
using namespace std;

int main()
{
	int n, i, h, m, c, a;
	cin >> n;
	a = 0;
	for (i = 1; i <= n; i++)
	{
		c = 0;
			cin >> m;
			if (m == 1)
				++c;
		if (c >= 2)
			++a;
	}

	cout << a;

	return 0;


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    ll hash[2]={0};
    for(int i=0;i<n;i++)
        hash[a[i]]++;
    if(n==1 && a[0]==0)
        cout<<"NO";
    else
    {
        if(hash[0]==1 || hash[0]==0)
            cout<<"YES";
        else
            cout<<"NO";
    }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    ll hash[2]={0};
    for(int i=0;i<n;i++)
        hash[a[i]]++;
    if(n==1 && a[0]==0)
        cout<<"NO";
    else
    {
        if(hash[0]==1)
            cout<<"YES";
        else
            cout<<"NO";
    }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    ll hash[2]={0};
    for(int i=0;i<n;i++)
        hash[a[i]]++;
    if(n==1)
    {
        if(a[0]==0)
            cout<<"NO";
        else
            cout<<"YES";
    }
    else
    {
        if(hash[0]==1)
            cout<<"YES";
        else
            cout<<"NO";
    }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    if(a[0]==1 && a[n-1]==1)
        cout<<"YES";
    else
        cout<<"NO";

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007

int main()
{
    ll n;
    cin>>n;
    ll a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    ll hash[2]={0};
    for(int i=0;i<n;i++)
        hash[a[i]]++;
    if(n==1 && a[0]==0)
        cout<<"NO";
    else
    {
        if(hash[0]==1 && hash[0]==0)
            cout<<"YES";
        else
            cout<<"NO";
    }

}


#include <iostream>
#include <vector>
using namespace std;
string check(int n,vector<int> v) {
	if (n == 1) {
		if (v[0] == 1) return "YES";
		else return "NO";
	}
	int count = 0;
	for (int i = 0; i < n; i++) {
		if (v[i] == 0) count++;
		if (count > 1) return "NO";
	}
	return "YES";

}
int main() {
	int n;
	cin >> n;
	vector<int> v;
	for (int i = 0; i < n; i++) {
		cin >> i;
		v.push_back(i);
	}
	check(n, v);
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;
string check(int n,vector<int> v) {
	if (n == 1) {
		if (v[0] == 1) return "YES";
		else return "NO";
	}
	int count = 0;
	for (int i = 0; i < n; i++) {
		if (v[i] == 0) count++;
		if (count > 1) return "NO";
	}
	return "YES";

}
int main() {
	int n;
	cin >> n;

	vector<int> v;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		v.push_back(x);
	}
	cout <<	check(n, v);
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
string check(int n,vector<int> v) {
	if (n == 1) {
		if (v[0] == 1) return "YES";
		else return "NO";
	}
	int count = 0;
	for (int i = 0; i < n; i++) {
		if (v[i] == 0) count++;
		if (count > 1) return "NO";
	}
	return "YES";

}
int main() {
	int n;
	cin >> n;
	vector<int> v;
	for (int i = 0; i < n; i++) {
		cin >> i;
		v.push_back(i);
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;
string check(int n,vector<int> v) {
	if (n == 1) {
		if (v[0] == 1) return "YES";
		else return "NO";
	}
	int count = 0;
	for (int i = 0; i < n; i++) {
		if (v[i] == 0) count++;
		if (count > 1  ) return "NO";
	}
	if (count == 0) return "NO";
	return "YES";

}
int main() {
	int n;
	cin >> n;

	vector<int> v;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		v.push_back(x);
	}
	cout <<	check(n, v);
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;
string check(int n,vector<int> v) {
	if (n == 1) {
		if (v[0] == 1) return "YES";
		else return "NO";
	}
	int count = 0;
	for (int i = 0; i < n; i++) {
		if (v[i] == 0) count++;
		if (count > 1 && count == 0) return "NO";
	}
	return "YES";

}
int main() {
	int n;
	cin >> n;

	vector<int> v;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		v.push_back(x);
	}
	cout <<	check(n, v);
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;
string check(int n,vector<int> v) {
	if (n == 1) {
		if (v[0] == 1) return "YES";
		else return "NO";
	}
	int count = 0;
	for (int i = 0; i < n; i++) {
		if (v[i] == 0) count++;
		if (count > 1 ) return "NO";
	}
	//if (count == 0) return "YES";
	return "YES";

}
int main() {
	int n;
	cin >> n;

	vector<int> v;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		v.push_back(x);
	}
	cout <<	check(n, v);
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
string check(int n,vector<int> v) {
	if (n == 1) {
		if (v[0] == 1) return "YES";
		else return "NO";
	}
	int count = 0;
	for (int i = 0; i < n; i++) {
		if (v[i] == 0) count++;
		if (count > 1) return "NO";
	}
	return "YES";

}
int main() {
	int n;
	cin >> n;

	vector<int> v;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		v.push_back(x);
	}
	check(n, v);
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;
string check(int n,vector<int> v) {
	if (n == 1) {
		if (v[0] == 1) return "YES";
		else return "NO";
	}
	int count = 0;
	for (int i = 0; i < n; i++) {
		if (v[i] == 0) count++;
		if (count > 1  ) return "NO";
	}
	if (count == 0) return "NO";
	return "YES";

}
int main() {
	int n;
	cin >> n;
	int x;
	vector<int> v;
	for (int i = 0; i < n; i++) {

		cin >> x;
		v.push_back(x);
	}
	cout <<	check(n, v);
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
string check(int n,vector<int> v) {
	if (n == 1) {
		if (v[0] == 1) return "YES";
		else return "NO";
	}
	int count = 0;
	for (int i = 0; i < n; i++) {
		if (v[i] == 0) count++;
		if (count > 1) return "NO";
	}
	return "YES";

}
int main() {
	int n;
	cin >> n;
	int x;
	vector<int> v;
	for (int i = 0; i < n; i++) {
		cin >> x;
		v.push_back(x);
	}
	check(n, v);
	return 0;
}


#include <bits/stdc++.h>
#include<iostream>
using namespace std;
#define ll long long
int main()
{

  int n,z=0,o=0;
  cin>>n;
  int p[n];
  for(int i=0;i<n;i++)
  {
      cin>>p[i];
      if(p[i]==0)
      {
          z++;
      }
      else
      {
          o++;
      }
  }
  if(o>n/2)
  {
      cout<<"YES"<<endl;
  }
  else
  {
      cout<<"NO"<<endl;
  }

}


#include <bits/stdc++.h>
#include<iostream>
using namespace std;
#define ll long long
int main()
{

  int n,z=0,o=0;
  cin>>n;
  int p[n];
  for(int i=0;i<n;i++)
  {
      cin>>p[i];
      if(p[i]==0)
      {
          z++;
      }
      else
      {
          o++;
      }
  }
  if((o>1 && o!=n )||(n<=2 && o==1))
  {
      cout<<"YES"<<endl;
  }
  else
  {
      cout<<"NO"<<endl;
  }

}


#include <bits/stdc++.h>
#include<iostream>
using namespace std;
#define ll long long
int main()
{

  int n,z=0,o=0;
  cin>>n;
  int p[n];
  for(int i=0;i<n;i++)
  {
      cin>>p[i];
      if(p[i]==0)
      {
          z++;
      }
      else
      {
          o++;
      }
  }
  if(o>1)
  {
      cout<<"YES"<<endl;
  }
  else
  {
      cout<<"NO"<<endl;
  }

}


#include <bits/stdc++.h>
#include<iostream>
using namespace std;
#define ll long long
int main()
{

  int n,z=0,o=0;
  cin>>n;
  int p[n];
  for(int i=0;i<n;i++)
  {
      cin>>p[i];
      if(p[i]==0)
      {
          z++;
      }
      else
      {
          o++;
      }
  }
  if(o>n/2  && (o!=n)&& (o!=1) &&(z!=0))
  {
      cout<<"YES"<<endl;
  }
  else
  {
      cout<<"NO"<<endl;
  }

}


#include <bits/stdc++.h>
#include<iostream>
using namespace std;
#define ll long long
int main()
{

  int n,z=0,o=0;
  cin>>n;
  int p[n];
  for(int i=0;i<n;i++)
  {
      cin>>p[i];
      if(p[i]==0)
      {
          z++;
      }
      else
      {
          o++;
      }
  }
  if((o>1 && o!=n )||(n==2 && o==1))
  {
      cout<<"YES"<<endl;
  }
  else
  {
      cout<<"NO"<<endl;
  }

}


#include <bits/stdc++.h>
#include<iostream>
using namespace std;
#define ll long long
int main()
{

  int n,z=0,o=0;
  cin>>n;
  int p[n];
  for(int i=0;i<n;i++)
  {
      cin>>p[i];
      if(p[i]==0)
      {
          z++;
      }
      else
      {
          o++;
      }
  }
  if((n==1 &&o==1)||(z==1 && o==n-1))
  {
      cout<<"YES"<<endl;
  }
  else
  {
      cout<<"NO"<<endl;
  }

}


#include <bits/stdc++.h>
#include<iostream>
using namespace std;
#define ll long long
int main()
{

  int n,z=0,o=0;
  cin>>n;
  int p[n];
  for(int i=0;i<n;i++)
  {
      cin>>p[i];
      if(p[i]==0)
      {
          z++;
      }
      else
      {
          o++;
      }
  }
  if((o>n/2  && (o!=n)&& (o!=1) &&(z!=0))
  {
      cout<<"YES"<<endl;
  }
  else
  {
      cout<<"NO"<<endl;
  }

}


#include <bits/stdc++.h>
#include<iostream>
using namespace std;
#define ll long long
int main()
{

  int n,z=0,o=0;
  cin>>n;
  int p[n];
  for(int i=0;i<n;i++)
  {
      cin>>p[i];
      if(p[i]==0)
      {
          z++;
      }
      else
      {
          o++;
      }
  }
  //cout<<z<< " "<<o<<endl;

  if(n==1 && o==1)
  {
      cout<<"YES"<<endl;
  }
  else if((z==1 && o>1 && o==n-1))
  {
      cout<<"YES"<<endl;
  }
  else
  {
      cout<<"NO"<<endl;
  }

}


#include <bits/stdc++.h>
#include<iostream>
using namespace std;
#define ll long long
int main()
{

  int n,z=0,o=0;
  cin>>n;
  int p[n];
  for(int i=0;i<n;i++)
  {
      cin>>p[i];
      if(p[i]==0)
      {
          z++;
      }
      else
      {
          o++;
      }
  }
  if(o>1 && o!=n)
  {
      cout<<"YES"<<endl;
  }
  else
  {
      cout<<"NO"<<endl;
  }

}


#include <bits/stdc++.h>
#include<iostream>
using namespace std;
#define ll long long
int main()
{

  int n,z=0,o=0;
  cin>>n;
  int p[n];
  for(int i=0;i<n;i++)
  {
      cin>>p[i];
      if(p[i]==0)
      {
          z++;
      }
      else
      {
          o++;
      }
  }
  if(o>n/2 && o!=n)
  {
      cout<<"YES"<<endl;
  }
  else
  {
      cout<<"NO"<<endl;
  }

}


#include <bits/stdc++.h>
#include<iostream>
using namespace std;
#define ll long long
int main()
{

  int n,z=0,o=0;
  cin>>n;
  int p[n];
  for(int i=0;i<n;i++)
  {
      cin>>p[i];
      if(p[i]==0)
      {
          z++;
      }
      else
      {
          o++;
      }
  }
  if(o>n/2  && (o!=n)&& (o!=1) )
  {
      cout<<"YES"<<endl;
  }
  else
  {
      cout<<"NO"<<endl;
  }

}


#include <bits/stdc++.h>
#include<iostream>
using namespace std;
#define ll long long
int main()
{

  int n,z=0,o=0;
  cin>>n;
  int p[n];
  for(int i=0;i<n;i++)
  {
      cin>>p[i];
      if(p[i]==0)
      {
          z++;
      }
      else
      {
          o++;
      }
  }
  //cout<<z<< " "<<o<<endl;

  if(n==1 && o==1)
  {
      cout<<"YES"<<endl;
  }
  else if((z==1 && o>1 && o==n-1)||(n==2 && z==1 && o==1))
  {
      cout<<"YES"<<endl;
  }
  else
  {
      cout<<"NO"<<endl;
  }

}


#include <iostream>
using namespace std;
int main()
{
	int n, i;
	int a[100];
	int check = 0;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> a[i];

	}
	for (i = 0; i < n; i++)
	{
		if (n == 1)
		{
			if (a[i] == 0) cout << "NO";
			else cout << "YES";
		}

	}
	for (i = 0; i < n; i++)
	{
			if (a[i] == 0)
			{
				check++;
			}

	}
	if (check == 1)
	{
		cout << "YES";
	}
	else cout << "NO";

	return 0;
}


#include<iostream>
#include<vector>
using namespace std;

int main()
{
	//freopen("INPUT.INP", "rt", stdin);
	int n, value, count = 0, res = 1, a[100];
	vector<int> v;
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	//Xét trường hợp có 1 chiếc nút.
	if (n == 1)
	{
		if (a[0] == 1)
		{
			cout << "YES";
		}
		else
		{
			cout << "NO";
		}
		return 0;
	}
	//Nếu từ 2 chiếc  trở lên thì đếm xem số lượng chiếc nút không được cài lớn hơn 1 là NO ngược lại là YES
	for (int i = 0; i<n; i++)
	{
		if (a[i] == 0)
			count++;
		if (count > 1)
		{
			res = 0;
			break;
		}
	}
	if (res == 1 && count == 1)
		cout << "YES";
	else
		cout << "NO";
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int n, i;
	int a[100];
	int check = 0;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> a[i];

	}
	for (i = 0; i < n; i++)
	{
		if (n == 1)
		{
			if (a[i] == 0) cout << "NO";
			else cout << "YES";
		}

	}
	for (i = 0; i < n; i++)
	{
			if (a[i] == 0)
			{
				check++;
			}
			/*else
				continue;*/
	}
	if (check == 1)
	{
		cout << "YES";
	}
	else cout << "NO";

	system("pause");
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int n, i;
	int a[100];
	int check = 0;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> a[i];

	}
	for (i = 0; i < n; i++)
	{
		if (n == 1)
		{
			if (a[i] == 0) cout << "NO";
			else cout << "YES";
		}

	}
	for (i = 0; i < n; i++)
	{
			if (a[i] == 0)
			{
				check++;
			}
			/*else
				continue;*/
	}
	if (check == 1)
	{
		cout << "YES";
	}
	else cout << "NO";

	return 0;
}


#include<iostream>
#include<vector>
using namespace std;

int main()
{
	//freopen("INPUT.INP", "rt", stdin);
	int n, value, count = 0, res = 1;
	vector<int> v;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> value;
		v.push_back(value);
	}

	//Xét trường hợp có 1 chiếc nút.
	if (n == 1)
	{
		if (v[0] == 1)
		{
			cout << "YES";
		}
		else
		{
			cout << "NO";
		}
		return 0;
	}
	//Nếu từ 2 chiếc  trở lên thì đếm xem số lượng chiếc nút không được cài lớn hơn 1 là NO ngược lại là YES
	for (int i = 0; i<n; i++)
	{
		if (v[i] == 0)
			count++;
		if (count > 1)
		{
			res = 0;
			break;
		}
	}
	if (res == 1 && count == 1)
		cout << "YES";
	else
		cout << "NO";
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int n, i;
	int a[100];
	int check = 0;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> a[i];

	}
	for (i = 0; i < n; i++)
	{
		if (n == 1)
		{
			if (a[i] == 0) cout << "NO";
			else cout << "YES";
		}

	}
	for (i = 0; i < n; i++)
	{
			if (a[i] == 0)
			{
				check++;
			}
			/*else
				continue;*/
	}
	if (check > 1)
	{
		cout << "NO";
	}
	else cout << "YES";

	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int n, i;
	int a[100];
	int check = 0;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> a[i];

	}
	for (i = 0; i < n; i++)
	{
		if (n == 1)
		{
			if (a[i] == 0) cout << "NO";
			else cout << "YES";
		}

	}
	for (i = 0; i < n; i++)
	{
			if (a[i] == 0)
			{
				check++;
			}
			/*else
				continue;*/
	}
	if (check > 1)
	{
		cout << "NO";
	}
	else cout << "YES";

	system("pause");
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	int n, i;
	int a[100];
	int check = 0;
	cin >> n;
	for (i = 0; i < n; i++)
	{
		cin >> a[i];
		if (n == 1)
		{
			if (a[0] == 0) check+2;
			else check++;
		}

	}

	for (i = 0; i < n; i++)
	{
			if (a[i] == 0)
			{
				check++;
			}
			/*else
				continue;*/
	}
	if (check == 1 )
	{
		cout << "YES";
	}
	else cout << "NO";

	return 0;
}


#include<iostream>
#include<vector>

using namespace std;

int main()
{
	int n;
	vector<int> v;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int x;
		cin >> x;
		v.push_back(x);
	}

	int kq=1;
	int dem = 0;

	if (v.size() == 1)
	{
		if (v[0] == 1)
			kq = 1;
		else
			kq = 0;
	}
	else
	{
		for (int i = 0; i < v.size(); i++)
		{
			if (v[i] == 0)
				dem++;
		}
		if (dem >= 2)
			kq = 0;
		else
			kq = 1;
	}

	if (kq == 0)
		cout << "NO";
	else
		cout << "YES";

	return 0;
}


#include<iostream>
#include<vector>

using namespace std;

int main()
{
	int n = 0;
	vector<int> v;
	cin >> n;
	int x=0;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		v.push_back(x);
	}

	int kq = 0;
	int dem = 0;

	if (n == 1)
	{
		if (v[0] == 1)
			cout << "YES";
		else
			cout << "NO";
	}
	else
	{
		for (int i = 0; i < v.size(); i++)
		{
			if (v[i] == 0)
				dem++;
		}
		if (dem == 1)
			cout << "YES";
		else
			cout << "NO";
	}

	return 0;
}


#include<iostream>
#include<vector>

using namespace std;

int main()
{
	int n = 0;
	vector<int> v;
	cin >> n;
	int x=0;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		v.push_back(x);
	}

	int kq = 0;
	int dem = 0;

	if (n == 1)
	{
		if (v[0] == 1)
			cout << "YES";
		else
			cout << "NO";
	}
	else
	{
		for (int i = 0; i < v.size(); i++)
		{
			if (v[i] == 0)
				dem++;
		}
		if (dem >= 2)
			cout << "NO";
		else
			cout << "YES";
	}

	return 0;
}


#include<iostream>
#include<vector>

using namespace std;

int main()
{
	int n;
	vector<int> v;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int x;
		cin >> x;
		v.push_back(x);
	}
	int dem = 0;
	if (v.size() == 1)
	{
		if (v[0] == 1)
			cout << "YES";
		else
			cout << "NO";
	}
	else
	{
		for (int i = 0; i < v.size(); i++)
		{
			if (v[i] == 0)
				dem++;
		}
		if (dem >= 2)
			cout << "NO";
		else
			cout << "YES";
	}

	return 0;
}


#include<iostream>
#include<vector>

using namespace std;

int main()
{
	int n;
	vector<int> v;
	cin >> n;
	int x=0;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		v.push_back(x);
	}

	int kq = 0;
	int dem = 0;

	if (n == 1)
	{
		if (v[0] == 1)
			cout << "YES";
		else
			cout << "NO";
	}
	else
	{
		for (int i = 0; i < v.size(); i++)
		{
			if (v[i] == 0)
				dem++;
		}
		if (dem == 1)
			cout << "YES";
		else
			cout << "NO";
	}

	return 0;
}


#include<iostream>
#include<vector>

using namespace std;

int main()
{
	int n;
	vector<int> v;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int x;
		cin >> x;
		v.push_back(x);
	}

	int kq=1;
	int dem = 0;

	if (v.size() == 1)
	{
		if (v[0] == 1)
			kq = 1;
		else
			kq = 0;
	}
	else
	{
		for (int i = 0; i < v.size(); i++)
		{
			if (v[i] == 0)
				dem++;
		}
		if (dem >= 2)
			kq = 0;
		else
			kq = 1;
	}

	if (kq == 0)
		cout << "NO\n";
	else
		cout << "YES\n";

	return 0;
}


#include<iostream>
#include<vector>

using namespace std;

int main()
{
	int n=0;
	int h=0;
	vector<int> v;
	cin >> n;
	cin >> h;
	int x;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		v.push_back(x);
	}
	int kq=0;
	for (int i = 0; i < v.size(); i++)
	{
		if (v[i] > h)
			kq += 2;
		else
			kq++;
	}

	cout << kq;

	return 0;
}


#include<iostream>
#include<vector>

using namespace std;

int main()
{
	int n=0;
	vector<int> v;
	cin >> n;
	int x=0;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		v.push_back(x);
	}

	int time = 0;
	int hieu = 0;
	for (int i = 0; i < v.size()-1; i++)
	{
		if (v[0] > 15)
		{
			time = 15;
			break;
		}
		hieu = v[i + 1] - v[i];
		if (hieu <= 15)
			continue;
		else
		{
			time = v[i]+15;
			break;
		}
	}

	if (time == 0)
		time = v[v.size() - 1]+15;
	if (time > 90)
		time = 90;

	cout << time;

	return 0;
}


#include<iostream>
#include<vector>

using namespace std;

int main()
{
	int n;
	vector<int> v;
	cin >> n;
	int x;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		v.push_back(x);
	}

	int dem = 0;

	if (n == 1)
	{
		if (v[0] == 1)
			cout << "YES";
		else
			cout << "NO";
	}
	else
	{
		for (int i = 0; i < v.size(); i++)
		{
			if (v[i] == 0)
				dem++;
		}
		if (dem >= 2)
			cout << "NO";
		else
			cout << "YES";
	}

	return 0;
}


#include<stdio.h>

int main()
{
	int n, c, a;
	c = 0;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		if (a == 0) c = c + 1;
		else continue;
	}
	if (n == 1 && a == 0) printf("NO");
	else if (a == 1 &&(n==1 || c == 1)) printf("YES");
	else printf("NO");
	return 0;

}


#include <stdio.h>
#include <iostream>
int main()
{
	int n, a, c = 0;
	scanf("%d", &n);

	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		if (a == 0)
			c= c+ 1; continue;
}  if ((n == 1) && (a== 1)) printf("YES");
	else if ((c == 1) || ((n == 1) && (a == 0))) printf("YES");
	else printf("NO");
	return 0;

   }


#include<stdio.h>

int main()
{
	int n, c, a;
	c = 0;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		if (a == 1) c = c + 1;
		else continue;
	}
	if (n == 1 && a == 0) printf("NO");
	else if (n == 1 && a == 1) printf("YES");
	else if (c <=(n-2)) printf("NO");
	else printf("YES");
	return 0;

}


#include<stdio.h>
int main()
{
	int n, c, a;
	c = 0;
	scanf("%d", &n);
	for(int i=1; i<=n; i++)
	{
		scanf("%d", &a);
		if (a == 1) c = c + 1;
		else continue;
	}
	if (n == 1 && a == 1) printf("YES");
	else if (c >= (n - 1)) printf("YES");
	else printf("NO");
	return 0;

}


#include <stdio.h>
#include <iostream>
int main()
{
	int n, a, c = 0;
	scanf("%d", &n);

	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		if (a == 0)
			c= c+ 1; continue;
}  if ((n == 1) && (a== 0)) printf("NO");
	else if ((c == 0) || ((n == 1) && (a == 1))) printf("YES");
	else if( c<(n-1)) printf("NO"); else printf("YES");
	return 0;

   }


#include <stdio.h>
#include <iostream>
int main()
{
	int n, a, c = 0;
	scanf("%d", &n);

	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		if (a == 0)
			c= c+ 1; continue;
}
if ((n == 1) && (a == 0)) printf("NO");
	else if ((c == 1) || ((n == 1) && (a == 1))) printf("YES");
	else printf("NO");
	return 0;
   }


#include <stdio.h>
#include <iostream>
int main()
{
	int n, a, c = 0;
	scanf("%d", &n);

	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		if (a == 0)
			c= c+ 1; else if ((n == 1) && (a== 0)) { printf("NO"); return 0;}
	else if ((c == 1) || ((n == 1) && (a == 1))) {printf("YES"); return 0;} else continue;
} if(n>1&& c>1) printf("NO"); return 0;
   }


#include <stdio.h>
#include <iostream>
int main()
{
	int n, a, c = 0;
	scanf("%d", &n);

	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		if (a == 1)
			c= c+ 1;
	}
	if ((n == 1) && (a== 0)) printf("NO");
	else if ((c == 0) || ((n == 1) && (a == 1))) printf("YES");
	else if( c<(n-1)) printf("NO"); else printf("YES");
	return 0;
}


#include<stdio.h>

int main()
{
	int n, c, a;
	c = 0;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		if (a == 0) c = c + 1;
		else continue;
	}
	if (n == 1 && a == 0&& c==1) printf("NO");
	else if (n == 1 && a == 1&&c==1) printf("YES");
	else if (c > 1) printf("NO");
	else printf("YES");
	return 0;

}


#include<stdio.h>
int main()
{
	int n, c, a;
	c = 0;
	scanf("%d", &n);
	for(int i=1; i<=n; i++)
	{
		scanf("%d", &a);
		if (a == 0) c = c + 1;
		else continue;
	}
	if ((n == 1 && a == 1)|| c==1) printf("YES");
	else if (n == 1 && a == 0) printf("NO");
	else printf("NO");
	return 0;

}


#include<stdio.h>
int main()
{
	int n, c, a;
	c = 0;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		if (a == 0) c = c + 1;
		else continue;
	}
	if (n == 1 && a == 0) printf("NO");
	else if ((n == 1 && a == 1)||c==1) printf("YES");
	else printf("YES");
	return 0;

}


#include<stdio.h>

int main()
{
	int n, c, a;
	c = 0;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		if (a == 0) c = c + 1;
		else continue;
	}
	if (n == 1 && a == 0&& c==1) printf("NO");
	else if (n == 1 && a == 1&&c==0) printf("YES");
	else if (c > 1) printf("NO");
	else printf("YES");
	return 0;

}


#include<stdio.h>
#include<iostream>

int main()
{
	int n, c, a;
	c = 0;
	scanf_s("%d", &n);
	for(int i=1; i<=n; i++)
	{
		scanf_s("%d", &a);
		if (n == 1 && a == 1) {
			printf("YES");  system("pause"); return 0;
	}
 else {
			printf("NO");  system("pause"); return 0;
		}
		c = c + a;

	}
	if (c < (n - 1)) printf("NO"); else printf("YES");
	system("pause");
	return 0;

}


#include<stdio.h>
int main()
{
	int n, c, a;
	c = 0;
	scanf("%d", &n);
	for(int i=1; i<=n; i++)
	{
		scanf("%d", &a);
		if (a == 0) c = c + 1;
		else continue;
	}
	if (n == 1 && a == 1) printf("YES");
	else if (n == 1 && a == 0) printf("NO");
	else if (c > 1) printf("NO");
	else printf("YES");
	return 0;

}


#include <stdio.h>
#include <iostream>
int main()
{
	int n, a, c = 0;
	scanf("%d", &n);

	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		if (a == 0)
			c = c + 1; continue;
	}
	if ((n == 1) && (a == 0)) printf("NO");
	else if ((c == 1) || ((n == 1) && (a == 1))) printf("YES");
	else printf("NO");
	return 0;
}


#include <stdio.h>
#include <iostream>
int main()
{
	int n, a, c = 0;
	scanf("%d", &n);

	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		if (a == 0)
			c= c+ 1;
	}
	if ((n == 1) && (a== 0)) printf("NO");
	else if ((c == 1) || ((n == 1) && (a == 1))) printf("YES");
	else printf("NO");
	return 0;
}


#include <stdio.h>
#include <iostream>
int main()
{
	int n, a, c = 0;
	scanf("%d", &n);

	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		if (a == 0)
			c= c+ 1; else continue;
	}
	if ((n == 1) && (a== 0)) printf("NO");
	else if ((c == 1) || ((n == 1) && (a == 1))) printf("YES");
	else printf("NO");
	return 0;
}


#include<stdio.h>

int main()
{
	int n, c, a;
	c = 0;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a);
		if (a == 0) c = c + 1;
		else continue;
	}
	if (n == 1 && a == 0) printf("NO");
	else if (n == 1 && a == 1) printf("YES");
	else if (c >=2) printf("NO");
	else printf("YES");
	return 0;

}


#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <math.h>
#include <iterator>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <string.h>
#include <cstring>
#include <queue>
#include <iomanip>
#include <istream>
#include <map>
#include <list>
#include <stack>
#include <numeric>
#include <ostream>
#include <set>
#include <cassert>
#include <sstream>
#include <string>
#include <utility>
#include <stdio.h>
#include <vector>
using namespace std;
#define dout if (debug) cout
#define PB push_back
#define MP make_pair
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int((x.size())))
typedef long double ld;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<PII> VPI;
#define REP(i,a,n) for (int i=a;i<n;i++)
#define db(x) cerr << #x << " = " << x << endl
#define PER(i,a,n) for (int i=n-1;i>=a;i--)
const int INF = 1000000404;
const ll MOD = 1000000007ll;
const ld PI = acos(-1.0);
const ld EPS = 1e-9;
template <typename t1, typename t2> inline void upmax(t1 &a, t2 b) { a = max(a, (t1)b); }
template <typename t1, typename t2> inline void upmin(t1 &a, t2 b) { a = min(a, (t1)b); }
template <typename T>inline T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template <typename T>inline T lcm(T a, T b) { return a*(b / gcd(a, b)); }
template <typename T>inline T sqr(T a) { return a*a; }
template <typename T>inline bool pal(T &x)
{
	int n = SZ(x); REP(i, 0, n / 2) { if (x[i] != x[n - i - 1]) return 0; }return 1;
}
template <typename T>inline void rev(T &x) { int n = SZ(x); REP(i, 0, n / 2) swap(x[i], x[n - i - 1]); }
int month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
int dy[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
inline ll mp(ll a, ll b) { return (a << 31) + b; }
class compare{
public:
	bool operator()(const int a, const int b) const {
		return 1;
	}
};
int SQ = 400;

#define debug 1
#define N 111111
int tin[N];
int tout[N];
int up[N][20];
int h[N];
int ar[N];
VI g[N];
VI t[N][20];
int c[N];
int timer, L;
VI merge(VI &a, VI &b) {
	VI c;
	auto x = a.begin();
	auto y = b.begin();

	while ((x != a.end() && y != b.end()) && SZ(c) < 10) {
		if (*x < *y) {
			c.push_back(*x); x++;
		}
		else {
			if (*x == *y) x++;
			c.push_back(*y);
			y++;
		}
	}
	while (x != a.end() && SZ(c)<10) {
		c.push_back(*x); x++;
	}
	while (y != b.end() && SZ(c)<10) {
		c.push_back(*y); y++;
	}
	return c;
}
void vmerge(VI &a, VI &b, VI &c) {

	auto x = a.begin();
	auto y = b.begin();

	while ((x != a.end() && y != b.end()) && SZ(c) < 10) {
		if (*x < *y) {
			c.push_back(*x); x++;
		}
		else {
			if (*x == *y) x++;
			c.push_back(*y);
			y++;
		}
	}
	while (x != a.end() && SZ(c)<10) {
		c.push_back(*x); x++;
	}
	while (y != b.end() && SZ(c)<10) {
		c.push_back(*y); y++;
	}

}

bool upper(int a, int b) {
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}

void DFS(int v, int p) {
	tin[v] = ++timer;
	up[v][0] = p;
	h[v] = h[p] + 1;
	sort(ALL(t[v][0]));
	t[v][0].resize(min(SZ(t[v][0]),10);
	REP(i, 1, L) {
		up[v][i] = up[up[v][i - 1]][i - 1];
		vmerge(t[v][i - 1], t[up[v][i - 1]][i - 1], t[v][i]);
	}
	for (auto x : g[v]) {
		if (x != p) DFS(x, v);
	}
	tout[v] = ++timer;
}

int LCA(int a, int b) {
	if (upper(a, b)) return a;
	if (upper(b, a)) return b;
	PER(i, 0, L) {
		if (!upper(up[a][i], b)) a = up[a][i];
	}
	return up[a][0];
}
void under(int a, int b, VI &ret) {

	PER(i, 0, L){
		if (h[up[a][i]] >= h[b] && a != up[a][i]) {
			ret = merge(ret, t[a][i]);
			a = up[a][i];
		}
	}
}
void print(VI &v) {
	cout << "P:" << endl;
	for (auto x : v) cout << x << ' '; cout << endl;
}
void solve()
{
	int n, m, q;
	cin >> n >> m >> q;
	REP(i, 2, n + 1) {
		int a, b;
		scanf("%d%d", &a, &b);
		g[a].push_back(b);
		g[b].push_back(a);
	}
	L = (int)log2(n) + 1;

	REP(i, 1, m + 1) {
		scanf("%d", c + i);
		t[c[i]][0].push_back(i);
	}
	DFS(1, 1);
	REP(i, 0, q) {
		int u, v, x;
		scanf("%d%d%d", &u, &v, &x);
		int lca = LCA(u, v);
		VI a = t[lca][0];
		under(u, lca, a);
		under(v, lca, a);

		printf("%d ", min(x, SZ(a)));
		REP(i, 0, min(x, SZ(a))) printf("%d ", a[i]); printf("\n");
	}

}

int main()
{
	//freopen("codeforces.in", "r", stdin);
	//freopen("codeforces.out", "w", stdout);
	int t = 1;
	//cin>>t;

	while (t--) {
		solve();
	};

	getchar();
	getchar();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <math.h>
#include <iterator>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <string.h>
#include <cstring>
#include <queue>
#include <iomanip>
#include <istream>
#include <map>
#include <list>
#include <stack>
#include <numeric>
#include <ostream>
#include <set>
#include <cassert>
#include <sstream>
#include <string>
#include <utility>
#include <stdio.h>
#include <vector>
using namespace std;
#define dout if (debug) cout
#define PB push_back
#define MP make_pair
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int((x.size())))
typedef long double ld;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<PII> VPI;
#define REP(i,a,n) for (int i=a;i<n;i++)
#define db(x) cerr << #x << " = " << x << endl
#define PER(i,a,n) for (int i=n-1;i>=a;i--)
const int INF = 1000000404;
const ll MOD = 1000000007ll;
const ld PI = acos(-1.0);
const ld EPS = 1e-9;
template <typename t1, typename t2> inline void upmax(t1 &a, t2 b) { a = max(a, (t1)b); }
template <typename t1, typename t2> inline void upmin(t1 &a, t2 b) { a = min(a, (t1)b); }
template <typename T>inline T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template <typename T>inline T lcm(T a, T b) { return a*(b / gcd(a, b)); }
template <typename T>inline T sqr(T a) { return a*a; }
template <typename T>inline bool pal(T &x)
{
	int n = SZ(x); REP(i, 0, n / 2) { if (x[i] != x[n - i - 1]) return 0; }return 1;
}
template <typename T>inline void rev(T &x) { int n = SZ(x); REP(i, 0, n / 2) swap(x[i], x[n - i - 1]); }
int month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
int dy[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
inline ll mp(ll a, ll b) { return (a << 31) + b; }
class compare{
public:
	bool operator()(const int a, const int b) const {
		return 1;
	}
};
int SQ = 400;

#define debug 1
#define N 111111
int tin[N];
int tout[N];
int up[N][17];
int h[N];
int ar[N];
VI g[N];
VI t[N][17];
int c[N];
int timer, L;
void merge(VI &a, VI &b, VI &c) {
	if (a == b) {c = a; return;}
	auto x = a.begin();
	auto y = b.begin();

	while ((x != a.end() || y != b.end())&&SZ(c)<10) {
		if (x == a.end()) {
			while (y != b.end()) {
				c.push_back(*y); y++;
			}
			continue;
		}
		if (y == b.end()) {
			while (x != a.end()) {
				c.push_back(*x); x++;
			}
			continue;
		}
		if (*x < *y) {
			c.push_back(*x); x++;
		}
		else {
			if (*x == *y) x++;
			c.push_back(*y);
			y++;
		}
	}
}

bool upper(int a, int b) {
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}

void DFS(int v, int p) {
	tin[v] = ++timer;
	up[v][0] = p;
	h[v] = h[p] +1;
	sort(ALL(t[v][0]));
	REP(i, 1, L) {
		up[v][i] = up[up[v][i - 1]][i - 1];
		merge(t[v][i - 1], t[up[v][i - 1]][i - 1],t[v][i]);
	}
	for (auto x : g[v]) {
		if (x != p) DFS(x, v);
	}
	tout[v] = ++timer;
}

int LCA(int a, int b) {
	if (upper(a, b)) return a;
	if (upper(b, a)) return b;
	PER(i, 0, L) {
		if (!upper(up[a][i], b)) a = up[a][i];
	}
	return up[a][0];
}
VI under(int a, int b) {
	VI ret;
	PER(i, 0, L){
		if (h[up[a][i]] >= h[b]&&a!=up[a][i]) {
			VI v=ret;
			merge(v, t[a][i],ret);
			a = up[a][i];
		}
	}
	return ret;
}
void print(VI &v) {
	cout << "P:" << endl;
	for (auto x : v) cout << x << ' '; cout << endl;
}
void solve()
{
	int n, m, q;
	cin >> n >> m >> q;
	REP(i, 2, n + 1) {
		int a, b;
		scanf("%d%d", &a, &b);
		g[a].push_back(b);
		g[b].push_back(a);
	}
	L = (int)log2(n) + 1;

	REP(i, 1, m + 1) {
		scanf("%d", c + i);
		t[c[i]][0].push_back(i);
	}
	DFS(1, 1);
	REP(i, 0, q) {
		int u, v, x;
		scanf("%d%d%d", &u, &v, &x);
		int lca = LCA(u, v);
		VI a, b,c,d;
//		db(lca);
		a = under(u, lca);
		b = under(v, lca);
		merge(a, b, c);
		merge(c, t[lca][0], d);
		printf("%d ", min(x, SZ(d)));
		REP(i, 0, min(x, SZ(d))) printf("%d ", d[i]); printf("\n");
	}

}

int main()
{
	//freopen("codeforces.in", "r", stdin);
	//freopen("codeforces.out", "w", stdout);
	int t = 1;
	//cin>>t;

	while (t--) {
		solve();
	};

	getchar();
	getchar();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <math.h>
#include <iterator>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <string.h>
#include <cstring>
#include <queue>
#include <iomanip>
#include <istream>
#include <map>
#include <list>
#include <stack>
#include <numeric>
#include <ostream>
#include <set>
#include <cassert>
#include <sstream>
#include <string>
#include <utility>
#include <stdio.h>
#include <vector>
using namespace std;
#define dout if (debug) cout
#define PB push_back
#define MP make_pair
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int((x.size())))
typedef long double ld;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<PII> VPI;
#define REP(i,a,n) for (int i=a;i<n;i++)
#define db(x) cerr << #x << " = " << x << endl
#define PER(i,a,n) for (int i=n-1;i>=a;i--)
const int INF = 1000000404;
const ll MOD = 1000000007ll;
const ld PI = acos(-1.0);
const ld EPS = 1e-9;
template <typename t1, typename t2> inline void upmax(t1 &a, t2 b) { a = max(a, (t1)b); }
template <typename t1, typename t2> inline void upmin(t1 &a, t2 b) { a = min(a, (t1)b); }
template <typename T>inline T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template <typename T>inline T lcm(T a, T b) { return a*(b / gcd(a, b)); }
template <typename T>inline T sqr(T a) { return a*a; }
template <typename T>inline bool pal(T &x)
{
	int n = SZ(x); REP(i, 0, n / 2) { if (x[i] != x[n - i - 1]) return 0; }return 1;
}
template <typename T>inline void rev(T &x) { int n = SZ(x); REP(i, 0, n / 2) swap(x[i], x[n - i - 1]); }
int month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
int dy[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
inline ll mp(ll a, ll b) { return (a << 31) + b; }
class compare{
public:
	bool operator()(const int a, const int b) const {
		return 1;
	}
};
int SQ = 400;

#define debug 1
#define N 111111
int tin[N];
int tout[N];
int up[N][17];
int h[N];
int ar[N];
VI g[N];
VI t[N][17];
int c[N];
int timer, L;
VI merge(VI &a, VI &b) {
	VI c;
	auto x = a.begin();
	auto y = b.begin();

	while ((x != a.end() || y != b.end())&&SZ(c)<10) {
		if (x == a.end()) {
			while (y != b.end()) {
				c.push_back(*y); y++;
			}
			continue;
		}
		if (y == b.end()) {
			while (x != a.end()) {
				c.push_back(*x); x++;
			}
			continue;
		}
		if (*x < *y) {
			c.push_back(*x); x++;
		}
		else {
			if (*x == *y) x++;
			c.push_back(*y);
			y++;
		}
	}
	return c;
}

bool upper(int a, int b) {
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}

void DFS(int v, int p) {
	tin[v] = ++timer;
	up[v][0] = p;
	h[v] = h[p] +1;
	sort(ALL(t[v][0]));
	REP(i, 1, L) {
		up[v][i] = up[up[v][i - 1]][i - 1];
		t[v][i] = merge(t[v][i - 1], t[up[v][i - 1]][i - 1]);
	}
	for (auto x : g[v]) {
		if (x != p) DFS(x, v);
	}
	tout[v] = ++timer;
}

int LCA(int a, int b) {
	if (upper(a, b)) return a;
	if (upper(b, a)) return b;
	PER(i, 0, L) {
		if (!upper(up[a][i], b)) a = up[a][i];
	}
	return up[a][0];
}
VI under(int a, int b) {
	VI ret;
	PER(i, 0, L){
		if (h[up[a][i]] >= h[b]&&a!=up[a][i]) {
			ret = merge(ret, t[a][i]);
			a = up[a][i];
		}
	}
	return ret;
}
void print(VI &v) {
	cout << "P:" << endl;
	for (auto x : v) cout << x << ' '; cout << endl;
}
void solve()
{
	int n, m, q;
	cin >> n >> m >> q;
	REP(i, 2, n + 1) {
		int a, b;
		scanf("%d%d", &a, &b);
		g[a].push_back(b);
		g[b].push_back(a);
	}
	L = (int)log2(n) + 1;

	REP(i, 1, m + 1) {
		scanf("%d", c + i);
		t[c[i]][0].push_back(i);
	}
	DFS(1, 1);
	REP(i, 0, q) {
		int u, v, x;
		scanf("%d%d%d", &u, &v, &x);
		int lca = LCA(u, v);
		VI a, b;
//		db(lca);
		a = under(u, lca);
		b = under(v, lca);

		a = merge(a, b);
		a = merge(a, t[lca][0]);
		printf("%d ", min(x, SZ(a)));
		REP(i, 0, min(x, SZ(a))) printf("%d ", a[i]); printf("\n");
	}

}

int main()
{
	//freopen("codeforces.in", "r", stdin);
	//freopen("codeforces.out", "w", stdout);
	int t = 1;
	//cin>>t;

	while (t--) {
		solve();
	};

	getchar();
	getchar();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <math.h>
#include <iterator>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <string.h>
#include <cstring>
#include <queue>
#include <iomanip>
#include <istream>
#include <map>
#include <list>
#include <stack>
#include <numeric>
#include <ostream>
#include <set>
#include <cassert>
#include <sstream>
#include <string>
#include <utility>
#include <stdio.h>
#include <vector>
using namespace std;
#define dout if (debug) cout
#define PB push_back
#define MP make_pair
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int((x.size())))
typedef long double ld;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<PII> VPI;
#define REP(i,a,n) for (int i=a;i<n;i++)
#define db(x) cerr << #x << " = " << x << endl
#define PER(i,a,n) for (int i=n-1;i>=a;i--)
const int INF = 1000000404;
const ll MOD = 1000000007ll;
const ld PI = acos(-1.0);
const ld EPS = 1e-9;
template <typename t1, typename t2> inline void upmax(t1 &a, t2 b) { a = max(a, (t1)b); }
template <typename t1, typename t2> inline void upmin(t1 &a, t2 b) { a = min(a, (t1)b); }
template <typename T>inline T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template <typename T>inline T lcm(T a, T b) { return a*(b / gcd(a, b)); }
template <typename T>inline T sqr(T a) { return a*a; }
template <typename T>inline bool pal(T &x)
{
	int n = SZ(x); REP(i, 0, n / 2) { if (x[i] != x[n - i - 1]) return 0; }return 1;
}
template <typename T>inline void rev(T &x) { int n = SZ(x); REP(i, 0, n / 2) swap(x[i], x[n - i - 1]); }
int month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
int dy[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
inline ll mp(ll a, ll b) { return (a << 31) + b; }
class compare{
public:
	bool operator()(const int a, const int b) const {
		return 1;
	}
};
int SQ = 400;

#define debug 1
#define N 111111
int tin[N];
int tout[N];
int up[N][17];
int h[N];
int ar[N];
VI g[N];
VI t[N][17];
int c[N];
int timer, L;
VI merge(VI &a, VI &b) {
	VI c;
	auto x = a.begin();
	auto y = b.begin();

	while ((x != a.end() && y != b.end()) && SZ(c) < 10) {
		if (*x < *y) {
			c.push_back(*x); x++;
		}
		else {
			if (*x == *y) x++;
			c.push_back(*y);
			y++;
		}
	}
	while (x != a.end() && SZ(c)<10) {
		c.push_back(*x); x++;
	}
	while (y != b.end() && SZ(c)<10) {
		c.push_back(*y); y++;
	}
	return c;
}
void vmerge(VI &a, VI &b, VI &c) {

	auto x = a.begin();
	auto y = b.begin();

	while ((x != a.end() && y != b.end()) && SZ(c) < 10) {
		if (*x < *y) {
			c.push_back(*x); x++;
		}
		else {
			if (*x == *y) x++;
			c.push_back(*y);
			y++;
		}
	}
	while (x != a.end() && SZ(c)<10) {
		c.push_back(*x); x++;
	}
	while (y != b.end()&&SZ(c)<10) {
		c.push_back(*y); y++;
	}

}

bool upper(int a, int b) {
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}

void DFS(int v, int p) {
	tin[v] = ++timer;
	up[v][0] = p;
	h[v] = h[p] + 1;
	sort(ALL(t[v][0]));
	REP(i, 1, L) {
		up[v][i] = up[up[v][i - 1]][i - 1];
		vmerge(t[v][i - 1], t[up[v][i - 1]][i - 1], t[v][i]);
	}
	for (auto x : g[v]) {
		if (x != p) DFS(x, v);
	}
	tout[v] = ++timer;
}

int LCA(int a, int b) {
	if (upper(a, b)) return a;
	if (upper(b, a)) return b;
	PER(i, 0, L) {
		if (!upper(up[a][i], b)) a = up[a][i];
	}
	return up[a][0];
}
void under(int a, int b, VI &ret) {

	PER(i, 0, L){
		if (h[up[a][i]] >= h[b] && a != up[a][i]) {
			ret = merge(ret, t[a][i]);
			a = up[a][i];
		}
	}
}
void print(VI &v) {
	cout << "P:" << endl;
	for (auto x : v) cout << x << ' '; cout << endl;
}
void solve()
{
	int n, m, q;
	cin >> n >> m >> q;
	REP(i, 2, n + 1) {
		int a, b;
		scanf("%d%d", &a, &b);
		g[a].push_back(b);
		g[b].push_back(a);
	}
	L = (int)log2(n) + 1;

	REP(i, 1, m + 1) {
		scanf("%d", c + i);
		t[c[i]][0].push_back(i);
	}
	DFS(1, 1);
	REP(i, 0, q) {
		int u, v, x;
		scanf("%d%d%d", &u, &v, &x);
		int lca = LCA(u, v);
		VI a = t[lca][0];
		under(u, lca, a);
		under(v, lca, a);

		printf("%d ", min(x, SZ(a)));
		REP(i, 0, min(x, SZ(a))) printf("%d ", a[i]); printf("\n");
	}

}

int main()
{
	//freopen("codeforces.in", "r", stdin);
	//freopen("codeforces.out", "w", stdout);
	int t = 1;
	//cin>>t;

	while (t--) {
		solve();
	};

	getchar();
	getchar();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <math.h>
#include <iterator>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <string.h>
#include <cstring>
#include <queue>
#include <iomanip>
#include <istream>
#include <map>
#include <list>
#include <stack>
#include <numeric>
#include <ostream>
#include <set>
#include <cassert>
#include <sstream>
#include <string>
#include <utility>
#include <stdio.h>
#include <vector>
using namespace std;
#define dout if (debug) cout
#define PB push_back
#define MP make_pair
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int((x.size())))
typedef long double ld;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<PII> VPI;
#define REP(i,a,n) for (int i=a;i<n;i++)
#define db(x) cerr << #x << " = " << x << endl
#define PER(i,a,n) for (int i=n-1;i>=a;i--)
const int INF = 1000000404;
const ll MOD = 1000000007ll;
const ld PI = acos(-1.0);
const ld EPS = 1e-9;
template <typename t1, typename t2> inline void upmax(t1 &a, t2 b) { a = max(a, (t1)b); }
template <typename t1, typename t2> inline void upmin(t1 &a, t2 b) { a = min(a, (t1)b); }
template <typename T>inline T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template <typename T>inline T lcm(T a, T b) { return a*(b / gcd(a, b)); }
template <typename T>inline T sqr(T a) { return a*a; }
template <typename T>inline bool pal(T &x)
{
	int n = SZ(x); REP(i, 0, n / 2) { if (x[i] != x[n - i - 1]) return 0; }return 1;
}
template <typename T>inline void rev(T &x) { int n = SZ(x); REP(i, 0, n / 2) swap(x[i], x[n - i - 1]); }
int month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
int dy[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
inline ll mp(ll a, ll b) { return (a << 31) + b; }
class compare{
public:
	bool operator()(const int a, const int b) const {
		return 1;
	}
};
int SQ = 400;

#define debug 1
#define N 111111
int tin[N];
int tout[N];
int up[N][20];
int h[N];
int ar[N];
VI g[N];
VI t[N][20];
int c[N];
int timer, L;
VI merge(VI &a, VI &b) {
	VI c;
	auto x = a.begin();
	auto y = b.begin();

	while ((x != a.end() && y != b.end()) && SZ(c) < 10) {
		if (*x < *y) {
			c.push_back(*x); x++;
		}
		else {
			if (*x == *y) x++;
			c.push_back(*y);
			y++;
		}
	}
	while (x != a.end() && SZ(c)<10) {
		c.push_back(*x); x++;
	}
	while (y != b.end() && SZ(c)<10) {
		c.push_back(*y); y++;
	}
	return c;
}
void vmerge(VI &a, VI &b, VI &c) {

	auto x = a.begin();
	auto y = b.begin();

	while ((x != a.end() && y != b.end()) && SZ(c) < 10) {
		if (*x < *y) {
			c.push_back(*x); x++;
		}
		else {
			if (*x == *y) x++;
			c.push_back(*y);
			y++;
		}
	}
	while (x != a.end() && SZ(c)<10) {
		c.push_back(*x); x++;
	}
	while (y != b.end() && SZ(c)<10) {
		c.push_back(*y); y++;
	}

}

bool upper(int a, int b) {
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}

void DFS(int v, int p) {
	tin[v] = ++timer;
	up[v][0] = p;
	h[v] = h[p] + 1;
	sort(ALL(t[v][0]));
	t[v][0].resize(0);
	REP(i, 1, L) {
		up[v][i] = up[up[v][i - 1]][i - 1];
		vmerge(t[v][i - 1], t[up[v][i - 1]][i - 1], t[v][i]);
	}
	for (auto x : g[v]) {
		if (x != p) DFS(x, v);
	}
	tout[v] = ++timer;
}

int LCA(int a, int b) {
	if (upper(a, b)) return a;
	if (upper(b, a)) return b;
	PER(i, 0, L) {
		if (!upper(up[a][i], b)) a = up[a][i];
	}
	return up[a][0];
}
void under(int a, int b, VI &ret) {

	PER(i, 0, L){
		if (h[up[a][i]] >= h[b] && a != up[a][i]) {
			ret = merge(ret, t[a][i]);
			a = up[a][i];
		}
	}
}
void print(VI &v) {
	cout << "P:" << endl;
	for (auto x : v) cout << x << ' '; cout << endl;
}
void solve()
{
	int n, m, q;
	cin >> n >> m >> q;
	REP(i, 2, n + 1) {
		int a, b;
		scanf("%d%d", &a, &b);
		g[a].push_back(b);
		g[b].push_back(a);
	}
	L = (int)log2(n) + 1;

	REP(i, 1, m + 1) {
		scanf("%d", c + i);
		t[c[i]][0].push_back(i);
	}
	DFS(1, 1);
	REP(i, 0, q) {
		int u, v, x;
		scanf("%d%d%d", &u, &v, &x);
		int lca = LCA(u, v);
		VI a = t[lca][0];
		under(u, lca, a);
		under(v, lca, a);

		printf("%d ", min(x, SZ(a)));
		REP(i, 0, min(x, SZ(a))) printf("%d ", a[i]); printf("\n");
	}

}

int main()
{
	//freopen("codeforces.in", "r", stdin);
	//freopen("codeforces.out", "w", stdout);
	int t = 1;
	//cin>>t;

	while (t--) {
		solve();
	};

	getchar();
	getchar();
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <math.h>
#include <iterator>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <string.h>
#include <cstring>
#include <queue>
#include <iomanip>
#include <istream>
#include <map>
#include <list>
#include <stack>
#include <numeric>
#include <ostream>
#include <set>
#include <cassert>
#include <sstream>
#include <string>
#include <utility>
#include <stdio.h>
#include <vector>
using namespace std;
#define dout if (debug) cout
#define PB push_back
#define MP make_pair
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int((x.size())))
typedef long double ld;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<PII> VPI;
#define REP(i,a,n) for (int i=a;i<n;i++)
#define db(x) cerr << #x << " = " << x << endl
#define PER(i,a,n) for (int i=n-1;i>=a;i--)
const int INF = 1000000404;
const ll MOD = 1000000007ll;
const ld PI = acos(-1.0);
const ld EPS = 1e-9;
template <typename t1, typename t2> inline void upmax(t1 &a, t2 b) { a = max(a, (t1)b); }
template <typename t1, typename t2> inline void upmin(t1 &a, t2 b) { a = min(a, (t1)b); }
template <typename T>inline T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template <typename T>inline T lcm(T a, T b) { return a*(b / gcd(a, b)); }
template <typename T>inline T sqr(T a) { return a*a; }
template <typename T>inline bool pal(T &x)
{
	int n = SZ(x); REP(i, 0, n / 2) { if (x[i] != x[n - i - 1]) return 0; }return 1;
}
template <typename T>inline void rev(T &x) { int n = SZ(x); REP(i, 0, n / 2) swap(x[i], x[n - i - 1]); }
int month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
int dy[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
inline ll mp(ll a, ll b) { return (a << 31) + b; }
class compare{
public:
	bool operator()(const int a, const int b) const {
		return 1;
	}
};
int SQ = 400;

#define debug 1
#define N 111111
int tin[N];
int tout[N];
int up[N][17];
int h[N];
int ar[N];
VI g[N];
VI t[N][17];
int c[N];
int timer, L;
VI merge(VI a, VI b) {
	VI c;
	auto x = a.begin();
	auto y = b.begin();

	while (x != a.end() || y != b.end()) {
		if (x == a.end()) {
			while (y != b.end()) {
				c.push_back(*y); y++;
			}
			continue;
		}
		if (y == b.end()) {
			while (x != a.end()) {
				c.push_back(*x); x++;
			}
			continue;
		}
		if (*x < *y) {
			c.push_back(*x); x++;
		}
		else {
			if (*x == *y) x++;
			c.push_back(*y);
			y++;
		}
	}

	c.resize(min(SZ(c), 10));
	return c;
}

bool upper(int a, int b) {
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}

void DFS(int v, int p) {
	tin[v] = ++timer;
	up[v][0] = p;
	h[v] = h[p] +1;
	sort(ALL(t[v][0]));
	REP(i, 1, L) {
		up[v][i] = up[up[v][i - 1]][i - 1];
		t[v][i] = merge(t[v][i - 1], t[up[v][i - 1]][i - 1]);
	}
	for (auto x : g[v]) {
		if (x != p) DFS(x, v);
	}
	tout[v] = ++timer;
}

int LCA(int a, int b) {
	if (upper(a, b)) return a;
	if (upper(b, a)) return b;
	PER(i, 0, L) {
		if (!upper(up[a][i], b)) a = up[a][i];
	}
	return up[a][0];
}
VI under(int a, int b) {
	VI ret;
	PER(i, 0, L){
		if (h[up[a][i]] >= h[b]&&a!=up[a][i]) {
			ret = merge(ret, t[a][i]);
			a = up[a][i];
		}
	}
	return ret;
}
void print(VI &v) {
	cout << "P:" << endl;
	for (auto x : v) cout << x << ' '; cout << endl;
}
void solve()
{
	int n, m, q;
	cin >> n >> m >> q;
	REP(i, 2, n + 1) {
		int a, b;
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	L = (int)log2(n) + 1;

	REP(i, 1, m + 1) {
		cin >> c[i];
		t[c[i]][0].push_back(i);
	}
	DFS(1, 1);
	REP(i, 0, q) {
		int u, v, x;
		cin >> u >> v >> x;
		int lca = LCA(u, v);
		VI a, b;
//		db(lca);
		a = under(u, lca);
		b = under(v, lca);

		a = merge(a, b);
		a = merge(a, t[lca][0]);
		cout << min(x, SZ(a)) << ' '; REP(i, 0, min(x, SZ(a))) cout << a[i] << ' '; cout << endl;
	}

}

int main()
{
	//freopen("codeforces.in", "r", stdin);
	//freopen("codeforces.out", "w", stdout);
	ios_base::sync_with_stdio(false);	cin.tie(0);	cout.tie(0);
	int t = 1;
	//cin>>t;

	while (t--) {
		solve();
	};

	getchar();
	getchar();
	return 0;
}