#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <iostream>

// #define int long long

#define For(i,a,b) for (int i = a; i <= (b); ++i)
#define For2(i,a,b) for (int i = a; i >= (b); --i)

#define test int _t; cin >> _t; while (_t--)

#define pii pair<int, int>
#define mpii map<int, int>
#define all(a) a.begin(), a.end()
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
#define sz(a) (int)a.size()

#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define INF (int)1e18
#define EPS 1e-9

using namespace std;
 //#define FILEOPEN

const int MAXN = 200005;
unsigned char a[MAXN];

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	#ifdef FILEOPEN
		freopen("input.in", "r", stdin);
		freopen("input.out", "w", stdout);
	#endif
	int n;
	scanf("%d", &n);
	For(i,1,n) scanf("%hhu", &a[i]);
	int q;
	scanf("%d", &q);
	while (q--) {
		int l, r;
		unsigned char x, y;
		scanf("%d %d %hhu %hhu", &l, &r, &x, &y);
// 		printf("%d %d %hhu %hhu", l, r, x, y);
		For(i,l,r) a[i] = (a[i] == x) ? y : a[i];
	}
	For(i,1,n) printf("%hhu ", a[i]);
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

// #define int long long

#define For(i,a,b) for (int i = a; i <= (b); ++i)
#define For2(i,a,b) for (int i = a; i >= (b); --i)

#define test int _t; cin >> _t; while (_t--)

#define pii pair<int, int>
#define mpii map<int, int>
#define all(a) a.begin(), a.end()
#define pb emplace_back
#define fi first
#define se second
#define mp make_pair
#define sz(a) (int)a.size()

#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define INF (int)1e18
#define EPS 1e-9

using namespace std;
using namespace __gnu_pbds;

template <class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//#define FILEOPEN

const int MAXN = 200005;
int a[MAXN];

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	#ifdef FILEOPEN
		freopen("input.in", "r", stdin);
		freopen("input.out", "w", stdout);
	#endif
	int n;
	cin >> n;
	For(i,1,n) cin >> a[i];
	int q;
	cin >> q;
	while (q--) {
		int l, r;
		short x, y;
		#pragma GCC ivdep
		cin >> l >> r >> x >> y;
		For(i,l - 1,r) a[i] = (a[i] == x) ? y : a[i];
	}
	For(i,1,n) cout << a[i] << " ";
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define p(n) printf("%d\n",n)
#define fi first
#define sec second
#define mp make_pair
#define pb push_back
#define INF 1LL*1e18
int a[1000005];
list<int>sq[451][101];
void apply(int bl,int id,int flag,int x,int y,int id2 = 0)
{
	//cout << id << " " << id2 << endl;
		for(auto it = sq[bl][x].begin();it != sq[bl][x].end();){
			//cout << *it << endl;
			if((flag == 1 && *it >= id) || (flag == 0 && *it <= id) || (flag == 2 && *it >= id && *it <= id2)){
				sq[bl][y].pb(*it);
				it = sq[bl][x].erase(it);
			}
			else{
				it++;
			}
		}
}
int main()
{
	int i,j,k,l,m,n,t,r,cnt;
	int flag = 0;
	ll ans = 0,an = 0;
	t = 1;
	while(t--){
		s(n);
		int fa = 450;
		for(i = 1;i <= n;i++){
			s(a[i]);
			sq[i/fa][a[i]].pb(i);
		}
		int q,x,y;
		s(q);
		//cout << q << endl;
		while(q--){
			s(l);s(r);s(x);s(y);
		//	cout << l << " " << r << " " << x << " " << y << endl;
			if(l/fa == r/fa){
				apply(l/fa,l,2,x,y,r);
			}
			else{
				apply(l/fa,l,1,x,y);
				apply(r/fa,r,0,x,y);
				for(i = l/fa +1;i < r/fa;i++){
					sq[i][y].splice(sq[i][y].begin(),sq[i][x]);
				}
			}
		}
		for(i = 0;i < 450;i++){
			for(j = 0;j < 101;j++){
				for(int it : sq[i][j]){
					a[it] = j;
				}
			}
		}
		for(i = 1;i <= n;i++){
			cout << a[i] << " ";
		}
		cout << endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define p(n) printf("%d ",n)
#define fi first
#define sec second
#define mp make_pair
#define pb push_back
#define INF 1LL*1e18
int a[1000005];
list<int>sq[511][101];
void apply(int bl,int id,int flag,int x,int y,int id2 = 0)
{
	//cout << id << " " << id2 << endl;
		for(auto it = sq[bl][x].begin();it != sq[bl][x].end();){
			//cout << *it << endl;
			if((flag == 1 && *it >= id) || (flag == 0 && *it <= id) || (flag == 2 && *it >= id && *it <= id2)){
				auto it2 = ++it;
				--it;
				sq[bl][y].splice(sq[bl][y].begin(),sq[bl][x],it);
				it = it2;
			}
			else{
				it++;
			}
		}
}
int main()
{
	int i,j,k,l,m,n,t,r,cnt;
	int flag = 0;
	ll ans = 0,an = 0;
	t = 1;
	while(t--){
		s(n);
		int fa = 400;
		for(i = 1;i <= n;i++){
			s(a[i]);
			sq[i/fa][a[i]].pb(i);
		}
		int q,x,y;
		s(q);
		//cout << q << endl;
		while(q--){
			s(l);s(r);s(x);s(y);
			if(x == y) continue;
		//	cout << l << " " << r << " " << x << " " << y << endl;
			if(l/fa == r/fa){
				apply(l/fa,l,2,x,y,r);
			}

			else{
				apply(l/fa,l,1,x,y);
				apply(r/fa,r,0,x,y);
				for(i = l/fa +1;i < r/fa;i++){
					sq[i][y].splice(sq[i][y].begin(),sq[i][x]);
				}
			}
		}
		for(i = 0;i < 450;i++){
			for(j = 0;j < 101;j++){
				for(int it : sq[i][j]){
					a[it] = j;
				}
			}
		}
		for(i = 1;i <= n;i++){
			p(a[i]);
		}
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define p(n) printf("%d ",n)
#define fi first
#define sec second
#define mp make_pair
#define pb push_back
#define INF 1LL*1e18
int a[1000005];
list<int>sq[505][101];
void apply(int bl,int id,int flag,int x,int y,int id2 = 0)
{
	//cout << id << " " << id2 << endl;
		for(auto it = sq[bl][x].begin();it != sq[bl][x].end();){
			//cout << *it << endl;
			if((flag == 1 && *it >= id) || (flag == 0 && *it <= id) || (flag == 2 && *it >= id && *it <= id2)){
				auto it2 = ++it;
				--it;
				sq[bl][y].splice(sq[bl][y].begin(),sq[bl][x],it);
				it = it2;
			}
			else{
				it++;
			}
		}
}
int main()
{
	int i,j,k,l,m,n,t,r,cnt;
	int flag = 0;
	ll ans = 0,an = 0;
	t = 1;
	while(t--){
		s(n);
		int fa = 401;
		for(i = 1;i <= n;i++){
			s(a[i]);
			sq[i/fa][a[i]].pb(i);
		}
		int q,x,y;
		s(q);
		//cout << q << endl;
		while(q--){
			s(l);s(r);s(x);s(y);
			if(x == y) continue;
		//	cout << l << " " << r << " " << x << " " << y << endl;
			if(l/fa == r/fa){
				apply(l/fa,l,2,x,y,r);
			}

			else{
				apply(l/fa,l,1,x,y);
				apply(r/fa,r,0,x,y);
				for(i = l/fa +1;i < r/fa;i++){
					sq[i][y].splice(sq[i][y].begin(),sq[i][x]);
				}
			}
		}
		for(i = 0;i < 505;i++){
			for(j = 0;j < 101;j++){
				for(int it : sq[i][j]){
					a[it] = j;
				}
			}
		}
		for(i = 1;i <= n;i++){
			p(a[i]);
		}
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define p(n) printf("%d ",n)
#define fi first
#define sec second
#define mp make_pair
#define pb push_back
#define INF 1LL*1e18
int a[1000005];
list<int>sq[451][101];
void apply(int bl,int id,int flag,int x,int y,int id2 = 0)
{
	//cout << id << " " << id2 << endl;
		for(auto it = sq[bl][x].begin();it != sq[bl][x].end();){
			//cout << *it << endl;
			if((flag == 1 && *it >= id) || (flag == 0 && *it <= id) || (flag == 2 && *it >= id && *it <= id2)){
				auto it2 = ++it;
				--it;
				sq[bl][y].splice(sq[bl][y].begin(),sq[bl][x],it);
				it = it2;
			}
			else{
				it++;
			}
		}
}
int main()
{
	int i,j,k,l,m,n,t,r,cnt;
	int flag = 0;
	ll ans = 0,an = 0;
	t = 1;
	while(t--){
		s(n);
		int fa = 450;
		for(i = 1;i <= n;i++){
			s(a[i]);
			sq[i/fa][a[i]].pb(i);
		}
		int q,x,y;
		s(q);
		//cout << q << endl;
		while(q--){
			s(l);s(r);s(x);s(y);
		//	cout << l << " " << r << " " << x << " " << y << endl;
			if(l/fa == r/fa){
				apply(l/fa,l,2,x,y,r);
			}
			else{
				apply(l/fa,l,1,x,y);
				apply(r/fa,r,0,x,y);
				for(i = l/fa +1;i < r/fa;i++){
					sq[i][y].splice(sq[i][y].begin(),sq[i][x]);
				}
			}
		}
		for(i = 0;i < 450;i++){
			for(j = 0;j < 101;j++){
				for(int it : sq[i][j]){
					a[it] = j;
				}
			}
		}
		for(i = 1;i <= n;i++){
			p(a[i]);
		}
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define p(n) printf("%d ",n)
#define fi first
#define sec second
#define mp make_pair
#define pb push_back
#define INF 1LL*1e18
int a[1000005];
list<int>sq[601][101];
void apply(int bl,int id,int flag,int x,int y,int id2 = 0)
{
	//cout << id << " " << id2 << endl;
		for(auto it = sq[bl][x].begin();it != sq[bl][x].end();){
			//cout << *it << endl;
			if((flag == 1 && *it >= id) || (flag == 0 && *it <= id) || (flag == 2 && *it >= id && *it <= id2)){
				auto it2 = ++it;
				--it;
				sq[bl][y].splice(sq[bl][y].begin(),sq[bl][x],it);
				it = it2;
			}
			else{
				it++;
			}
		}
}
int main()
{
	int i,j,k,l,m,n,t,r,cnt;
	int flag = 0;
	ll ans = 0,an = 0;
	t = 1;
	while(t--){
		s(n);
		int fa = 401;
		for(i = 1;i <= n;i++){
			s(a[i]);
			sq[i/fa][a[i]].pb(i);
		}
		int q,x,y;
		s(q);
		//cout << q << endl;
		while(q--){
			s(l);s(r);s(x);s(y);
			if(x == y) continue;
		//	cout << l << " " << r << " " << x << " " << y << endl;
			if(l/fa == r/fa){
				apply(l/fa,l,2,x,y,r);
			}

			else{
				apply(l/fa,l,1,x,y);
				apply(r/fa,r,0,x,y);
				for(i = l/fa +1;i < r/fa;i++){
					sq[i][y].splice(sq[i][y].begin(),sq[i][x]);
				}
			}
		}
		for(i = 0;i < 450;i++){
			for(j = 0;j < 101;j++){
				for(int it : sq[i][j]){
					a[it] = j;
				}
			}
		}
		for(i = 1;i <= n;i++){
			p(a[i]);
		}
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define p(n) printf("%d ",n)
#define fi first
#define sec second
#define mp make_pair
#define pb push_back
#define INF 1LL*1e18
int a[1000005];
list<int>sq[451][101];
void apply(int bl,int id,int flag,int x,int y,int id2 = 0)
{
	//cout << id << " " << id2 << endl;
		for(auto it = sq[bl][x].begin();it != sq[bl][x].end();){
			//cout << *it << endl;
			if((flag == 1 && *it >= id) || (flag == 0 && *it <= id) || (flag == 2 && *it >= id && *it <= id2)){
				auto it2 = ++it;
				--it;
				sq[bl][y].splice(sq[bl][y].begin(),sq[bl][x],it);
				it = it2;
			}
			else{
				it++;
			}
		}
}
int main()
{
	int i,j,k,l,m,n,t,r,cnt;
	int flag = 0;
	ll ans = 0,an = 0;
	t = 1;
	while(t--){
		s(n);
		int fa = 450;
		for(i = 1;i <= n;i++){
			s(a[i]);
			sq[i/fa][a[i]].pb(i);
		}
		int q,x,y;
		s(q);
		//cout << q << endl;
		while(q--){
			s(l);s(r);s(x);s(y);
			if(x == y) continue;
		//	cout << l << " " << r << " " << x << " " << y << endl;
			if(l/fa == r/fa){
				apply(l/fa,l,2,x,y,r);
			}

			else{
				apply(l/fa,l,1,x,y);
				apply(r/fa,r,0,x,y);
				for(i = l/fa +1;i < r/fa;i++){
					sq[i][y].splice(sq[i][y].begin(),sq[i][x]);
				}
			}
		}
		for(i = 0;i < 450;i++){
			for(j = 0;j < 101;j++){
				for(int it : sq[i][j]){
					a[it] = j;
				}
			}
		}
		for(i = 1;i <= n;i++){
			p(a[i]);
		}
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define p(n) printf("%d ",n)
#define fi first
#define sec second
#define mp make_pair
#define pb push_back
#define INF 1LL*1e18
int a[1000005];
list<int>sq[451][101];
void apply(int bl,int id,int flag,int x,int y,int id2 = 0)
{
	//cout << id << " " << id2 << endl;
		for(auto it = sq[bl][x].begin();it != sq[bl][x].end();){
			//cout << *it << endl;
			if((flag == 1 && *it >= id) || (flag == 0 && *it <= id) || (flag == 2 && *it >= id && *it <= id2)){
				auto it2 = ++it;
				--it;
				sq[bl][y].splice(sq[bl][y].begin(),sq[bl][x],it);
				it = it2;
			}
			else{
				it++;
			}
		}
}
int main()
{
	int i,j,k,l,m,n,t,r,cnt;
	int flag = 0;
	ll ans = 0,an = 0;
	t = 1;
	while(t--){
		s(n);
		int fa = 480;
		for(i = 1;i <= n;i++){
			s(a[i]);
			sq[i/fa][a[i]].pb(i);
		}
		int q,x,y;
		s(q);
		//cout << q << endl;
		while(q--){
			s(l);s(r);s(x);s(y);
			if(x == y) continue;
		//	cout << l << " " << r << " " << x << " " << y << endl;
			if(l/fa == r/fa){
				apply(l/fa,l,2,x,y,r);
			}

			else{
				apply(l/fa,l,1,x,y);
				apply(r/fa,r,0,x,y);
				for(i = l/fa +1;i < r/fa;i++){
					sq[i][y].splice(sq[i][y].begin(),sq[i][x]);
				}
			}
		}
		for(i = 0;i < 450;i++){
			for(j = 0;j < 101;j++){
				for(int it : sq[i][j]){
					a[it] = j;
				}
			}
		}
		for(i = 1;i <= n;i++){
			p(a[i]);
		}
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define p(n) printf("%d ",n)
#define fi first
#define sec second
#define mp make_pair
#define pb push_back
#define INF 1LL*1e18
int a[1000005];
list<int>sq[502][101];
void apply(int bl,int id,int flag,int x,int y,int id2 = 0)
{
	//cout << id << " " << id2 << endl;
		for(auto it = sq[bl][x].begin();it != sq[bl][x].end();){
			//cout << *it << endl;
			if((flag == 1 && *it >= id) || (flag == 0 && *it <= id) || (flag == 2 && *it >= id && *it <= id2)){
				auto it2 = ++it;
				--it;
				sq[bl][y].splice(sq[bl][y].begin(),sq[bl][x],it);
				it = it2;
			}
			else{
				it++;
			}
		}
}
int main()
{
	int i,j,k,l,m,n,t,r,cnt;
	int flag = 0;
	ll ans = 0,an = 0;
	t = 1;
	while(t--){
		s(n);
		int fa = 400;
		for(i = 1;i <= n;i++){
			s(a[i]);
			sq[i/fa][a[i]].pb(i);
		}
		int q,x,y;
		s(q);
		//cout << q << endl;
		while(q--){
			s(l);s(r);s(x);s(y);
		//	cout << l << " " << r << " " << x << " " << y << endl;
			if(l/fa == r/fa){
				apply(l/fa,l,2,x,y,r);
			}
			else{
				apply(l/fa,l,1,x,y);
				apply(r/fa,r,0,x,y);
				for(i = l/fa +1;i < r/fa;i++){
					sq[i][y].splice(sq[i][y].begin(),sq[i][x]);
				}
			}
		}
		for(i = 0;i < 450;i++){
			for(j = 0;j < 101;j++){
				for(int it : sq[i][j]){
					a[it] = j;
				}
			}
		}
		for(i = 1;i <= n;i++){
			p(a[i]);
		}
		printf("\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define s(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
#define p(n) printf("%d ",n)
#define fi first
#define sec second
#define mp make_pair
#define pb push_back
#define INF 1LL*1e18
int a[1000005];
list<int>sq[451][101];
void apply(int bl,int id,int flag,int x,int y,int id2 = 0)
{
	//cout << id << " " << id2 << endl;
		for(auto it = sq[bl][x].begin();it != sq[bl][x].end();){
			//cout << *it << endl;
			if((flag == 1 && *it >= id) || (flag == 0 && *it <= id) || (flag == 2 && *it >= id && *it <= id2)){
				auto it2 = ++it;
				--it;
				sq[bl][y].splice(sq[bl][y].begin(),sq[bl][x],it);
				it = it2;
			}
			else{
				it++;
			}
		}
}
int main()
{
	int i,j,k,l,m,n,t,r,cnt;
	int flag = 0;
	ll ans = 0,an = 0;
	t = 1;
	while(t--){
		s(n);
		int fa = 480;
		for(i = 1;i <= n;i++){
			s(a[i]);
			sq[i/fa][a[i]].pb(i);
		}
		int q,x,y;
		s(q);
		//cout << q << endl;
		while(q--){
			s(l);s(r);s(x);s(y);
		//	cout << l << " " << r << " " << x << " " << y << endl;
			if(l/fa == r/fa){
				apply(l/fa,l,2,x,y,r);
			}
			else{
				apply(l/fa,l,1,x,y);
				apply(r/fa,r,0,x,y);
				for(i = l/fa +1;i < r/fa;i++){
					sq[i][y].splice(sq[i][y].begin(),sq[i][x]);
				}
			}
		}
		for(i = 0;i < 450;i++){
			for(j = 0;j < 101;j++){
				for(int it : sq[i][j]){
					a[it] = j;
				}
			}
		}
		for(i = 1;i <= n;i++){
			p(a[i]);
		}
		printf("\n");
	}
}


#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse2")
#include <iostream>
using namespace std;

int main() {
	ios_base::sync_with_stdio(0);
	//cin.tie(0);
	//cout.tie(0);

	unsigned char a[200007], x, y;
	int n, q, l, r;
	cin >> n;
	for(int i = 1; i <= n; ++i)
		cin >> a[i];

	cin >> q;
	while(q--) {
		cin >> l >> r >> x >> y;
		for(int i = l; i <= r; ++i)
  			if(a[i] == x) a[i] = y;
	}

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

	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);

   int n, q;
   uint16_t A;
   cin >> n;
   vector<uint8_t> a(n);
   for (int i = 0; i < n; ++i) {
      cin >> A;
      a[i] = (uint8_t)A;
   }

   int l, r;
   uint8_t x, y;
   uint16_t X, Y;
   cin >> q;
   while (q--) {
      cin >> l >> r >> X >> Y;
      x = (uint8_t)X;
      y = (uint8_t)Y;
      for (int i = l - 1; i < r; ++i) {
         a[i] = a[i] == x ? y : a[i];
      }
   }

   for (int i = 0; i < n; ++i) {
      cout << (uint16_t)a[i] << ' ';
   }
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);

   int n, q;
   uint16_t A;
   cin >> n;
   uint8_t a[200000];
   for (int i = 0; i < n; ++i) {
      cin >> A;
      a[i] = (uint8_t)A;
   }

   int l, r;
   uint8_t x, y;
   uint16_t X, Y;
   cin >> q;
   while (q--) {
      cin >> l >> r >> X >> Y;
      x = (uint8_t)X;
      y = (uint8_t)Y;
      for (int i = l - 1; i < r; ++i) {
         if (a[i] == x) {
            a[i] = y;
         }
      }
   }

   for (int i = 0; i < n; ++i) {
      cout << (uint16_t)a[i] << ' ';
   }
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")
#include <iostream>
using namespace std;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	unsigned char a[200007], x, y;
	int n, q, l, r;
	cin >> n;
	for(int i = 1; i <= n; )
		cin >> a[i++];

	cin >> q;
	while(q--) {
		cin >> l >> r >> x >> y;
		for(int i = l; i <= r; ++i)
  			if(a[i] == x) a[i] = y;
	}

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

	return 0;
}


#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse2")
#include <iostream>

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	unsigned char a[200007], x, y;
	int n, q, l, r;
	cin >> n;
	for(int i = 1; i <= n; )
		cin >> a[i++];

	cin >> q;
	while(q--) {
		cin >> l >> r >> x >> y;
		for(int i = l; i <= r; ++i)
  			if(a[i] == x) a[i] = y;
	}

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

	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);

   int n, q;
   uint16_t A;
   cin >> n;
   vector<uint8_t> a(n);
   for (int i = 0; i < n; ++i) {
      cin >> A;
      a[i] = (uint8_t)A;
   }

   int l, r;
   uint8_t x, y;
   uint16_t X, Y;
   cin >> q;
   while (q--) {
      cin >> l >> r >> X >> Y;
      x = (uint8_t)X;
      y = (uint8_t)Y;
      for (int i = l - 1; i < r; ++i) {
         if (a[i] == x) {
            a[i] = y;
         }
      }
   }

   for (int i = 0; i < n; ++i) {
      cout << (uint16_t)a[i] << ' ';
   }
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <iostream>
#include <vector>
using namespace std;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n, q;
	vector<int> v;
	v.reserve(200007);
	cin >> n;
	for(int i = 0; i < n; ++i)
		cin >> v[i];

	cin >> q;
	while(q--) {
		int L, R, x, y;
		cin >> L >> R >> x >> y;
		for(int i = L - 1; i < R; ++i) {
			v[i] = v[i] == x ? y : v[i];
		}
	}

	for(int i = 0; i < n; ++i) {
		cout << v[i] << " ";
	}

	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;

int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);

   int n, q;
   uint16_t A;
   cin >> n;
   array<uint8_t, 200000> a;
   for (int i = 0; i < n; ++i) {
      cin >> A;
      a[i] = (uint8_t)A;
   }

   int l, r;
   uint8_t x, y;
   uint16_t X, Y;
   cin >> q;
   while (q--) {
      cin >> l >> r >> X >> Y;
      x = (uint8_t)X;
      y = (uint8_t)Y;
      for (int i = l - 1; i < r; ++i) {
         if (a[i] == x) {
            a[i] = y;
         }
      }
   }

   for (int i = 0; i < n; ++i) {
      cout << (uint16_t)a[i] << ' ';
   }
}


#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cout<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cout<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 1120000000
#define long long long // for codeforces

#define SZ 100

template<typename T>
class SegTree {
public:
  int n;
  T e;
  inline vector<char> op(const vector<char> &lv, const vector<char> &rv){
    vector<char> nv(SZ);
    rep(i,SZ) nv[i] = rv[lv[i]];
    return move(nv);
  }
  vector<T> data;
  SegTree(int m, T _e) : e(_e){
    n=1;
    while(n<m) n*=2;
    data.resize(2*n, e);
  }
  T query(int l, int r){
    T vl = e, vr = e;
    for(l+=n, r+=n; l<r; l/=2, r/=2){
      if(l&1) vl = op(vl, data[l++]);
      if(r&1) vr = op(data[--r], vr);
    }
    return op(vl,vr);
  }
  void update(int k, const T &a){
    k+=n;
    data[k]=a;
    while(k>0){
      k = k/2;
      data[k] = op(data[k*2], data[k*2+1]);
    }
  }
  inline T operator[](int idx){ return data[idx+n]; }
};

int l[200005], r[200005], x[200005], y[200005];
vector<int> vl[200005], vr[200005];

int main(){
  int n;
  scanf("%d", &n);
  vector<int> v(n);
  rep(i,n) scanf("%d", &v[i]);

  vector<char> ev(SZ);
  rep(i,SZ) ev[i] = i;

  SegTree<vector<char>> st(n, ev);

  int m;
  scanf("%d", &m);
  rep(i,m) scanf("%d %d %d %d", l+i, r+i, x+i, y+i);
  rep(i,m){
    x[i]--; y[i]--;
    l[i]--;
    vl[l[i]].pb(i);
    vr[r[i]].pb(i);
  }

  rep(i,n){
    // del
    for(auto p : vr[i]){
      auto nv = ev;
      ev[x[p]] = x[p];
      st.update(p, ev);
    }

    // ins
    for(auto p : vl[i]){
      auto nv = ev;
      ev[x[p]] = y[p];
      st.update(p, ev);
    }

    int ans = st.query(0,n)[v[i]-1] + 1;
    printf("%d%c", ans, " \n"[i==n-1]);
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cout<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cout<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 1120000000
#define long long long // for codeforces

#define SZ 100

using VC = vector<char>;

template<typename T>
class SegTree {
public:
  int n;
  T e;
  inline VC op(const VC &lv, const VC &rv){
    VC nv(SZ);
    rep(i,SZ) nv[i] = rv[lv[i]];
    return move(nv);
  }
  vector<T> data;
  SegTree(int m, T _e) : e(_e){
    n=1;
    while(n<m) n*=2;
    data.resize(2*n, e);
  }
  T query(int l, int r){
    T vl = e, vr = e;
    for(l+=n, r+=n; l<r; l/=2, r/=2){
      if(l&1) vl = op(vl, data[l++]);
      if(r&1) vr = op(data[--r], vr);
    }
    return op(vl,vr);
  }
  void update(int k, int s, int t){
    k+=n;
    data[k][s] = t;
    while(k>0){
      k = k/2;
      data[k] = op(data[k*2], data[k*2+1]);
    }
  }
  inline T operator[](int idx){ return data[idx+n]; }
};

int v[200005];
int l[200005], r[200005], x[200005], y[200005];
vector<int> vl[200005], vr[200005];

int main(){
  int n;
  scanf("%d", &n);
  rep(i,n) scanf("%d", v+i);

  int m;
  scanf("%d", &m);
  rep(i,m){
    scanf("%d %d %d %d", l+i, r+i, x+i, y+i);
    x[i]--; y[i]--;
    l[i]--;
    vl[l[i]].pb(i);
    vr[r[i]].pb(i);
  }

  VC ev(SZ);
  rep(i,SZ) ev[i] = i;

  SegTree<VC> st(m, ev);

  rep(i,n){
    // del
    for(auto p : vr[i]){
      st.update(p, x[p], x[p]);
    }

    // ins
    for(auto p : vl[i]){
      st.update(p, x[p], y[p]);
    }

    int ans = st.query(0,m)[v[i]-1] + 1;
    printf("%d%c", ans, " \n"[i==n-1]);
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cout<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cout<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 1120000000
#define long long long // for codeforces

#define SZ 100

template<typename T>
class SegTree {
public:
  int n;
  T e;
  inline vector<char> op(const vector<char> &lv, const vector<char> &rv){
    vector<char> nv(SZ);
    rep(i,SZ) nv[i] = rv[lv[i]];
    return move(nv);
  }
  vector<T> data;
  SegTree(int m, T _e) : e(_e){
    n=1;
    while(n<m) n*=2;
    data.resize(2*n, e);
  }
  T query(int l, int r){
    T vl = e, vr = e;
    for(l+=n, r+=n; l<r; l/=2, r/=2){
      if(l&1) vl = op(vl, data[l++]);
      if(r&1) vr = op(data[--r], vr);
    }
    return op(vl,vr);
  }
  void update(int k, const T &a){
    k+=n;
    data[k]=a;
    while(k>0){
      k = k/2;
      data[k] = op(data[k*2], data[k*2+1]);
    }
  }
  inline T operator[](int idx){ return data[idx+n]; }
};

int v[200005];
int l[200005], r[200005], x[200005], y[200005];
vector<int> vl[200005], vr[200005];

int main(){
  int n;
  scanf("%d", &n);
  rep(i,n) scanf("%d", v+i);

  int m;
  scanf("%d", &m);
  rep(i,m){
    scanf("%d %d %d %d", l+i, r+i, x+i, y+i);
    x[i]--; y[i]--;
    l[i]--; r[i]--;
    vl[l[i]].pb(i);
    vr[r[i]].pb(i);
  }

  vector<char> ev(SZ);
  rep(i,SZ) ev[i] = i;

  SegTree<vector<char>> st(m, ev);

  rep(i,n){
    // del
    for(auto p : vr[i]){
      auto nv = ev;
      ev[x[p]] = x[p];
      st.update(p, ev);
    }

    // ins
    for(auto p : vl[i]){
      auto nv = ev;
      ev[x[p]] = y[p];
      st.update(p, ev);
    }

    int ans = st.query(0,m)[v[i]-1] + 1;
    printf("%d%c", ans, " \n"[i==n-1]);
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cout<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cout<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 1120000000
#define long long long // for codeforces

#define SZ 100

using VC = vector<char>;

template<typename T>
class SegTree {
public:
  int n;
  T e;
  inline VC op(const VC &lv, const VC &rv){
    VC nv(SZ);
    rep(i,SZ) nv[i] = rv[lv[i]];
    return move(nv);
  }
  vector<T> data;
  SegTree(int m, T _e) : e(_e){
    n=1;
    while(n<m) n*=2;
    data.resize(2*n, e);
  }
  T query(int l, int r){
    T vl = e, vr = e;
    for(l+=n, r+=n; l<r; l/=2, r/=2){
      if(l&1) vl = op(vl, data[l++]);
      if(r&1) vr = op(data[--r], vr);
    }
    return op(vl,vr);
  }
  void update(int k, int s, int t){
    k+=n;
    data[k][s] = t;
    while(k>0){
      k = k/2;
      data[k] = op(data[k*2], data[k*2+1]);
    }
  }
  inline T operator[](int idx){ return data[idx+n]; }
};

int v[200005];
int l[200005], r[200005], x[200005], y[200005];
vector<int> vl[200005], vr[200005];

int main(){
  int n;
  scanf("%d", &n);
  rep(i,n) scanf("%d", v+i);

  int m;
  scanf("%d", &m);
  rep(i,m){
    scanf("%d %d %d %d", l+i, r+i, x+i, y+i);
    x[i]--; y[i]--;
    l[i]--;
    vl[l[i]].pb(i);
    vr[r[i]].pb(i);
  }

  VC ev(SZ);
  rep(i,SZ) ev[i] = i;

  SegTree<VC> st(m, ev);

  rep(i,n){
    // del
    for(auto p : vr[i]){
      st.update(p, x[p], x[p]);
    }

    // ins
    for(auto p : vl[i]){
      st.update(p, x[p], y[p]);
    }

    // int ans = st.query(0,m)[v[i]-1] + 1;
    int ans = st.data[1][v[i]-1] + 1;
    printf("%d%c", ans, " \n"[i==n-1]);
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cout<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cout<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 1120000000
#define long long long // for codeforces

template<typename T>
class SegTree {
public:
  int n;
  T e;
  inline vector<char> op(const vector<char> &lv, const vector<char> &rv){
    vector<char> nv(10);
    rep(i,10) nv[i] = rv[lv[i]];
    return move(nv);
  }
  vector<T> data;
  SegTree(int m, T _e) : e(_e){
    n=1;
    while(n<m) n*=2;
    data.resize(2*n, e);
  }
  T query(int l, int r){
    T vl = e, vr = e;
    for(l+=n, r+=n; l<r; l/=2, r/=2){
      if(l&1) vl = op(vl, data[l++]);
      if(r&1) vr = op(data[--r], vr);
    }
    return op(vl,vr);
  }
  void update(int k, const T &a){
    k+=n;
    data[k]=a;
    while(k>0){
      k = k/2;
      data[k] = op(data[k*2], data[k*2+1]);
    }
  }
  inline T operator[](int idx){ return data[idx+n]; }
};

int l[200005], r[200005], x[200005], y[200005];
vector<int> vl[200005], vr[200005];

int main(){
  int n;
  scanf("%d", &n);
  vector<int> v(n);
  rep(i,n) scanf("%d", &v[i]);

  vector<char> ev(10);
  rep(i,10) ev[i] = i;

  SegTree<vector<char>> st(n, ev);

  int m;
  scanf("%d", &m);
  rep(i,m) scanf("%d %d %d %d", l+i, r+i, x+i, y+i);
  rep(i,m){
    x[i]--; y[i]--;
    l[i]--;
    vl[l[i]].pb(i);
    vr[r[i]].pb(i);
  }

  rep(i,n){
    // del
    for(auto p : vr[i]){
      auto nv = ev;
      ev[x[p]] = x[p];
      st.update(p, ev);
    }

    // ins
    for(auto p : vl[i]){
      auto nv = ev;
      ev[x[p]] = y[p];
      st.update(p, ev);
    }

    int ans = st.query(0,n)[v[i]-1] + 1;
    printf("%d%c", ans, " \n"[i==n-1]);
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cout<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cout<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 1120000000
#define long long long // for codeforces

#define SZ 100

template<typename T>
class SegTree {
public:
  int n;
  T e;
  inline vector<char> op(const vector<char> &lv, const vector<char> &rv){
    vector<char> nv(SZ);
    rep(i,SZ) nv[i] = rv[lv[i]];
    return move(nv);
  }
  vector<T> data;
  SegTree(int m, T _e) : e(_e){
    n=1;
    while(n<m) n*=2;
    data.resize(2*n, e);
  }
  T query(int l, int r){
    T vl = e, vr = e;
    for(l+=n, r+=n; l<r; l/=2, r/=2){
      if(l&1) vl = op(vl, data[l++]);
      if(r&1) vr = op(data[--r], vr);
    }
    return op(vl,vr);
  }
  void update(int k, const T &a){
    k+=n;
    data[k]=a;
    while(k>0){
      k = k/2;
      data[k] = op(data[k*2], data[k*2+1]);
    }
  }
  inline T operator[](int idx){ return data[idx+n]; }
};

int l[200005], r[200005], x[200005], y[200005];
vector<int> vl[200005], vr[200005];

int main(){
  int n;
  scanf("%d", &n);
  vector<int> v(n);
  rep(i,n) scanf("%d", &v[i]);

  vector<char> ev(SZ);
  rep(i,SZ) ev[i] = i;

  int m;
  scanf("%d", &m);
  rep(i,m) scanf("%d %d %d %d", l+i, r+i, x+i, y+i);
  rep(i,m){
    x[i]--; y[i]--;
    l[i]--;
    vl[l[i]].pb(i);
    vr[r[i]].pb(i);
  }

  SegTree<vector<char>> st(m, ev);

  rep(i,n){
    // del
    for(auto p : vr[i]){
      auto nv = ev;
      ev[x[p]] = x[p];
      st.update(p, ev);
    }

    // ins
    for(auto p : vl[i]){
      auto nv = ev;
      ev[x[p]] = y[p];
      st.update(p, ev);
    }

    int ans = st.query(0,m)[v[i]-1] + 1;
    printf("%d%c", ans, " \n"[i==n-1]);
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cout<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cout<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 1120000000
#define long long long // for codeforces

template<typename T>
class SegTree {
public:
  int n;
  T e;
  T (*op)(T, T);
  vector<T> data;
  SegTree(int m, T _e, T (*_op)(T, T)) : e(_e), op(_op){
    n=1;
    while(n<m) n*=2;
    data.resize(2*n, e);
  }
  T query(int l, int r){
    T vl = e, vr = e;
    for(l+=n, r+=n; l<r; l/=2, r/=2){
      if(l&1) vl = op(vl, data[l++]);
      if(r&1) vr = op(data[--r], vr);
    }
    return op(vl,vr);
  }
  void update(int k, const T &a){
    k+=n;
    data[k]=a;
    while(k>0){
      k = k/2;
      data[k] = op(data[k*2], data[k*2+1]);
    }
  }
  inline T operator[](int idx){ return data[idx+n]; }
};

int l[200005], r[200005], x[200005], y[200005];
vector<int> vl[200005], vr[200005];

int main(){
  int n;
  scanf("%d", &n);
  vector<int> v(n);
  rep(i,n) scanf("%d", &v[i]);

  vector<char> ev(10);
  rep(i,10) ev[i] = i;

  SegTree<vector<char>> st(n, ev, [](vector<char> lv, vector<char> rv){
    vector<char> nv(10);
    rep(i,10) nv[i] = rv[lv[i]];
    return nv;
  });

  int m;
  scanf("%d", &m);
  rep(i,m) scanf("%d %d %d %d", l+i, r+i, x+i, y+i);
  rep(i,m){
    x[i]--; y[i]--;
    l[i]--;
    vl[l[i]].pb(i);
    vr[r[i]].pb(i);
  }

  rep(i,n){
    // del
    for(auto p : vr[i]){
      auto nv = ev;
      ev[x[p]] = x[p];
      st.update(p, ev);
    }

    // ins
    for(auto p : vl[i]){
      auto nv = ev;
      ev[x[p]] = y[p];
      st.update(p, ev);
    }

    int ans = st.query(0,n)[v[i]-1] + 1;
    printf("%d%c", ans, " \n"[i==n-1]);
  }

  return 0;
}


#include <cstdio>
using namespace std;

const int maxn = 2e5;
int lson[25 * maxn + 11],rson[25 * maxn + 11],root[111],ans[maxn + 11];
int tot = 0;

void insert(int l,int r,int pos,int &x) {
    if (l > pos || r < pos) return;
    if (x == 0) x = ++tot;
    if (l == r) return;
    int mid = (l + r) >> 1;
    insert(l , mid , pos , lson[x]);
    insert(mid + 1 , r , pos , rson[x]);
}

void merge(int &x , int &y) {
    if (x == 0) return;
    if (y == 0) { y = x; x = 0; return; }
    merge(lson[x] , lson[y]);
    merge(rson[x] , rson[y]);
}

void update(int l,int r,int al,int ar,int &x,int &y) {
    if (l > ar || r < al) return;
    if (l >= al && r <= ar) { merge(x , y); return; }
    if (x == 0) return;
    if (y == 0) y = ++tot;
    int mid = (l + r) >> 1;
    update(l , mid , al , ar , lson[x] , lson[y]);
    update(mid + 1 , r , al , ar , rson[x] , rson[y]);
}

void getans(int p , int l , int r , int &x) {
    if (x == 0) return;
    if (l == r) { ans[l] = p; return; }
    int mid = (l + r) >> 1;
    getans(p , l , mid , lson[x]);
    getans(p , mid + 1 , r , rson[x]);
}

int main(){
    int n;
    scanf("%d" , &n);
    for (int i = 1; i <= n; i++) {
        int x;
        scanf("%d" , &x);
        insert(1 , n , i , root[x]);
    }
    int m;
    scanf("%d" , &m);
    for (int i = 1; i <= m; i++) {
        int l,r,x,y;
        scanf("%d %d %d %d",&l,&r,&x,&y);
        update(1 , n , l , r , root[x] , root[y]);
    }
    for (int i = 1; i <= 100; i++) getans(i , 1 , n , root[i]);
    for (int i = 1; i <= n; i++) printf("%d ",ans[i]);
}


#include <cstdio>
using namespace std;

const int maxn = 2e5;
int lson[25 * maxn + 11],rson[25 * maxn + 11],root[111],ans[maxn + 11];
int tree[25 * maxn + 11];

int tot = 0;

void insert(int l,int r,int pos,int &x) {
	if (l > pos || r < pos) return;
	if (x == 0) x = ++tot;
	if (l == r) { tree[x] = 1; return; }
	int mid = (l + r) >> 1;
	insert(l , mid , pos , lson[x]);
	insert(mid + 1 , r , pos , rson[x]);
	tree[x] = tree[lson[x]] + tree[rson[x]];
}

void merge(int &x , int &y) {
	if (x == 0) return;
	if (y == 0) { y = x; x = 0; return; }
	merge(lson[x] , lson[y]);
	merge(rson[x] , rson[y]);
	tree[y] += tree[x]; x = 0;
}

void update(int l,int r,int al,int ar,int &x,int &y) {
	if (l > ar || r < al) return;
	if (l >= al && r <= ar) { merge(x , y); return; }
	if (x == 0) return;
	if (y == 0) y = ++tot;
	int mid = (l + r) >> 1;
	update(l , mid , al , ar , lson[x] , lson[y]);
	update(mid + 1 , r , al , ar , rson[x] , rson[y]);
}

void getans(int p , int l , int r , int &x) {
	if (x == 0) return;
	if (tree[x] == r - l + 1) {
		for (int i = l; i <= r; i++) ans[i] = p;
		return;
	}
	int mid = (l + r) >> 1;
	getans(p , l , mid , lson[x]);
	getans(p , mid + 1 , r , rson[x]);
}

int main(){
	int n;
	scanf("%d" , &n);
	for (int i = 1; i <= n; i++) {
		int x;
		scanf("%d" , &x);
		insert(1 , n , i , root[x]);
	}
	int m;
	scanf("%d" , &m);
	for (int i = 1; i <= m; i++) {
		int l,r,x,y;
		scanf("%d %d %d %d",&l,&r,&x,&y);
		update(1 , n , l , r , root[x] , root[y]);
	}
	for (int i = 1; i <= 100; i++) getans(i , 1 , n , root[i]);
	for (int i = 1; i <= n; i++) printf("%d ",ans[i]);
}


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

const int maxn = 2e5;
int ans[maxn + 11],root[100 + 11],lson[25 * maxn + 11],rson[25 * maxn + 11],tree[25 * maxn + 11];
int tot = 0;

void insert(int &x,int l,int r,int pos) {
	if (l > pos || r < pos) return;
	if (x == 0) x = ++tot;
	if (l == r) { tree[x] = 1; return; }
	int mid = (l + r) >> 1;
	insert(lson[x] , l , mid , pos);
	insert(rson[x] , mid + 1 , r , pos);
	tree[x] = tree[lson[x]] + tree[rson[x]];
}

void merge(int &x,int &y) {
	if (x == 0) return;
	if (y == 0) { y = x; x = 0; return; }
	merge(lson[x] , lson[y]);
	merge(rson[x] , rson[y]);
	tree[y] += tree[x]; x = 0;
}

void update(int l,int r,int &x,int &y,int al,int ar) {
	if (l > ar || r < al) return;
	if (x == 0 || tree[x] == 0) return;
	if (l >= al && r <= ar) { merge(x , y); return; }
	if (y == 0) y = ++tot;
	int mid = (l + r) >> 1;
	update(l , mid , lson[x] , lson[y] , al , ar);
	update(mid + 1 , r , rson[x] , rson[y] , al , ar);
}

void get(int p,int x,int l,int r) {
	if (tree[x] == r - l + 1) {
		for (int i = l; i <= r; i++) ans[i] = p;
		return;
	}
	if (l == r) return;
	if (x == 0 || tree[x] == 0) return;
	int mid = (l + r) >> 1;
	get(p , lson[x] , l , mid);
	get(p , rson[x] , mid + 1 , r);
}

int main(){
	//freopen("in.txt" , "r" ,stdin);
	int n;
	scanf("%d",&n);
	for (int i = 1; i <= n; i++) {
		int x;
		scanf("%d",&x);
		insert(root[x] , 1 , n , i);
	}
	int m;
	scanf("%d",&m);
	for (int i = 1; i <=m  ;i++) {
		int l,r,x,y;
		scanf("%d %d %d %d",&l,&r,&x,&y);
		if (x == y) continue;
		update(1 , n , root[x] , root[y] , l , r);
	}
	for (int i = 1; i <= 100; i++){
		get(i , root[i] , 1 , n);
	}
	for (int i = 1;i <= n; i++) printf("%d " , ans[i]);
}


#include <bits/stdc++.h>
#define lson rt << 1
#define rson rt << 1 | 1
using namespace std;

const int maxn = 2e5;
int ans[maxn + 11];

struct segment{
	int cnt[4  * maxn + 11];
	short lazy[4 * maxn + 11];
	//int lazy1[4  * maxn + 11],lazy2[4  * maxn + 11]; //1->del 2->add
	void push_up(int rt) { cnt[rt] = cnt[lson] + cnt[rson]; }
	void insert(int rt,int l,int r,int pos,int val) {
		if (l > pos || r < pos) return;
		if (l == r) { cnt[rt] += val; return; }
		int mid = (l + r) >> 1;
		insert(lson , l , mid , pos , val);
		insert(rson , mid + 1 , r , pos , val);
		push_up(rt);
	}
}seg[101];

void push_down(int i,int rt) {
	short x = seg[i].lazy[rt];
	if (seg[i].cnt[rt] == 0) {
		seg[x].cnt[lson] += seg[i].cnt[lson]; seg[x].cnt[rson] += seg[i].cnt[rson];
		seg[i].cnt[lson] = 0; seg[i].cnt[rson] = 0;
		seg[i].lazy[lson] = seg[i].lazy[rson] = x; seg[x].lazy[lson] = seg[x].lazy[rson] = seg[x].lazy[rt];
		seg[i].lazy[rt] = 0; seg[x].lazy[rt] = 0;
	}
	else {
		seg[i].cnt[lson] += seg[x].cnt[lson]; seg[i].cnt[rson] += seg[x].cnt[rson];
		seg[x].cnt[lson] = seg[x].cnt[rson] = 0;
		seg[i].lazy[lson] = seg[i].lazy[rson] = x; seg[x].lazy[lson] = seg[x].lazy[rson] = seg[x].lazy[rt];
		seg[i].lazy[rt] = 0; seg[x].lazy[rt] = 0;
	}
}

void get(int rt,int l,int r,int x) {
	if (seg[x].cnt[rt] == 0) return;
	if (seg[x].cnt[rt] == r - l + 1) {
		for (int i = l; i <= r; i++) ans[i] = x;
		return;
	}
	int mid = (l + r) >> 1;
	if (seg[x].lazy[rt]) push_down(x , rt);
	get(lson , l , mid , x);
	get(rson , mid + 1 , r , x);
}

void update(int rt,int l ,int r,int al,int ar,int x,int y) {
	if (l > ar || r < al) return;
	if (seg[x].lazy[rt]) push_down(x , rt);
	if (seg[y].lazy[rt]) push_down(y , rt);
	if (l >= al && r <= ar) {
		seg[x].lazy[rt] = y; seg[y].lazy[rt] = x;
		seg[y].cnt[rt] += seg[x].cnt[rt]; seg[x].cnt[rt] = 0;
		return;
	}
	int mid = (l + r) >> 1;
	update(lson , l , mid , al , ar , x , y);
	update(rson , mid + 1 , r , al , ar , x , y);
	seg[x].push_up(rt); seg[y].push_up(rt);
}

int main(){
	//freopen("in.txt" , "r" ,stdin);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int x;
		cin >> x;
		seg[x].insert(1 , 1 , n , i , 1);
	}
	int m;
	scanf("%d",&m);
	for (int i = 1; i <=m  ;i++) {
		int l,r,x,y;
		cin >> l >> r >> x >> y;
		if (x == y) continue;
		update(1 , 1 , n , l , r , x , y);
	}
	for (int i = 1; i <= 100; i++)
		get(1 , 1 , n , i);
	for (int i = 1;i <= n; i++) printf("%d " , ans[i]);
}


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

const int maxn = 2e5;
int lson[25 * maxn + 11],rson[25 * maxn + 11],root[111],ans[maxn + 11];
int tot = 0;

void insert(int l,int r,int pos,int &x) {
    if (l > pos || r < pos) return;
    if (x == 0) x = ++tot;
    if (l == r) return;
    int mid = (l + r) >> 1;
    insert(l , mid , pos , lson[x]);
    insert(mid + 1 , r , pos , rson[x]);
}

void merge(int &x , int &y) {
    if (x == 0) return;
    if (y == 0) { y = x; x = 0; return; }
    merge(lson[x] , lson[y]);
    merge(rson[x] , rson[y]);
    x = 0;
}

void update(int l,int r,int al,int ar,int &x,int &y) {
    if (l > ar || r < al) return;
    if (l >= al && r <= ar) { merge(x , y); return; }
    if (x == 0) return;
    if (y == 0) y = ++tot;
    int mid = (l + r) >> 1;
    update(l , mid , al , ar , lson[x] , lson[y]);
    update(mid + 1 , r , al , ar , rson[x] , rson[y]);
}

void getans(int p , int l , int r , int &x) {
    if (x == 0) return;
    if (l == r) { ans[l] = p; return; }
    int mid = (l + r) >> 1;
    getans(p , l , mid , lson[x]);
    getans(p , mid + 1 , r , rson[x]);
}

int main(){
    int n;
    scanf("%d" , &n);
    for (int i = 1; i <= n; i++) {
        int x;
        scanf("%d" , &x);
        insert(1 , n , i , root[x]);
    }
    int m;
    scanf("%d" , &m);
    for (int i = 1; i <= m; i++) {
        int l,r,x,y;
        scanf("%d %d %d %d",&l,&r,&x,&y);
        if (x == y) continue;
        update(1 , n , l , r , root[x] , root[y]);
    }
    for (int i = 1; i <= 100; i++) getans(i , 1 , n , root[i]);
    for (int i = 1; i <= n; i++) printf("%d ",ans[i]);
}


#include <bits/stdc++.h>
#define lson rt << 1
#define rson rt << 1 | 1
using namespace std;

const int maxn = 2e5;
int ans[maxn + 11];

struct segment{
	int cnt[4  * maxn + 11];
	short lazy[4 * maxn + 11];
	//int lazy1[4  * maxn + 11],lazy2[4  * maxn + 11]; //1->del 2->add
	void push_up(int rt) { cnt[rt] = cnt[lson] + cnt[rson]; }
	void insert(int rt,int l,int r,int pos,int val) {
		if (l > pos || r < pos) return;
		if (l == r) { cnt[rt] += val; return; }
		int mid = (l + r) >> 1;
		insert(lson , l , mid , pos , val);
		insert(rson , mid + 1 , r , pos , val);
		push_up(rt);
	}
}seg[101];

void push_down(int i,int rt) {
	short x = seg[i].lazy[rt];
	if (seg[i].cnt[rt] == 0) {
		seg[x].cnt[lson] += seg[i].cnt[lson]; seg[x].cnt[rson] += seg[i].cnt[rson];
		seg[i].cnt[lson] = 0; seg[i].cnt[rson] = 0;
		seg[i].lazy[lson] = seg[i].lazy[rson] = x; seg[x].lazy[lson] = seg[x].lazy[rson] = seg[x].lazy[rt];
		seg[i].lazy[rt] = 0; seg[x].lazy[rt] = 0;
	}
	else {
		seg[i].cnt[lson] += seg[x].cnt[lson]; seg[i].cnt[rson] += seg[x].cnt[rson];
		seg[x].cnt[lson] = seg[x].cnt[rson] = 0;
		seg[i].lazy[lson] = seg[i].lazy[rson] = x; seg[x].lazy[lson] = seg[x].lazy[rson] = seg[x].lazy[rt];
		seg[i].lazy[rt] = 0; seg[x].lazy[rt] = 0;
	}
}

void get(int rt,int l,int r,int x) {
	if (seg[x].cnt[rt] == 0) return;
	if (seg[x].cnt[rt] == r - l + 1) {
		for (int i = l; i <= r; i++) ans[i] = x;
		return;
	}
	int mid = (l + r) >> 1;
	if (seg[x].lazy[rt]) push_down(x , rt);
	get(lson , l , mid , x);
	get(rson , mid + 1 , r , x);
}

void update(int rt,int l ,int r,int al,int ar,int x,int y) {
	if (l > ar || r < al) return;
	if (l >= al && r <= ar) {
		seg[x].lazy[rt] = y; seg[y].lazy[rt] = x;
		seg[y].cnt[rt] += seg[x].cnt[rt]; seg[x].cnt[rt] = 0;
		return;
	}
	if (seg[x].lazy[rt]) push_down(x , rt);
	if (seg[y].lazy[rt]) push_down(y , rt);
	int mid = (l + r) >> 1;
	update(lson , l , mid , al , ar , x , y);
	update(rson , mid + 1 , r , al , ar , x , y);
	seg[x].push_up(rt); seg[y].push_up(rt);
}

int main(){
	//freopen("in.txt" , "r" ,stdin);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int x;
		cin >> x;
		seg[x].insert(1 , 1 , n , i , 1);
	}
	int m;
	scanf("%d",&m);
	for (int i = 1; i <=m  ;i++) {
		int l,r,x,y;
		cin >> l >> r >> x >> y;
		if (x == y) continue;
		update(1 , 1 , n , l , r , x , y);
	}
	for (int i = 1; i <= 100; i++)
		get(1 , 1 , n , i);
	for (int i = 1;i <= n; i++) printf("%d " , ans[i]);
}


#include <bits/stdc++.h>
#define lson rt << 1
#define rson rt << 1 | 1
using namespace std;

const int maxn = 2e5;
int ans[maxn + 11];

struct segment{
	int cnt[4  * maxn + 11];
	void push_up(int rt) { cnt[rt] = cnt[lson] + cnt[rson]; }
	void insert(int rt,int l,int r,int pos,int val) {
		if (l > pos || r < pos) return;
		if (l == r) { cnt[rt] += val; return; }
		int mid = (l + r) >> 1;
		insert(lson , l , mid , pos , val);
		insert(rson , mid + 1 , r , pos , val);
		push_up(rt);
	}
}seg[101];

void get(int rt,int l,int r,int x) {
	if (seg[x].cnt[rt] == 0) return;
	if (seg[x].cnt[rt] == r - l + 1) {
		for (int i = l; i <= r; i++) ans[i] = x;
		return;
	}
	int mid = (l + r) >> 1;
	get(lson , l , mid , x);
	get(rson , mid + 1 , r , x);
}

void merge(int x,int y,int rt,int l,int r) {
	if (seg[x].cnt[rt] == 0) return;
	seg[y].cnt[rt] += seg[x].cnt[rt]; seg[x].cnt[rt] = 0;
	if (l == r) return;
	int mid = (l + r) >> 1;
	merge(x , y , lson , l , mid);
	merge(x , y , rson , mid + 1 , r);
}

void update(int rt,int l ,int r,int al,int ar,int x,int y) {
	if (l > ar || r < al) return;
	if (l >= al && r <= ar) {
		merge(x , y , rt , l , r);
		return;
	}
	int mid = (l + r) >> 1;
	update(lson , l , mid , al , ar , x , y);
	update(rson , mid + 1 , r , al , ar , x , y);
	seg[x].push_up(rt); seg[y].push_up(rt);
}

int main(){
	//freopen("in.txt" , "r" ,stdin);
	int n;
	scanf("%d",&n);
	for (int i = 1; i <= n; i++) {
		int x;
		scanf("%d",&x);
		seg[x].insert(1 , 1 , n , i , 1);
	}
	int m;
	scanf("%d",&m);
	for (int i = 1; i <=m  ;i++) {
		int l,r,x,y;
		scanf("%d %d %d %d",&l,&r,&x,&y);
		if (x == y) continue;
		update(1 , 1 , n , l , r , x , y);
	}
	for (int i = 1; i <= 100; i++){
		get(1 , 1 , n , i);
	}
	for (int i = 1;i <= n; i++) printf("%d " , ans[i]);
}


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

const int maxn = 2e5;
int lson[25 * maxn + 11],rson[25 * maxn + 11],root[111],ans[maxn + 11];
int tot = 0;

void insert(int l,int r,int pos,int &x) {
    if (l > pos || r < pos) return;
    if (x == 0) x = ++tot;
    if (l == r) return;
    int mid = (l + r) >> 1;
    insert(l , mid , pos , lson[x]);
    insert(mid + 1 , r , pos , rson[x]);
}

void merge(int &x , int &y) {
    if (x == 0) return;
    if (y == 0) { y = x; x = 0; return; }
    merge(lson[x] , lson[y]);
    merge(rson[x] , rson[y]);
    x = 0;
}

void update(int l,int r,int al,int ar,int &x,int &y) {
    if (l > ar || r < al) return;
    if (l >= al && r <= ar) { merge(x , y); return; }
    if (x == 0) return;
    if (y == 0) y = ++tot;
    int mid = (l + r) >> 1;
    update(l , mid , al , ar , lson[x] , lson[y]);
    update(mid + 1 , r , al , ar , rson[x] , rson[y]);
}

void getans(int p , int l , int r , int &x) {
    if (x == 0) return;
    if (l == r) { ans[l] = p; return; }
    int mid = (l + r) >> 1;
    getans(p , l , mid , lson[x]);
    getans(p , mid + 1 , r , rson[x]);
}

int main(){
    //memset(root , 0 , sizeof(root));
    int n;
    scanf("%d" , &n);
    for (int i = 1; i <= n; i++) {
        int x;
        scanf("%d" , &x);
        insert(1 , n , i , root[x]);
    }
    int m;
    scanf("%d" , &m);
    for (int i = 1; i <= m; i++) {
        int l,r,x,y;
        scanf("%d %d %d %d",&l,&r,&x,&y);
        update(1 , n , l , r , root[x] , root[y]);
    }
    //cout << root[50] << endl;
    for (int i = 1; i <= 100; i++) getans(i , 1 , n , root[i]);
    for (int i = 1; i <= n; i++) printf("%d ",ans[i]);
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#define P pair<int,int>
using namespace std;
int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	return x*f;
}
int n,sc,ls[1<<24],rs[1<<24],ans[200005],root[102],x,y,l,r,q;
/*void add(int &k,int l,int r,int x)//建不同权值根的区间主席树
{
	if (!k) k=++sc;
	if (l==r) return;
	int mid=(l+r)>>1;
	if (x<=mid) add(ls[k],l,mid,x);
	else add(rs[k],mid+1,r,x);
}*/
void add(int &pos,int l,int r,int v)
{
	if(!pos)pos=++sc;
	if(l==r)return;
	int m=(l+r)>>1;
	if(v<=m)add(ls[pos],l,m,v);
	else add(rs[pos],m+1,r,v);
}
/*P merge(int k1,int k2,int l,int r,int L,int R)
{
	if (!k1||l>R||r<L) return P(k1,k2);
	if (L<=l&&r<=R&&!k2) return P(0,k1);//如果对方这一段树是空的，那么可以直接接过去。否则只能一个一个接。
	if (!k2) k2=++sc;
	int mid=(l+r)>>1;P k;
	 k=merge(ls[k1],ls[k2],l,mid,L,R);
	ls[k1]=k.first;ls[k2]=k.second;
	 k=merge(rs[k1],rs[k2],mid+1,r,L,R);
	rs[k1]=k.first;rs[k2]=k.second;
	if (!ls[k1]&&!rs[k1]) k1=0;
	if (!ls[k2]&&!rs[k2]) k2=0;
	return P(k1,k2);
}*/
pair<int,int> merge(int p,int q,int l,int r,int x,int y)
{
	if(l>y||r<x||!p) return {p,q};
	if(!q&&x<=l&&r<=y) return {0,p};
	if(!q)q=++sc;
	int m=(l+r)>>1;
	pair<int,int> k=merge(ls[p],ls[q],l,m,x,y);
	ls[p]=k.first;ls[q]=k.second;
	k=merge(rs[p],rs[q],m+1,r,x,y);
	rs[p]=k.first;rs[q]=k.second;
	if(!ls[p]&&!rs[p])p=0;
	if(!ls[q]&&!rs[q])q=0;
	return {p,q};
}/*
void work(int k,int l,int r,int x)
{
	if (!k) return;
	if (l==r) {ans[l]=x;return;}
	int mid=(l+r)>>1;
	work(ls[k],l,mid,x);
	work(rs[k],mid+1,r,x);
}*/
void work(int pos,int l,int r,int v)
{
	if(!pos)return;
	if(l==r){ans[l]=v;return;}
	int m=(l+r)>>1;
    work(ls[pos],l,m,v);
	work(rs[pos],m+1,r,v);
}
int main()
{
    n=read();
	for (int i=1;i<=n;i++) x=read(),add(root[x],1,n,i);
	q=read();
	while (q--)
	{
		x=read(),y=read(),l=read(),r=read();
		if (x==y) continue;
		P k=merge(root[x],root[y],1,n,l,r);
		root[x]=k.first;root[y]=k.second;
	}
	for (int i=1;i<=100;i++) work(root[i],1,n,i);
	for (int i=1;i<=n;i++) printf("%d ",ans[i]);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	return x*f;
}
const int N=200005;
int n,sc,ls[N*20],rs[N*20],ans[N*20],root[102],x,y,l,r,q;
void add(int &k,int l,int r,int x)//建不同权值根的区间主席树
{
	if (!k) k=++sc;
	if (l==r) return;
	int mid=(l+r)>>1;
	if (x<=mid) add(ls[k],l,mid,x);
	else add(rs[k],mid+1,r,x);
}
void merge(int &k1,int &k2,int l,int r,int L,int R)
{
	if (!k1) return;
	if (L<=l&&r<=R&&!k2) {k2=k1;k1=0;return;}//如果对方这一段树是空的，那么可以直接接过去。否则只能一个一个接。
	if (!k2) k2=++sc;
	int mid=(l+r)>>1;
	if (L<=mid) merge(ls[k1],ls[k2],l,mid,L,R);
	if (R>mid) merge(rs[k1],rs[k2],mid+1,r,L,R);
	if (!ls[k1]&&!rs[k1]) k1=0;
	if (!ls[k2]&&!rs[k2]) k2=0;
}
void work(int k,int l,int r,int x)
{
	if (!k) return;
	if (l==r) {ans[l]=x;return;}
	int mid=(l+r)>>1;
	work(ls[k],l,mid,x);
	work(rs[k],mid+1,r,x);
}
int main()
{
    n=read();
	for (int i=1;i<=n;i++) x=read(),add(root[x],1,n,i);
	q=read();
	while (q--)
	{
		x=read(),y=read(),l=read(),r=read();
		merge(root[x],root[y],1,n,l,r);
	}
	for (int i=1;i<=100;i++) work(root[i],1,n,i);
	for (int i=1;i<=n;i++) printf("%d ",ans[i]);
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define M 1000000007
using namespace std;
int t,n,m,u,v,p;
vector<int> a[100001],b;
int vis[100001];
long long tt[100001],ans;
vector<long long> d;
void dfs(int k){
	 vis[k]=1;
	 for (int i=0; i<a[k].size(); i++)
	     if (!vis[a[k][i]]){
	     	dfs(a[k][i]);
	     	tt[k]+=tt[a[k][i]]+1;
		 }
}
int main(){
    cin>>t;
    while (t--){
    	  cin>>n;  d.clear(); ans=0; b.clear();
    	  for (int i=0; i<=n; i++){
    	  	  a[i].clear();
    	  	  tt[i+1]=vis[i+1]=0;
		  }

    	  for (int i=0; i<n-1; i++){
    	  	  cin>>u>>v;
    	  	  a[u].pb(v);
    	  	  a[v].pb(u);

	      }
		  cin>>m;
		  for (int i=0; i<m; i++){
		  	  cin>>u;
		  	  b.pb(u);
		  }
		  sort(b.begin(),b.end());
		  reverse(b.begin(),b.end());
		  vis[1]=1;
		  dfs(1);
		  /*for (int i=1; i<=n; i++)
		      cout<<tt[i]<<' ';
		 cout<<'\n'; */
    	  for (int i=2; i<=n; i++){
    	  	  d.pb(((long long)(tt[i]+1)*(long long)(n-(tt[i]+1)))%M);
		  }

		  sort(d.begin(),d.end());
		  reverse(d.begin(),d.end());
		  /*for (int i=0; i<n-1; i++)
		      cout<<d[i]<<' ';
		  cout<<'\n';*/
		  if (m>n-1){
		     int x=1;
		  	 for (int j=1; j<=(m-n+1); j++)
		  	 	 b[0]=((long long)b[0]*(long long)(b[j]))%M;

		  	 ans+=(d[0]*(long long)b[0])%M;
		  	 for (int i=1; i<n-1; i++)
		  	     ans+=(d[i]*(long long)b[i+m-n+1])%M;
		  	 cout<<ans<<endl;
		  }
		  else{
		  	 p=n-1-m;
		  	 for (int i=0; i<m; i++)
		  	     ans+=(d[i]*(long long)b[i])%M;
			 for (int i=m; i<n-1; i++)
		  	     ans+=d[i];
		  	 cout<<ans<<endl;
		  }
	}
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define M 1000000007
using namespace std;
int t,n,m,u,v,p;
vector<int> a[100005]; //邻接表存相邻结点
vector<long long> b; //存储所有的素因数
long long vis[100005]; //结点访问情况
long long tt[100005],ans; //tt：每个结点子孙数
vector<long long> d; //每条路径访问次数

void dfs(int k){
	 vis[k]=1;
	 for (int i=0; i<a[k].size(); i++)
	     if (!vis[a[k][i]]){
	     	dfs(a[k][i]);
	     	tt[k]+=tt[a[k][i]]+1;
		 }
}

int main(){
    cin>>t;
    while (t--){
    	  cin>>n;
		  d.clear(); b.clear();
		  ans=0;
    	  for (int i=0; i<=n; i++){
    	  	  a[i].clear();
    	  	  tt[i]=vis[i]=0;
		  }

    	  for (int i=0; i<n-1; i++){
    	  	  cin>>u>>v;
    	  	  a[u].pb(v);
    	  	  a[v].pb(u);

	      }

		  cin>>m;
		  for (int i=0; i<m; i++){
		  	  cin>>u;
		  	  b.pb(u);
		  }
		  sort(b.begin(),b.end());
		  reverse(b.begin(),b.end());

		  dfs(1); //求每个结点子孙数（构造以1为根的树）
		  /*for (int i=1; i<=n; i++)
		      cout<<tt[i]<<' ';
		 cout<<'\n'; */
		 //求每条路的访问次数
    	  for (int i=2; i<=n; i++){
    	  	  d.pb(((long long)(tt[i]+1)*(long long)(n-(tt[i]+1)))%M);
		  }

		  sort(d.begin(),d.end());
		  reverse(d.begin(),d.end());
		  /*for (int i=0; i<n-1; i++)
		      cout<<d[i]<<' ';
		  cout<<'\n';*/
		  if (m>(n-1)){
		     int x=1;
		  	 for (int j=1; j<=(m-n+1); j++)
		  	 	 b[0]=((long long)b[0]*(long long)(b[j]))%M;

		  	 ans=(ans+(long long)d[0]*(long long)b[0])%M;
		  	 for (int i=1; i<n-1; i++)
		  	     ans=(ans+d[i]*(long long)b[i+m-n+1])%M;
		  	 cout<<ans<<endl;
		  }
		  else{
		  	 for (int i=0; i<m; i++)
		  	     ans=(ans+d[i]*(long long)b[i])%M;
			 for (int i=m; i<n-1; i++)
		  	     ans=(ans+d[i])%M;
		  	 cout<<ans<<endl;
		  }
	}
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define M 1000000007
using namespace std;
int t,n,m,u,v,p;
vector<int> a[100005];
vector<long long> b;
long long vis[100005];
long long tt[100005],ans;
vector<long long> d;
void dfs(int k){
	 vis[k]=1;
	 for (int i=0; i<a[k].size(); i++)
	     if (!vis[a[k][i]]){
	     	dfs(a[k][i]);
	     	tt[k]+=tt[a[k][i]]+1;
		 }
}
int main(){
    cin>>t;
    while (t--){
    	  cin>>n;  d.clear(); ans=0; b.clear();
    	  for (int i=0; i<=n; i++){
    	  	  a[i].clear();
    	  	  tt[i+1]=vis[i+1]=0;
		  }

    	  for (int i=0; i<n-1; i++){
    	  	  cin>>u>>v;
    	  	  a[u].pb(v);
    	  	  a[v].pb(u);

	      }
		  cin>>m;
		  for (int i=0; i<m; i++){
		  	  cin>>u;
		  	  b.pb(u);
		  }
		  sort(b.begin(),b.end());
		  reverse(b.begin(),b.end());
		  vis[1]=1;
		  dfs(1);
		  /*for (int i=1; i<=n; i++)
		      cout<<tt[i]<<' ';
		 cout<<'\n'; */
    	  for (int i=2; i<=n; i++){
    	  	  d.pb(((long long)(tt[i]+1)*(long long)(n-(tt[i]+1)))%M);
		  }

		  sort(d.begin(),d.end());
		  reverse(d.begin(),d.end());
		  /*for (int i=0; i<n-1; i++)
		      cout<<d[i]<<' ';
		  cout<<'\n';*/
		  if (m>n-1){
		     int x=1;
		  	 for (int j=1; j<=(m-n+1); j++)
		  	 	 b[0]=((long long)b[0]*(long long)(b[j]))%M;

		  	 ans+=(d[0]*(long long)b[0])%M;
		  	 for (int i=1; i<n-1; i++)
		  	     ans=(ans+d[i]*(long long)b[i+m-n+1])%M;
		  	 cout<<ans<<endl;
		  }
		  else{
		  	 p=n-1-m;
		  	 for (int i=0; i<m; i++)
		  	     ans=(ans+d[i]*(long long)b[i])%M;
			 for (int i=m; i<n-1; i++)
		  	     ans=(ans+d[i])%M;
		  	 cout<<ans<<endl;
		  }
	}
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define M 1000000007
using namespace std;
int t,n,m,u,v,p;
vector<int> a[100001],b;
int vis[100001];
long long tt[100001],ans;
vector<long long> d;
void dfs(int k){
	 vis[k]=1;
	 for (int i=0; i<a[k].size(); i++)
	     if (!vis[a[k][i]]){
	     	dfs(a[k][i]);
	     	tt[k]+=tt[a[k][i]]+1;
		 }
}
int main(){
    cin>>t;
    while (t--){
    	  cin>>n;  d.clear(); ans=0; b.clear();
    	  for (int i=0; i<=n; i++){
    	  	  a[i].clear();
    	  	  tt[i+1]=vis[i+1]=0;
		  }

    	  for (int i=0; i<n-1; i++){
    	  	  cin>>u>>v;
    	  	  a[u].pb(v);
    	  	  a[v].pb(u);

	      }
		  cin>>m;
		  for (int i=0; i<m; i++){
		  	  cin>>u;
		  	  b.pb(u);
		  }
		  sort(b.begin(),b.end());
		  reverse(b.begin(),b.end());
		  vis[1]=1;
		  dfs(1);
		  /*for (int i=1; i<=n; i++)
		      cout<<tt[i]<<' ';
		 cout<<'\n'; */
    	  for (int i=2; i<=n; i++){
    	  	  d.pb(((long long)(tt[i]+1)*(long long)(n-(tt[i]+1)))%M);
		  }

		  sort(d.begin(),d.end());
		  reverse(d.begin(),d.end());
		  /*for (int i=0; i<n-1; i++)
		      cout<<d[i]<<' ';
		  cout<<'\n';*/
		  if (m>n-1){
		     int x=1;
		  	 for (int j=1; j<=(m-n+1); j++)
		  	 	 b[0]=((long long)b[0]*(long long)(b[j]))%M;

		  	 ans+=(d[0]*(long long)b[0])%M;
		  	 for (int i=1; i<n-1; i++)
		  	     ans=(ans+d[i]*(long long)b[i+m-n+1])%M;
		  	 cout<<ans<<endl;
		  }
		  else{
		  	 p=n-1-m;
		  	 for (int i=0; i<m; i++)
		  	     ans=(ans+d[i]*(long long)b[i])%M;
			 for (int i=m; i<n-1; i++)
		  	     ans=(ans+d[i])%M;
		  	 cout<<ans<<endl;
		  }
	}
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define M 1000000007
using namespace std;
int t,n,m,u,v,p;
vector<int> a[100005]; //邻接表存相邻结点
long long b[60002]; //存储所有的素因数
long long vis[100005]; //结点访问情况
long long tt[100005],ans; //tt：每个结点子孙数
long long d[100005]; //每条路径访问次数

void dfs(int k){
	 vis[k]=1;
	 for (int i=0; i<a[k].size(); i++)
	     if (!vis[a[k][i]]){
	     	dfs(a[k][i]);
	     	tt[k]=tt[k]+tt[a[k][i]]+1;
		 }
}

int main(){
    cin>>t;
    while (t--){
    	  cin>>n;
		  ans=0;
    	  for (int i=0; i<=n; i++){
    	  	  a[i].clear();
    	  	  tt[i]=vis[i]=d[i]=0;
		  }

    	  for (int i=0; i<n-1; i++){
    	  	  cin>>u>>v;
    	  	  a[u].pb(v);
    	  	  a[v].pb(u);

	      }

	      dfs(1); //求每个结点子孙数（构造以1为根的树）

		  cin>>m;
		  for (int i=0; i<m; i++){
		  	  cin>>b[i];
		  }
		  sort(b,b+m,greater<long long>());
		 /*for (int i=1; i<=n; i++)
		      cout<<tt[i]<<' ';
		 cout<<'\n';*/
		 //求每条路的访问次数
    	  for (int i=2; i<=n; i++){
    	  	  d[i-2]=((long long)(tt[i]+1)*(long long)(n-(tt[i]+1)))%M;
		  }

		  sort(d,d+n-1,greater<long long>());
		  /*for (int i=0; i<n-1; i++)
		      cout<<d[i]<<' ';
		  cout<<'\n';*/
		  if (m>(n-1)){
		     int x=1;
		  	 for (int j=1; j<=(m-n+1); j++)
		  	 	 b[0]=((long long)b[0]*(long long)(b[j]))%M;

		  	 ans=(ans+(long long)d[0]*(long long)b[0])%M;
		  	 for (int i=1; i<n-1; i++)
		  	     ans=(ans+(d[i]*b[i+m-n+1])%M)%M;
		  	 cout<<ans<<endl;
		  }
		  else{
		  	 for (int i=0; i<m; i++)
		  	     ans=(ans+(d[i]*b[i])%M)%M;
			 for (int i=m; i<n-1; i++)
		  	     ans=(ans+d[i])%M;
		  	 cout<<ans<<endl;
		  }
	}
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define M 1000000007
typedef long long ll;
using namespace std;
int t,n,m,u,v,p;
vector<int> a[100005]; //邻接表存相邻结点
int b[60002]; //存储所有的素因数
int vis[100005]; //结点访问情况
int tt[100005]; //tt：每个结点子孙数
int d[100005]; //每条路径访问次数
ll ans,tmp;
void dfs(int k){
	 vis[k]=1;
	 for (int i=0; i<a[k].size(); i++)
	     if (!vis[a[k][i]]){
	     	dfs(a[k][i]);
	     	tt[k]+=tt[a[k][i]]+1;
		 }
}

int main(){
    cin>>t;
    while (t--){
    	  cin>>n;
		  ans=0;
    	  for (int i=0; i<=n; i++){
    	  	  a[i].clear();
    	  	  tt[i]=vis[i]=d[i]=0;
		  }

    	  for (int i=0; i<n-1; i++){
    	  	  cin>>u>>v;
    	  	  a[u].pb(v);
    	  	  a[v].pb(u);

	      }

	      dfs(1); //求每个结点子孙数（构造以1为根的树）

		  cin>>m;
		  for (int i=0; i<m; i++){
		  	  cin>>b[i];
		  }
		  sort(b,b+m,greater<ll>());
		 /*for (int i=1; i<=n; i++)
		      cout<<tt[i]<<' ';
		 cout<<'\n';*/
		 //求每条路的访问次数
    	  for (int i=2; i<=n; i++){
    	  	  d[i-2]=((ll)(tt[i]+1)*(n-(tt[i]+1)))%M;
		  }

		  sort(d,d+n-1,greater<ll>());
		  /*for (int i=0; i<n-1; i++)
		      cout<<d[i]<<' ';
		  cout<<'\n';*/
		  if (m>(n-1)){
		     int x=1;
		     tmp=b[0];
		  	 for (int j=1; j<=(m-n+1); j++)
		  	 	 tmp=((ll)tmp*(ll)(b[j]))%M;

		  	 ans=(ans+(ll)d[0]*tmp)%M;
		  	 for (int i=1; i<n-1; i++)
		  	     ans=(ans+(ll)d[i]*(ll)b[i+m-n+1])%M;
		  	 cout<<ans<<endl;
		  }
		  else{
		  	 for (int i=0; i<m; i++)
		  	     ans=(ans+(ll)d[i]*b[i])%M;
			 for (int i=m; i<n-1; i++)
		  	     ans=(ans+d[i])%M;
		  	 cout<<ans<<endl;
		  }
	}
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define M 1000000007
using namespace std;
int t,n,m,u,v,p;
vector<int> a[100005]; //邻接表存相邻结点
long long b[60002]; //存储所有的素因数
long long vis[100005]; //结点访问情况
long long tt[100005],ans; //tt：每个结点子孙数
long long d[100005]; //每条路径访问次数

void dfs(int k){
	 vis[k]=1;
	 for (int i=0; i<a[k].size(); i++)
	     if (!vis[a[k][i]]){
	     	dfs(a[k][i]);
	     	tt[k]+=tt[a[k][i]]+1;
		 }
}

int main(){
    cin>>t;
    while (t--){
    	  cin>>n;
		  ans=0;
    	  for (int i=0; i<=n; i++){
    	  	  a[i].clear();
    	  	  tt[i]=vis[i]=d[i]=0;
		  }

    	  for (int i=0; i<n-1; i++){
    	  	  cin>>u>>v;
    	  	  a[u].pb(v);
    	  	  a[v].pb(u);

	      }

		  cin>>m;
		  for (int i=0; i<m; i++){
		  	  cin>>b[i];
		  }
		  sort(b,b+m,greater<long long>());

		  dfs(1); //求每个结点子孙数（构造以1为根的树）
		 /*for (int i=1; i<=n; i++)
		      cout<<tt[i]<<' ';
		 cout<<'\n';*/
		 //求每条路的访问次数
    	  for (int i=2; i<=n; i++){
    	  	  d[i-2]=((long long)(tt[i]+1)*(long long)(n-(tt[i]+1)))%M;
		  }

		  sort(d,d+n-2,greater<long long>());
		  /*for (int i=0; i<n-1; i++)
		      cout<<d[i]<<' ';
		  cout<<'\n';*/
		  if (m>(n-1)){
		     int x=1;
		  	 for (int j=1; j<=(m-n+1); j++)
		  	 	 b[0]=((long long)b[0]*(long long)(b[j]))%M;

		  	 ans=(ans+(long long)d[0]*(long long)b[0])%M;
		  	 for (int i=1; i<n-1; i++)
		  	     ans=(ans+(long long)d[i]*b[i+m-n+1])%M;
		  	 cout<<ans<<endl;
		  }
		  else{
		  	 for (int i=0; i<m; i++)
		  	     ans=(ans+(long long)d[i]*b[i])%M;
			 for (int i=m; i<n-1; i++)
		  	     ans=(ans+d[i])%M;
		  	 cout<<ans<<endl;
		  }
	}
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define M 1000000007
using namespace std;
int t,n,m,u,v,p;
vector<int> a[100001];
vector<long long> b;
int vis[100001];
long long tt[100001],ans;
vector<long long> d;
void dfs(int k){
	 vis[k]=1;
	 for (int i=0; i<a[k].size(); i++)
	     if (!vis[a[k][i]]){
	     	dfs(a[k][i]);
	     	tt[k]+=tt[a[k][i]]+1;
		 }
}
int main(){
    cin>>t;
    while (t--){
    	  cin>>n;  d.clear(); ans=0; b.clear();
    	  for (int i=0; i<=n; i++){
    	  	  a[i].clear();
    	  	  tt[i+1]=vis[i+1]=0;
		  }

    	  for (int i=0; i<n-1; i++){
    	  	  cin>>u>>v;
    	  	  a[u].pb(v);
    	  	  a[v].pb(u);

	      }
		  cin>>m;
		  for (int i=0; i<m; i++){
		  	  cin>>u;
		  	  b.pb(u);
		  }
		  sort(b.begin(),b.end());
		  reverse(b.begin(),b.end());
		  vis[1]=1;
		  dfs(1);
		  /*for (int i=1; i<=n; i++)
		      cout<<tt[i]<<' ';
		 cout<<'\n'; */
    	  for (int i=2; i<=n; i++){
    	  	  d.pb(((long long)(tt[i]+1)*(long long)(n-(tt[i]+1)))%M);
		  }

		  sort(d.begin(),d.end());
		  reverse(d.begin(),d.end());
		  /*for (int i=0; i<n-1; i++)
		      cout<<d[i]<<' ';
		  cout<<'\n';*/
		  if (m>n-1){
		     int x=1;
		  	 for (int j=1; j<=(m-n+1); j++)
		  	 	 b[0]=((long long)b[0]*(long long)(b[j]))%M;

		  	 ans+=(d[0]*(long long)b[0])%M;
		  	 for (int i=1; i<n-1; i++)
		  	     ans=(ans+d[i]*(long long)b[i+m-n+1])%M;
		  	 cout<<ans<<endl;
		  }
		  else{
		  	 p=n-1-m;
		  	 for (int i=0; i<m; i++)
		  	     ans=(ans+d[i]*(long long)b[i])%M;
			 for (int i=m; i<n-1; i++)
		  	     ans=(ans+d[i])%M;
		  	 cout<<ans<<endl;
		  }
	}
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define M 1000000007
using namespace std;
int t,n,m,u,v,p;
vector<int> a[100005]; //邻接表存相邻结点
vector<long long> b; //存储所有的素因数
long long vis[100005]; //结点访问情况
long long tt[100005],ans; //tt：每个结点子孙数
vector<long long> d; //每条路径访问次数

void dfs(int k){
	 vis[k]=1;
	 for (int i=0; i<a[k].size(); i++)
	     if (!vis[a[k][i]]){
	     	dfs(a[k][i]);
	     	tt[k]+=tt[a[k][i]]+1;
		 }
}

int main(){
    cin>>t;
    while (t--){
    	  cin>>n;
		  d.clear(); b.clear();
		  ans=0;
    	  for (int i=0; i<=n; i++){
    	  	  a[i].clear();
    	  	  tt[i]=vis[i]=0;
		  }

    	  for (int i=0; i<n-1; i++){
    	  	  cin>>u>>v;
    	  	  a[u].pb(v);
    	  	  a[v].pb(u);

	      }

		  cin>>m;
		  for (int i=0; i<m; i++){
		  	  cin>>u;
		  	  b.pb(u);
		  }
		  sort(b.begin(),b.end());
		  reverse(b.begin(),b.end());

		  dfs(1); //求每个结点子孙数（构造以1为根的树）
		  /*for (int i=1; i<=n; i++)
		      cout<<tt[i]<<' ';
		 cout<<'\n'; */
		 //求每条路的访问次数
    	  for (int i=2; i<=n; i++){
    	  	  d.pb(((long long)(tt[i]+1)*(long long)(n-(tt[i]+1)))%M);
		  }

		  sort(d.begin(),d.end());
		  reverse(d.begin(),d.end());
		  /*for (int i=0; i<n-1; i++)
		      cout<<d[i]<<' ';
		  cout<<'\n';*/
		  if (m>(n-1)){
		     int x=1;
		  	 for (int j=1; j<=(m-n+1); j++)
		  	 	 b[0]=((long long)b[0]*(long long)(b[j]))%M;

		  	 ans=(ans+(long long)d[0]*(long long)b[0])%M;
		  	 for (int i=1; i<n-1; i++)
		  	     ans=(ans+(long long)d[i]*b[i+m-n+1])%M;
		  	 cout<<ans<<endl;
		  }
		  else{
		  	 for (int i=0; i<m; i++)
		  	     ans=(ans+(long long)d[i]*b[i])%M;
			 for (int i=m; i<n-1; i++)
		  	     ans=(ans+d[i])%M;
		  	 cout<<ans<<endl;
		  }
	}
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define M 1000000007
using namespace std;
int t,n,m,u,v,p;
vector<int> a[100001],b;
int vis[100001];
long long tt[100001],ans;
vector<long long> d;
void dfs(int k){
	 vis[k]=1;
	 for (int i=0; i<a[k].size(); i++)
	     if (!vis[a[k][i]]){
	     	dfs(a[k][i]);
	     	tt[k]+=tt[a[k][i]]+1;
		 }
}
int main(){
    cin>>t;
    while (t--){
    	  cin>>n;  d.clear(); ans=0;
    	  for (int i=0; i<n; i++){
    	  	  a[i].clear();
    	  	  tt[i+1]=vis[i+1]=0;
		  }

    	  for (int i=0; i<n-1; i++){
    	  	  cin>>u>>v;
    	  	  a[u].pb(v);
    	  	  a[v].pb(u);

	      }
		  cin>>m;
		  for (int i=0; i<m; i++){
		  	  cin>>u;
		  	  b.pb(u);
		  }
		  sort(b.begin(),b.end());
		  reverse(b.begin(),b.end());
		  vis[1]=1;
		  dfs(1);
		  /*for (int i=1; i<=n; i++)
		      cout<<tt[i]<<' ';
		 cout<<'\n'; */
    	  for (int i=2; i<=n; i++){
    	  	  d.pb(((long long)(tt[i]+1)*(long long)(n-(tt[i]+1)))%M);
		  }

		  sort(d.begin(),d.end());
		  reverse(d.begin(),d.end());
		  /*for (int i=0; i<n-1; i++)
		      cout<<d[i]<<' ';
		  cout<<'\n';*/
		  if (m>n-1){
		     int x=1;
		  	 for (int j=1; j<=(m-n+1); j++)
		  	 	 b[0]=((long long)b[0]*(long long)(b[j]))%M;

		  	 ans+=(d[0]*(long long)b[0])%M;
		  	 for (int i=1; i<n-1; i++)
		  	     ans+=(d[i]*(long long)b[i+m-n+1])%M;
		  	 cout<<ans<<endl;
		  }
		  else{
		  	 p=n-1-m;
		  	 for (int i=0; i<m; i++)
		  	     ans+=(d[i]*(long long)b[i])%M;
			 for (int i=m; i<n-1; i++)
		  	     ans+=d[i];
		  	 cout<<ans<<endl;
		  }
	}
	return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 200000+5;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
        }
    }
    return 0;
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        memset(p,0,sizeof(p));
        memset(sum,0,sizeof(sum));
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]));
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        sort(p+1,p+m+1);
        for(int i=n;i<=m;i++){
            p[n-1] *= p[i];
            p[n-1] %= mod;
        }
        sort(p+1,p+n,cmp);
        for(int i=n;i<=m;i++){
            p[1] *= p[i];
            p[1] %= mod;
        }
        for(int i=1;i<n;i++){
            sum[i] %= mod;
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += (p[i]*sum[i])%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 200000 + 5;
const int mod = 1e9+7;

vector <int> G[maxn];
long long p[maxn];
long long s[maxn];
long long q[maxn];

void dfs(int x){
    s[x] = 1;
    for(int i=0;i<G[x].size();i++){
        int y = G[x][i];
        if(!s[y]){
            dfs(y);
            s[x] += s[y];
            // cout << "d " << x << " " << y << endl;
        }
    }
}

int main(){
    int t;
    cin >> t;
    while(t--){
        memset(s,0,sizeof(s));
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<=n-1;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            G[x].push_back(y);
            G[y].push_back(x);
        }
        dfs(1);
        // for(int i=1;i<=n;i++){
        //     printf("s %d\n",s[i]);
        // }
        int m;
        scanf("%d",&m);
        for(int i=1;i<=n-1;i++){
            p[i] = 1;
        }
        for(int i=1;i<=m;i++){
            scanf("%d",&p[i]);
        }
        sort(p+1,p+n);
        // for(int i=1;i<=m;i++){
        //     printf("p %d\n",p[i]);
        // }
        for(int i=n;i<=m;i++){
            p[n-1] *= p[i];
            p[n-1] %= mod;
        }
        for(int i=1;i<=n;i++){
            q[i] = (n-s[i+1])*s[i+1];
        }
        sort(q+1,q+n);
        long long ans = 0;
        for(int i=1;i<n;i++){
            // cout << "d " << q[i] << " " << p[i] << endl;
            q[i] %= mod;
            long long now = (q[i] * p[i])%mod;
            ans += now;
            ans %= mod;
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 200000+5;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
        }
    }
    return 0;
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        memset(p,0,sizeof(p));
        memset(sum,0,sizeof(sum));
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]));
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        if(m > n-1){
            sort(p+1,p+m+1);
            for(int i=n;i<=m;i++){
                p[n-1] *= p[i];
                p[n-1] %= mod;
            }
            p[n] = p[n-1];
            sort(p+1,p+n-1,cmp);
            for(int i=n-2;i>=1;i--){
                p[i+1] = p[i];
            }
            p[1] = p[n];
        }else{
            sort(p+1,p+m+1,cmp);
        }
        for(int i=1;i<n;i++){
            sum[i] %= mod;
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += (p[i]*sum[i])%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 200000+5;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
        }
    }
    return 0;
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        memset(p,0,sizeof(p));
        memset(sum,0,sizeof(sum));
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]));
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        sort(p+1,p+m+1);
        for(int i=n;i<=m;i++){
            p[n-1] *= p[i];
            p[n-1] %= mod;
        }
        sort(p+1,p+n-1,cmp);
        p[n] = p[n-1];
        for(int i=1;i<=n-2;i++){
            p[i+1] = p[i];
        }
        p[1] = p[n];
        for(int i=1;i<n;i++){
            sum[i] %= mod;
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += (p[i]*sum[i])%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 100000;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
            sum[x] %= mod;
        }
    }
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]))%mod;
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        sort(p+1,p+m+1,cmp);
        for(int i=1;i<=n;i++){
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += (p[i]*sum[i])%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 200000 + 5;
const int mod = 1e9+7;

vector <int> G[maxn];
long long p[maxn];
long long s[maxn];
long long q[maxn];

void dfs(int x){
    s[x] = 1;
    for(int i=0;i<G[x].size();i++){
        int y = G[x][i];
        if(!s[y]){
            dfs(y);
            s[x] += s[y];
            // cout << "d " << x << " " << y << endl;
        }
    }
}

int main(){
    int t;
    cin >> t;
    while(t--){
        memset(s,0,sizeof(s));
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<=n-1;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            G[x].push_back(y);
            G[y].push_back(x);
        }
        dfs(1);
        // for(int i=1;i<=n;i++){
        //     printf("s %d\n",s[i]);
        // }
        int m;
        scanf("%d",&m);
        for(int i=1;i<=n-1;i++){
            p[i] = 1;
        }
        for(int i=1;i<=m;i++){
            scanf("%d",&p[i]);
        }
        sort(p+1,p+m+1);
        // for(int i=1;i<=m;i++){
        //     printf("p %d\n",p[i]);
        // }
        for(int i=n;i<=m;i++){
            p[n-1] *= p[i];
            p[n-1] %= mod;
        }
        for(int i=1;i<=n;i++){
            q[i] = (n-s[i+1])*s[i+1];
        }
        sort(q+1,q+n);
        sort(p+1,p+n);
        long long ans = 0;
        for(int i=1;i<n;i++){
            // cout << "d " << q[i] << " " << p[i] << endl;
            q[i] %= mod;
            long long now = (q[i] * p[i])%mod;
            ans += now;
            ans %= mod;
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 200000+5;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
        }
    }
    return 0;
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        memset(p,0,sizeof(p));
        memset(sum,0,sizeof(sum));
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]));
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        sort(p+1,p+m+1);
        for(int i=n;i<=n;i++){
            p[n-1] *= p[i];
            p[n-1] %= mod;
        }
        sort(p+1,p+n,cmp);
        for(int i=n;i<=m;i++){
            p[1] *= p[i];
            p[1] %= mod;
        }
        for(int i=1;i<n;i++){
            sum[i] %= mod;
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += (p[i]*sum[i])%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 100000;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
            sum[x] %= mod;
        }
    }
    return 0;
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]))%mod;
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        sort(p+1,p+m+1,cmp);
        for(int i=1;i<=n;i++){
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += (p[i]*sum[i])%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 200000;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
        }
    }
    return 0;
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        memset(p,0,sizeof(p));
        memset(sum,0,sizeof(sum));
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]));
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        sort(p+1,p+m+1,cmp);
        for(int i=n;i<=m;i++){
            p[1] *= p[i];
            p[1] %= mod;
        }
        for(int i=1;i<n;i++){
            sum[i] %= mod;
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += (p[i]*sum[i])%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 100000;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
            sum[x]%=mod;
        }
    }
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]))%mod;
            sum[i] %= mod;
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        sort(p+1,p+m+1,cmp);
        for(int i=1;i<=n;i++){
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += p[i]*sum[i]%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 100000;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
        }
    }
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = sum[i]*(n-sum[i]);
            sum[i] %= mod;
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        sort(p+1,p+m+1,cmp);
        for(int i=1;i<=n;i++){
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += p[i]*sum[i]%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 200000+5;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
        }
    }
    return 0;
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        memset(p,0,sizeof(p));
        memset(sum,0,sizeof(sum));
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]));
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        sort(p+1,p+m+1);
        if(m > n-1){
            for(int i=n;i<=m;i++){
                p[n-1] *= p[i];
                p[n-1] %= mod;
            }
            sort(p+1,p+n-1,cmp);
            p[n] = p[n-1];
            for(int i=1;i<=n-2;i++){
                p[i+1] = p[i];
            }
            p[1] = p[n];
        }
        for(int i=1;i<n;i++){
            sum[i] %= mod;
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += (p[i]*sum[i])%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 200000;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
            sum[x] %= mod;
        }
    }
    return 0;
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        memset(p,0,sizeof(p));
        memset(sum,0,sizeof(sum));
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]));
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        sort(p+1,p+m+1,cmp);
        for(int i=n;i<=m;i++){
            p[1] *= p[i];
            p[1] %= mod;
        }
        for(int i=1;i<n;i++){
            sum %= mod;
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += (p[i]*sum[i])%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 200000;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
            sum[x] %= mod;
        }
    }
    return 0;
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        memset(p,0,sizeof(p));
        memset(sum,0,sizeof(sum));
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]));
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        sort(p+1,p+m+1,cmp);
        for(int i=n;i<=m;i++){
            p[1] *= p[i];
            p[1] %= mod;
        }
        for(int i=1;i<n;i++){
            sum[i] %= mod;
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += (p[i]*sum[i])%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 200000;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
            sum[x] %= mod;
        }
    }
    return 0;
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        memset(p,0,sizeof(p));
        memset(sum,0,sizeof(sum));
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]))%mod;
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        sort(p+1,p+m+1,cmp);
        for(int i=1;i<=n;i++){
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += (p[i]*sum[i])%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 200000+5;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
        }
    }
    return 0;
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        memset(p,0,sizeof(p));
        memset(sum,0,sizeof(sum));
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]));
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        sort(p+1,p+m+1);
        for(int i=n;i<=m;i++){
            p[n-1] *= p[i];
            p[n-1] %= mod;
        }
        sort(p+1,p+n,cmp);
        for(int i=1;i<n;i++){
            sum[i] %= mod;
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += (p[i]*sum[i])%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 200000;
const int mod = 1e9 + 7;
vector <int> G[maxn];

int sum[maxn];
int p[maxn];

int dfs(int x,int fa){
    sum[x] = 1;
//    cout << "d " << x << endl;
    if(G[x].size() == 1 && x!=1){
        return 1;
    }
    for(auto it:G[x]){
        if(it != fa){
            dfs(it,x);
            sum[x] += sum[it];
            sum[x] %= mod;
        }
    }
    return 0;
}
bool cmp(int x,int y){
    return x > y;
}

signed main()
{
    int t;
    cin >> t;
    while(t--){
        memset(p,0,sizeof(p));
        memset(sum,0,sizeof(sum));
        int n;
        cin >> n;
        for(int i=1;i<=n;i++){
            G[i].clear();
        }
        for(int i=1;i<n;i++){
            int x,y;
            cin >> x >> y;
            G[x].push_back(y);
            G[y].push_back(x);
        }
        int m;
        cin >> m;
        for(int i=1;i<=m;i++){
            scanf("%lld",&p[i]);
        }
        dfs(1,-1);
        for(int i=1;i<=n;i++){
            sum[i] = (sum[i]*(n-sum[i]))%mod;
        }
        int ans = 0;
        sort(sum+1,sum+n+1,cmp);
        sort(p+1,p+m+1,cmp);
        for(int i=n;i<=m;i++){
            p[1] *= p[i];
            p[1] %= mod;
        }
        for(int i=1;i<n;i++){
            if(p[i]==0){
                ans += sum[i];
                ans %= mod;
            }else{
                ans += (p[i]*sum[i])%mod;
                ans %= mod;
            }
        }
        cout << ans << endl;
    }
    return 0;
}