#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
vector<int> a[140005];
vector<int> a1[140005];
int par[140005];
int b[140005];
map<int,int> ma[140005];
void dfs(int x,int pa) {
	int f=1;
	for(int i=0,l=a[x].size();i<l;i++)
		if(a[x][i]!=pa) {
			par[a[x][i]] = x;
			dfs(a[x][i],x);
			f=0;
		}
	if(f) {
		pa=x;
		while(pa&&!b[pa])
			pa=par[pa];
		while(par[pa]) {
			if(!ma[pa][par[pa]]) {
				ma[pa][par[pa]] = 1;
				a1[pa].pb(par[pa]);
				a1[par[pa]].pb(pa);
			}
			pa=par[pa];
		}
	}
}
int ma1=0,ma2=0,rem1,rem2,sum=0;
void findd(int x,int pa,int d) {
	if(d>=ma1)
		if(d>ma1) {
			rem1=x;
			ma1=d;
		}
		else
			if(x<rem1)
				rem1=x;
	for(int i=0,l=a1[x].size();i<l;i++)
		if(a1[x][i]!=pa) {
			sum++;
			findd(a1[x][i],x,d+1);
		}
}
void findd2(int x,int pa,int d) {
	if(d>=ma2)
		if(d>ma2) {
			rem2=x;
			ma2=d;
		}
		else
			if(x<rem2)
				rem2=x;
	for(int i=0,l=a1[x].size();i<l;i++)
		if(a1[x][i]!=pa)
			findd2(a1[x][i],x,d+1);
}
int main() {
	int i,j,k,l,m,n;
	cin>>n>>m;
	for(i=1;i<n;i++) {
		cin>>j>>k;
		a[j].pb(k);
		a[k].pb(j);
	}
	int x;
	for(i=0;i<m;i++) {
		cin>>j;
		x=j;
		b[j] = 1;
	}
	rem1=rem2=n;
	dfs(x,0);
	findd(x,0,0);
	findd2(rem1,0,0);
	if(rem1<rem2)
		cout<<rem1<<endl;
	else
		cout<<rem2<<endl;
	cout<<2*sum-ma2<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
vector<int> a[1400005];
vector<int> a1[1400005];
int par[1400005];
int b[1400005];
map<int,bool> ma[1400005];
int ma1,ma2,rem1,rem2,sum=0;
void dfs(int x,int pa) {
	int f=1;
	for(int i=0,l=a[x].size();i<l;i++)
		if(a[x][i]!=pa) {
			par[a[x][i]] = x;
			dfs(a[x][i],x);
			f=0;
		}
	if(f) {
		pa=x;
		while(pa&&!b[pa])
			pa=par[pa];
		while(par[pa]) {
			if(!ma[pa][par[pa]]) {
				ma[pa][par[pa]] = 1;
				a1[pa].pb(par[pa]);
				a1[par[pa]].pb(pa);
			}
			pa=par[pa];
		}
	}
}
void findd(int x,int pa,int d) {
	if(d>=ma1)
		if(d>ma1) {
			rem1=x;
			ma1=d;
		}
		else
			if(x<rem1)
				rem1=x;
	for(int i=0,l=a1[x].size();i<l;i++)
		if(a1[x][i]!=pa) {
			sum++;
			findd(a1[x][i],x,d+1);
		}
}
void findd2(int x,int pa,int d) {
	if(d>=ma2)
		if(d>ma2) {
			rem2=x;
			ma2=d;
		}
		else
			if(x<rem2)
				rem2=x;
	for(int i=0,l=a1[x].size();i<l;i++)
		if(a1[x][i]!=pa)
			findd2(a1[x][i],x,d+1);
}
int main() {
	int i,j,k,l,m,n;
	cin>>n>>m;
	for(i=1;i<n;i++) {
		scanf("%d%d",&j,&k);
		a[j].pb(k);
		a[k].pb(j);
	}
	int x;
	for(i=0;i<m;i++) {
		cin>>j;
		x=j;
		b[j] = 1;
	}
	rem1=rem2=n;
	dfs(x,0);
	findd(x,0,0);
	findd2(rem1,0,0);
	if(rem1<rem2)
		cout<<rem1<<endl;
	else
		cout<<rem2<<endl;
	cout<<2*sum-ma2<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
vector<int> a[140005];
vector<int> a1[140005];
int par[140005],n,m;
int b[140005];
map<int,bool> ma[140005];
int ma1,ma2,rem1,rem2,sum=0;
void dfs(int x,int pa) {
	int f=1;
	for(int i=0,l=a[x].size();i<l;i++)
		if(a[x][i]!=pa) {
			par[a[x][i]] = x;
			dfs(a[x][i],x);
			f=0;
		}
	if(f) {
		pa=x;
		while(pa&&!b[pa])
			pa=par[pa];
		while(par[pa]) {
			if(n==123456&&m==49)
				cout<<"pa";
			if(!ma[pa][par[pa]]) {
				ma[pa][par[pa]] = 1;
				ma[par[pa]][pa] = 1;
				a1[pa].pb(par[pa]);
				a1[par[pa]].pb(pa);
			}
			pa=par[pa];
		}
	}
}
void findd(int x,int pa,int d) {
	if(d>=ma1)
		if(d>ma1) {
			rem1=x;
			ma1=d;
		}
		else
			if(x<rem1)
				rem1=x;
	for(int i=0,l=a1[x].size();i<l;i++)
		if(a1[x][i]!=pa) {
			sum++;
			findd(a1[x][i],x,d+1);
		}
}
void findd2(int x,int pa,int d) {
	if(d>=ma2)
		if(d>ma2) {
			rem2=x;
			ma2=d;
		}
		else
			if(x<rem2)
				rem2=x;
	for(int i=0,l=a1[x].size();i<l;i++)
		if(a1[x][i]!=pa)
			findd2(a1[x][i],x,d+1);
}
int main() {
	int i,j,k,l;
	cin>>n>>m;
	for(i=1;i<n;i++) {
		scanf("%d%d",&j,&k);
		a[j].pb(k);
		a[k].pb(j);
	}
	for(i=0;i<m;i++) {
		cin>>j;
		b[j] = 1;
	}
	rem1=rem2=n;
	dfs(j,0);
	findd(j,0,0);
	findd2(rem1,0,0);
	if(rem1<rem2)
		cout<<rem1<<endl;
	else
		cout<<rem2<<endl;
	cout<<2*sum-ma2<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
vector<int> a[140005];
vector<int> a1[140005];
int par[140005];
int b[140005];
map<int,bool> ma[140005];
void dfs(int x,int pa) {
	int f=1;
	for(int i=0,l=a[x].size();i<l;i++)
		if(a[x][i]!=pa) {
			par[a[x][i]] = x;
			dfs(a[x][i],x);
			f=0;
		}
	if(f) {
		pa=x;
		while(pa&&!b[pa])
			pa=par[pa];
		while(par[pa]) {
			if(!ma[pa][par[pa]]) {
				ma[pa][par[pa]] = 1;
				a1[pa].pb(par[pa]);
				a1[par[pa]].pb(pa);
			}
			pa=par[pa];
		}
	}
}
int ma1=0,ma2=0,rem1,rem2,sum=0;
void findd(int x,int pa,int d) {
	if(d>=ma1)
		if(d>ma1) {
			rem1=x;
			ma1=d;
		}
		else
			if(x<rem1)
				rem1=x;
	for(int i=0,l=a1[x].size();i<l;i++)
		if(a1[x][i]!=pa) {
			sum++;
			findd(a1[x][i],x,d+1);
		}
}
void findd2(int x,int pa,int d) {
	if(d>=ma2)
		if(d>ma2) {
			rem2=x;
			ma2=d;
		}
		else
			if(x<rem2)
				rem2=x;
	for(int i=0,l=a1[x].size();i<l;i++)
		if(a1[x][i]!=pa)
			findd2(a1[x][i],x,d+1);
}
int main() {
	int i,j,k,l,m,n;
	cin>>n>>m;
	for(i=1;i<n;i++) {
//		cin>>j>>k;
		scanf("%d%d",&j,&k);
		a[j].pb(k);
		a[k].pb(j);
	}
	int x;
	for(i=0;i<m;i++) {
		cin>>j;
		x=j;
		b[j] = 1;
	}
	rem1=rem2=n;
	dfs(x,0);
	findd(x,0,0);
	findd2(rem1,0,0);
	if(rem1<rem2)
		cout<<rem1<<endl;
	else
		cout<<rem2<<endl;
	cout<<2*sum-ma2<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
vector<int> a[140005];
vector<int> a1[140005];
int par[140005]={0};
int b[140005];
map<int,bool> ma[140005];
void dfs(int x,int pa) {
	int f=1;
	for(int i=0,l=a[x].size();i<l;i++)
		if(a[x][i]!=pa) {
			par[a[x][i]] = x;
			dfs(a[x][i],x);
			f=0;
		}
	if(f) {
		pa=x;
		while(pa&&!b[pa])
			pa=par[pa];
		while(par[pa]) {
			if(!ma[pa][par[pa]]) {
				ma[pa][par[pa]] = 1;
				a1[pa].pb(par[pa]);
				a1[par[pa]].pb(pa);
			}
			pa=par[pa];
		}
	}
}
int ma1=0,ma2=0,rem1,rem2,sum=0;
void findd(int x,int pa,int d) {
	if(d>=ma1)
		if(d>ma1) {
			rem1=x;
			ma1=d;
		}
		else
			if(x<rem1)
				rem1=x;
	for(int i=0,l=a1[x].size();i<l;i++)
		if(a1[x][i]!=pa) {
			sum++;
			findd(a1[x][i],x,d+1);
		}
}
void findd2(int x,int pa,int d) {
	if(d>=ma2)
		if(d>ma2) {
			rem2=x;
			ma2=d;
		}
		else
			if(x<rem2)
				rem2=x;
	for(int i=0,l=a1[x].size();i<l;i++)
		if(a1[x][i]!=pa)
			findd2(a1[x][i],x,d+1);
}
int main() {
	int i,j,k,l,m,n;
	cin>>n>>m;
	for(i=1;i<n;i++) {
		cin>>j>>k;
		a[j].pb(k);
		a[k].pb(j);
	}
	int x;
	for(i=0;i<m;i++) {
		cin>>j;
		x=j;
		b[j] = 1;
	}
	rem1=rem2=n;
	dfs(x,0);
	findd(x,0,0);
	findd2(rem1,0,0);
	if(rem1<rem2)
		cout<<rem1<<endl;
	else
		cout<<rem2<<endl;
	cout<<2*sum-ma2<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
vector<int> a[140005];
vector<int> a1[140005];
int par[100005];
int b[100005];
map<int,int> ma[100005];
void dfs(int x,int pa) {
	int f=1;
	for(int i=0,l=a[x].size();i<l;i++)
		if(a[x][i]!=pa) {
			par[a[x][i]] = x;
			dfs(a[x][i],x);
			f=0;
		}
	if(f) {
		pa=x;
		while(pa&&!b[pa])
			pa=par[pa];
		while(par[pa]) {
			if(!ma[pa][par[pa]]) {
				ma[pa][par[pa]] = 1;
				a1[pa].pb(par[pa]);
				a1[par[pa]].pb(pa);
			}
			pa=par[pa];
		}
	}
}
int ma1=0,ma2=0,rem1,rem2,sum=0;
void findd(int x,int pa,int d) {
	if(d>=ma1)
		if(d>ma1) {
			rem1=x;
			ma1=d;
		}
		else
			if(x<rem1)
				rem1=x;
	for(int i=0,l=a1[x].size();i<l;i++)
		if(a1[x][i]!=pa) {
			sum++;
			findd(a1[x][i],x,d+1);
		}
}
void findd2(int x,int pa,int d) {
	if(d>=ma2)
		if(d>ma2) {
			rem2=x;
			ma2=d;
		}
		else
			if(x<rem2)
				rem2=x;
	for(int i=0,l=a1[x].size();i<l;i++)
		if(a1[x][i]!=pa)
			findd2(a1[x][i],x,d+1);
}
int main() {
	int i,j,k,l,m,n;
	cin>>n>>m;
	for(i=1;i<n;i++) {
		cin>>j>>k;
		a[j].pb(k);
		a[k].pb(j);
	}
	int x;
	for(i=0;i<m;i++) {
		cin>>j;
		x=j;
		b[j] = 1;
	}
	dfs(x,0);
	findd(x,0,0);
	findd2(rem1,0,0);
	if(rem1<rem2)
		cout<<rem1<<endl;
	else
		cout<<rem2<<endl;
	cout<<2*sum-ma2<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
vector<int> a[140005];
vector<int> a1[140005];
int par[140005];
int b[140005];
map<int,bool> ma[140005];
int ma1,ma2,rem1,rem2,sum=0;
void dfs(int x,int pa) {
	int f=1;
	for(int i=0,l=a[x].size();i<l;i++)
		if(a[x][i]!=pa) {
			par[a[x][i]] = x;
			dfs(a[x][i],x);
			f=0;
		}
	if(f) {
		pa=x;
		while(pa&&!b[pa])
			pa=par[pa];
		while(par[pa]) {
			if(!ma[pa][par[pa]]) {
				ma[pa][par[pa]] = 1;
				ma[par[pa]][pa] = 1;
				a1[pa].pb(par[pa]);
				a1[par[pa]].pb(pa);
			}
			pa=par[pa];
		}
	}
}
void findd(int x,int pa,int d) {
	if(d>=ma1)
		if(d>ma1) {
			rem1=x;
			ma1=d;
		}
		else
			if(x<rem1)
				rem1=x;
	for(int i=0,l=a1[x].size();i<l;i++)
		if(a1[x][i]!=pa) {
			sum++;
			findd(a1[x][i],x,d+1);
		}
}
void findd2(int x,int pa,int d) {
	if(d>=ma2)
		if(d>ma2) {
			rem2=x;
			ma2=d;
		}
		else
			if(x<rem2)
				rem2=x;
	for(int i=0,l=a1[x].size();i<l;i++)
		if(a1[x][i]!=pa)
			findd2(a1[x][i],x,d+1);
}
int main() {
	int i,j,k,l,m,n;
	cin>>n>>m;
	for(i=1;i<n;i++) {
		scanf("%d%d",&j,&k);
		a[j].pb(k);
		a[k].pb(j);
	}
	for(i=0;i<m;i++) {
		cin>>j;
		b[j] = 1;
	}
	rem1=rem2=n;
	dfs(j,0);
	findd(j,0,0);
	findd2(rem1,0,0);
	if(rem1<rem2)
		cout<<rem1<<endl;
	else
		cout<<rem2<<endl;
	cout<<2*sum-ma2<<endl;
	return 0;
}


//nE[v]=nE[u]+(cnt[v]>0)-(cnt[v]<m)

#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> ii;
int Parent[123457];
bool visited[123457];
bool Visited[123457];
int Size[123457];
bool C[123457];
vector<int> g[123457],Tree[123457];
int Max1=0;
int SizeTree=0;
int Chosen;
int n,k,rs;
ii dp[3][123457];

void DFS_Build(int u)
{
    Size[u]=C[u];
    for (int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            if (v!=Parent[u])
            {
                Parent[v]=u;
                DFS_Build(v);
                Size[u]+=Size[v];
            }
        }
}
void DFS_Eli(int u,bool Have)
{
    if (Parent[u]!=u&&Have)
    {
        Tree[u].push_back(Parent[u]);
        Tree[Parent[u]].push_back(u);
    }
    vector<int> HB;
    for (int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if (v!=Parent[u]&&Size[v]>0)
        HB.push_back(v);
    }
    if (!Have&&HB.size()<2)
    {
        if (HB.size())
        DFS_Eli(HB[0],C[u]);
        else
            return;
    }
    else
        for (int i=0;i<HB.size();i++)
        DFS_Eli(HB[i],1);
}
void DFS(int u,int L)
{
    SizeTree++;
    if (L>Max1)
    {
        Max1=L;
        Chosen=u;
    }
    for (int i=0;i<Tree[u].size();i++)
    {
        int v=Tree[u][i];
        if (v!=Parent[u])
        {
            Parent[v]=u;
            DFS(v,L+1);
        }
    }
}
/*********************************************************************/
void DFS_dp(int u)
{
    SizeTree++;
      visited[u]=true;
      ii MAX=ii(0,-10000000),PMAX=ii(0,-10000000);
      for (int i=0;i<Tree[u].size();i++)
      {
            int v=Tree[u][i];
            if (!visited[v])
                  {
                        DFS_dp(v);
                        ii temp=ii(dp[1][v].first+1,dp[1][v].second);
                        if (MAX<temp)
                        {
                              PMAX=MAX;
                              MAX=temp;
                        }
                        else
                        if (PMAX<temp)
                        PMAX=temp;
                  }
      }
      dp[1][u]=MAX;
      if (dp[1][u].second==-10000000)
      dp[1][u].second=-u;
            dp[2][u]=PMAX;
}
void Process()
{
      DFS_dp(Chosen);
      for (int i=1;i<=n;i++)
        if (visited[i])
    {
        if (rs<dp[1][i].first+dp[2][i].first)
            {
                rs=dp[1][i].first+dp[2][i].first;
                Chosen=max(dp[1][i].second,dp[2][i].second);
            }
        else
        if (rs==dp[1][i].first+dp[2][i].first)
        Chosen=max(Chosen,max(dp[1][i].second,dp[1][i].second));
    }
}
/**************************************************************************/
int main()
{
    //freopen("TEST.inp","r",stdin);
    cin>>n>>k;
    for (int i=1;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for (int i=1;i<=k;i++)
    {
        int x;
        cin>>x;
        C[x]=1;
    }
    Parent[1]=1;
    DFS_Build(1);
    DFS_Eli(1,0);
    for (int i=1;i<=n;i++)
    {
        sort(Tree[i].begin(),Tree[i].end(),greater<int>());
        if (Tree[i].size()>0)
            Chosen=i;
        Parent[i]=0;
    }
    Process();
    /*Parent[Chosen]=Chosen;
    SizeTree=0;
    DFS(Chosen,0);
    int rs=Chosen;
    Max1=0;
    SizeTree=0;
    for (int i=1;i<=n;i++)
        {
            sort(Tree[i].begin(),Tree[i].end());
            Parent[i]=0;
        }
    DFS(Chosen,0);
    rs=min(rs,Chosen);
    */
    if (Chosen==0)
    {
        for (int i=1;i<=n;i++)
            if (C[i])
            Chosen=-i;
    }
    cout<<-Chosen<<" "<<2*(SizeTree-1)-rs;
}


//nE[v]=nE[u]+(cnt[v]>0)-(cnt[v]<m)

#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> ii;
int Parent[123457];
bool visited[123457];
bool Visited[123457];
int Size[123457];
bool C[123457];
vector<int> g[123457],Tree[123457];
int Max1=0;
int SizeTree=0;
int Chosen;
int n,k,rs;
ii dp[3][123457];

void DFS_Build(int u)
{
    Size[u]=C[u];
    for (int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            if (v!=Parent[u])
            {
                Parent[v]=u;
                DFS_Build(v);
                Size[u]+=Size[v];
            }
        }
}
void DFS_Eli(int u,bool Have)
{
    if (Parent[u]!=u&&Have)
    {
        Tree[u].push_back(Parent[u]);
        Tree[Parent[u]].push_back(u);
    }
    vector<int> HB;
    for (int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if (v!=Parent[u]&&Size[v]>0)
        HB.push_back(v);
    }
    if (!Have&&HB.size()<2)
    {
        if (HB.size())
        DFS_Eli(HB[0],C[u]);
        else
            return;
    }
    else
        for (int i=0;i<HB.size();i++)
        DFS_Eli(HB[i],1);
}
void DFS(int u,int L)
{
    SizeTree++;
    if (L>Max1)
    {
        Max1=L;
        Chosen=u;
    }
    for (int i=0;i<Tree[u].size();i++)
    {
        int v=Tree[u][i];
        if (v!=Parent[u])
        {
            Parent[v]=u;
            DFS(v,L+1);
        }
    }
}
/*********************************************************************/
void DFS_dp(int u)
{
    SizeTree++;
      visited[u]=true;
      ii MAX=ii(0,-10000000),PMAX=ii(0,-10000000);
      for (int i=0;i<Tree[u].size();i++)
      {
            int v=Tree[u][i];
            if (!visited[v])
                  {
                        DFS_dp(v);
                        ii temp=ii(dp[1][v].first+1,dp[1][v].second);
                        if (MAX<temp)
                        {
                              PMAX=MAX;
                              MAX=temp;
                        }
                        else
                        if (PMAX<temp)
                        PMAX=temp;
                  }
      }
      dp[1][u]=MAX;
      if (dp[1][u].second==-10000000)
      dp[1][u].second=-u;
            dp[2][u]=PMAX;
}
void Process()
{
      DFS_dp(Chosen);
      for (int i=1;i<=n;i++)
        if (visited[i])
    {
        if (rs<dp[1][i].first+dp[2][i].first)
            {
                rs=dp[1][i].first+dp[2][i].first;
                Chosen=max(dp[1][i].second,dp[2][i].second);
            }
        else
        if (rs==dp[1][i].first+dp[2][i].first)
        Chosen=max(Chosen,max(dp[1][i].second,dp[2][i].second));
    }
}
/**************************************************************************/
int main()
{
   // freopen("TEST.inp","r",stdin);
    cin>>n>>k;
    for (int i=1;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for (int i=1;i<=k;i++)
    {
        int x;
        cin>>x;
        C[x]=1;
    }
    Parent[1]=1;
    DFS_Build(1);
    DFS_Eli(1,0);
    for (int i=1;i<=n;i++)
    {
        sort(Tree[i].begin(),Tree[i].end(),greater<int>());
        if (Tree[i].size()>0)
            Chosen=i;
        Parent[i]=0;
    }
    Process();
    /*Parent[Chosen]=Chosen;
    SizeTree=0;
    DFS(Chosen,0);
    int rs=Chosen;
    Max1=0;
    SizeTree=0;
    for (int i=1;i<=n;i++)
        {
            sort(Tree[i].begin(),Tree[i].end());
            Parent[i]=0;
        }
    DFS(Chosen,0);
    rs=min(rs,Chosen);
    */
    if (Chosen==0)
    {
        for (int i=1;i<=n;i++)
            if (C[i])
            Chosen=-i;
    }
    cout<<-Chosen<<" "<<2*(SizeTree-1)-rs;
}


#include <bits/stdc++.h>

using namespace std;

int Parent[123457];
bool Visited[123457];
int Size[123457];
bool C[123457];
vector<int> g[123457],Tree[123457];
int Max1=0;
int SizeTree=0;
int Chosen;
int n,k;
void DFS_Build(int u)
{
    Size[u]=C[u];
    for (int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            if (v!=Parent[u])
            {
                Parent[v]=u;
                DFS_Build(v);
                Size[u]+=Size[v];
            }
        }
}
void DFS_Eli(int u,bool Have)
{
    if (Parent[u]!=u&&Have)
    {
        Tree[u].push_back(Parent[u]);
        Tree[Parent[u]].push_back(u);
    }
    vector<int> HB;
    for (int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if (v!=Parent[u]&&Size[v]>0)
        HB.push_back(v);
    }
    if (!Have&&HB.size()<2)
    DFS_Eli(HB[0],C[u]);
    else
        for (int i=0;i<HB.size();i++)
        DFS_Eli(HB[i],1);
}
void DFS(int u,int L)
{
    SizeTree++;
    if (L>Max1)
    {
        Max1=L;
        Chosen=u;
    }
    for (int i=0;i<Tree[u].size();i++)
    {
        int v=Tree[u][i];
        if (v!=Parent[u])
        {
            Parent[v]=u;
            DFS(v,L+1);
        }
    }
}
int main()
{
    //freopen("TEST.inp","r",stdin);
    cin>>n>>k;
    for (int i=1;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for (int i=1;i<=k;i++)
    {
        int x;
        cin>>x;
        C[x]=1;
    }
    Parent[1]=1;
    DFS_Build(1);
    DFS_Eli(1,0);
    for (int i=1;i<=n;i++)
    {
        sort(Tree[i].begin(),Tree[i].end(),greater<int>());
        if (Tree[i].size()>0)
            Chosen=i;
        Parent[i]=0;
    }
    Parent[Chosen]=Chosen;
    SizeTree=0;
    DFS(Chosen,0);
    int rs=Chosen;
    Max1=0;
    SizeTree=0;
    for (int i=1;i<=n;i++)
        {
            sort(Tree[i].begin(),Tree[i].end());
            Parent[i]=0;
        }
    DFS(Chosen,0);
    rs=min(rs,Chosen);
    cout<<rs<<" "<<2*(SizeTree-1)-Max1;
}


#include <bits/stdc++.h>

using namespace std;

int Parent[123457];
bool Visited[123457];
int Size[123457];
bool C[123457];
vector<int> g[123457],Tree[123457];
int Max1=0;
int SizeTree=0;
int Chosen;
int n,k;
void DFS_Build(int u)
{
    Size[u]=C[u];
    for (int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            if (v!=Parent[u])
            {
                Parent[v]=u;
                DFS_Build(v);
                Size[u]+=Size[v];
            }
        }
}
void DFS_Eli(int u,bool Have)
{
    if (Parent[u]!=u&&Have)
    {
        Tree[u].push_back(Parent[u]);
        Tree[Parent[u]].push_back(u);
       // cout<<"hm";
    }
    vector<int> HB;
    for (int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if (v!=Parent[u]&&Size[v]>0)
        HB.push_back(v);
    }
    if (!Have&&HB.size()<2)
    DFS_Eli(HB[0],C[u]);
    else
        for (int i=0;i<HB.size();i++)
        DFS_Eli(HB[i],1);
}
void DFS(int u,int L)
{
    SizeTree++;
    if (L>Max1)
    {
        Max1=L;
        Chosen=u;
    }
    for (int i=0;i<Tree[u].size();i++)
    {
        int v=Tree[u][i];
        if (v!=Parent[u])
        {
            Parent[v]=u;
            DFS(v,L+1);
        }
    }
}
int main()
{
    freopen("TEST.inp","r",stdin);
    cin>>n>>k;
    for (int i=1;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for (int i=1;i<=k;i++)
    {
        int x;
        cin>>x;
        C[x]=1;
    }
    Parent[1]=1;
    DFS_Build(1);
    DFS_Eli(1,0);
    for (int i=1;i<=n;i++)
    {
        /*cout<<i<<":";
        for (int j=0;j<Tree[i].size();j++)
            cout<<Tree[i][j]<<" ";
        cout<<endl;*/
        sort(Tree[i].begin(),Tree[i].end());
        if (Tree[i].size()>0)
            Chosen=i;
        Parent[i]=0;
    }
    Parent[Chosen]=Chosen;
    SizeTree=0;
    DFS(Chosen,0);
    int rs=Chosen;
    Max1=0;
    SizeTree=0;
    for (int i=1;i<=n;i++)
    {
        Parent[i]=0;
    }
    DFS(Chosen,0);
    rs=min(rs,Chosen);
    cout<<rs<<" "<<2*(SizeTree-1)-Max1;
}


#include <bits/stdc++.h>

using namespace std;

int Parent[123457];
bool Visited[123457];
int Size[123457];
bool C[123457];
vector<int> g[123457],Tree[123457];
int Max1=0;
int SizeTree=0;
int Chosen;
int n,k;
void DFS_Build(int u)
{
    Size[u]=C[u];
    for (int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            if (v!=Parent[u])
            {
                Parent[v]=u;
                DFS_Build(v);
                Size[u]+=Size[v];
            }
        }
}
void DFS_Eli(int u,bool Have)
{
    if (Parent[u]!=u&&Have)
    {
        Tree[u].push_back(Parent[u]);
        Tree[Parent[u]].push_back(u);
    }
    vector<int> HB;
    for (int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if (v!=Parent[u]&&Size[v]>0)
        HB.push_back(v);
    }
    if (!Have&&HB.size()<2)
    {
        if (HB.size())
        DFS_Eli(HB[0],C[u]);
        else
            return;
    }
    else
        for (int i=0;i<HB.size();i++)
        DFS_Eli(HB[i],1);
}
void DFS(int u,int L)
{
    SizeTree++;
    if (L>Max1)
    {
        Max1=L;
        Chosen=u;
    }
    for (int i=0;i<Tree[u].size();i++)
    {
        int v=Tree[u][i];
        if (v!=Parent[u])
        {
            Parent[v]=u;
            DFS(v,L+1);
        }
    }
}
int main()
{
    //freopen("TEST.inp","r",stdin);
    cin>>n>>k;
    for (int i=1;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for (int i=1;i<=k;i++)
    {
        int x;
        cin>>x;
        C[x]=1;
    }
    Parent[1]=1;
    DFS_Build(1);
    DFS_Eli(1,0);
    for (int i=1;i<=n;i++)
    {
        sort(Tree[i].begin(),Tree[i].end(),greater<int>());
        if (Tree[i].size()>0)
            Chosen=i;
        Parent[i]=0;
    }
    Parent[Chosen]=Chosen;
    SizeTree=0;
    DFS(Chosen,0);
    int rs=Chosen;
    Max1=0;
    SizeTree=0;
    for (int i=1;i<=n;i++)
        {
            sort(Tree[i].begin(),Tree[i].end());
            Parent[i]=0;
        }
    DFS(Chosen,0);
    rs=min(rs,Chosen);
    if (rs==0)
    {
        for (int i=1;i<=n;i++)
            if (C[i])
            rs=i;
    }
    cout<<rs<<" "<<2*(SizeTree-1)-Max1;
}


#include <bits/stdc++.h>

using namespace std;

int Parent[123457];
bool Visited[123457];
int Size[123457];
bool C[123457];
vector<int> g[123457],Tree[123457];
int Max1=0;
int SizeTree=0;
int Chosen;
int n,k;
void DFS_Build(int u)
{
    Size[u]=C[u];
    for (int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            if (v!=Parent[u])
            {
                Parent[v]=u;
                DFS_Build(v);
                Size[u]+=Size[v];
            }
        }
}
void DFS_Eli(int u,bool Have)
{
    if (Parent[u]!=u&&Have)
    {
        Tree[u].push_back(Parent[u]);
        Tree[Parent[u]].push_back(u);
       // cout<<"hm";
    }
    vector<int> HB;
    for (int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if (v!=Parent[u]&&Size[v]>0)
        HB.push_back(v);
    }
    if (!Have&&HB.size()<2)
    DFS_Eli(HB[0],C[u]);
    else
        for (int i=0;i<HB.size();i++)
        DFS_Eli(HB[i],1);
}
void DFS(int u,int L)
{
    SizeTree++;
    if (L>Max1)
    {
        Max1=L;
        Chosen=u;
    }
    for (int i=0;i<Tree[u].size();i++)
    {
        int v=Tree[u][i];
        if (v!=Parent[u])
        {
            Parent[v]=u;
            DFS(v,L+1);
        }
    }
}
int main()
{
   // freopen("TEST.inp","r",stdin);
    cin>>n>>k;
    for (int i=1;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    for (int i=1;i<=k;i++)
    {
        int x;
        cin>>x;
        C[x]=1;
    }
    Parent[1]=1;
    DFS_Build(1);
    DFS_Eli(1,0);
    for (int i=1;i<=n;i++)
    {
        /*cout<<i<<":";
        for (int j=0;j<Tree[i].size();j++)
            cout<<Tree[i][j]<<" ";
        cout<<endl;*/
        sort(Tree[i].begin(),Tree[i].end());
        if (Tree[i].size()>0)
            Chosen=i;
        Parent[i]=0;
    }
    Parent[Chosen]=Chosen;
    SizeTree=0;
    DFS(Chosen,0);
    int rs=Chosen;
    Max1=0;
    SizeTree=0;
    for (int i=1;i<=n;i++)
    {
        Parent[i]=0;
    }
    DFS(Chosen,0);
    rs=min(rs,Chosen);
    cout<<rs<<" "<<2*(SizeTree-1)-Max1;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<utility>
#include<set>
#include<algorithm>

using namespace std;

typedef unsigned long long ULL;
typedef long long LL;

const int MAXN = 1237567;
const int MAXM = MAXN;

struct EDGE{
    int next, to;
}edge[MAXN * 2];

int first[MAXN];
int tot=-1;

void add_edge(int from, int to){
    edge[++tot] = (EDGE){first[from], to};
    first[from] = tot;
}

int n, m, root;
int dis[MAXN], len[MAXN];
int en[MAXM];

int dfs(int v, int pa){
    int tot = 0;
    dis[v] = 0;
    len[v] = 0;
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (pa != u) {
            int tmp = dfs(u, v);
            tot += tmp;
            if (tmp) dis[v] = max(dis[v], dis[u] + 1);
        }
    }
//  cout<<v<<' '<<dis[v]<<endl;
    if (en[v] || tot) return tot + 1;
    return 0;
}

void pawn(int v, int L, int pa){
    int fl = 0, fv = 0, sl = 0, sv = 0;
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (u == pa) continue;
        if (dis[u] > fl) {
            sl = fl;
            sv = fv;
            fl = dis[u];
            fv = u;
        } else if (dis[u] > sl) {
            sl = dis[u];
            sv = u;
        }
    }

//  cout<<v<<' '<<fl<<' '<<sl<<' '<<L<<endl;
    fl++;
    sl++;
    len[v] = max(L, fl);
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (u == pa) continue;
        if (u != fv) {
            pawn(u, max(L+1, fv+1), v);
        } else {
            pawn(u, max(L+1, sv+1), v);
        }
    }
}

int main(){
    memset(first, -1, sizeof(first));
    scanf("%d%d", &n, &m);
    for (int i = 1; i < n; ++i) {
        int a, b;
        scanf("%d%d", &a, &b);
        add_edge(a, b);
        add_edge(b, a);
    }
    for (int i = 0; i < m; ++i){
        int a;
        scanf("%d", &a);
        en[a] = 1;
        root = a;
    }
    int k = dfs(root, -1) - 1;
//  for (int i = 1; i <= n; i++) {
//      cout<<dis[i]<<' ';
//  }cout<<endl;
    //cout<<k*2<<' '<<dis[root]-1<<endl;
    pawn(root, 0, -1);
//  for (int i = 1; i <= n; ++i) {
//      cout<<len[i]<<' ';
//  }cout<<endl;
    int ans = 0;
    len[ans] = -1;
    for (int i = 1; i<=n; i++) {
        if (!en[i]) continue;
        if (len[i] > len[ans]) ans = i;
    }
//  cout<<len[4]<<' '<<len[ans]<<endl;
    cout<<ans<<endl<<k*2-len[ans]<<endl;
    return 0;
}

/*

7 2
1 2
1 3
1 4
3 5
3 6
3 7
2 7

6 4
1 2
2 3
2 4
4 5
4 6
2 4 5 6

10 4
8 7
7 6
1 2
3 2
3 4
6 5
10 7
7 9
5 4
9 5 10 4

*/


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<utility>
#include<set>
#include<algorithm>

using namespace std;

typedef unsigned long long ULL;
typedef long long LL;

const int MAXN = 1237567;
const int MAXM = MAXN;

struct EDGE{
    int next, to;
}edge[MAXN * 2];

int first[MAXN];
int tot=-1;

void add_edge(int from, int to){
    edge[++tot] = (EDGE){first[from], to};
    first[from] = tot;
}

int n, m, root;
int dis[MAXN], len[MAXN];
int en[MAXM];

int dfs(int v, int pa){
    int tot = 0;
    dis[v] = 0;
    len[v] = 0;
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (pa != u) {
            int tmp = dfs(u, v);
            tot += tmp;
            if (tmp) dis[v] = max(dis[v], dis[u] + 1);
        }
    }
//  cout<<pa << ' '<<v<<' '<<dis[v]<<endl;
    if (en[v] || tot) return tot + 1;
    return 0;
}

void pawn(int v, int L, int pa){
    int fl = -1, fv = 0, sl = -1, sv = 0;
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (u == pa) continue;
        if (dis[u] > fl) {
            sl = fl;
            sv = fv;
            fl = dis[u];
            fv = u;
        } else if (dis[u] > sl) {
            sl = dis[u];
            sv = u;
        }
    }

//  cout<<v<<' '<<fl<<' '<<sl<<' '<<L<<endl;
    len[v] = L;
    if (fl > 0 || en[fv]) len[v] = max(len[v], fl+1);
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (u == pa) continue;
        if (u != fv) {
            pawn(u, max(L+1, fl+2), v);
        } else {
            pawn(u, max(L+1, sl+2), v);
        }
    }
}

int main(){
    memset(first, -1, sizeof(first));
    scanf("%d%d", &n, &m);
    for (int i = 1; i < n; ++i) {
        int a, b;
        scanf("%d%d", &a, &b);
        add_edge(a, b);
        add_edge(b, a);
    }
    for (int i = 0; i < m; ++i){
        int a;
        scanf("%d", &a);
        en[a] = 1;
        root = a;
    }
    int k = dfs(root, 0) - 1;
//  for (int i = 1; i <= n; i++) {
//      cout<<dis[i]<<' ';
//  }cout<<endl;
    //cout<<k*2<<' '<<dis[root]-1<<endl;
    pawn(root, 0, -1);
//  for (int i = 1; i <= n; ++i) {
//      cout<<len[i]<<' ';
//  }cout<<endl;
    int ans = 0;
    len[ans] = -1;
    for (int i = 1; i<=n; i++) {
        if (!en[i]) continue;
        if (len[i] > len[ans]) ans = i;
    }
//  cout<<len[4]<<' '<<len[ans]<<endl;
//  if (n > 1000) cout<<k*2<<endl;
//  if (m == 1) len[ans] = 0;
    cout<<ans<<endl<<k*2-len[ans]<<endl;
    return 0;
}

/*

7 2
1 2
1 3
1 4
3 5
3 6
3 7
2 7

6 4
1 2
2 3
2 4
4 5
4 6
2 4 5 6

10 4
8 7
7 6
1 2
3 2
3 4
6 5
10 7
7 9
5 4
9 5 10 4

74 8
24 33
39 11
2 19
64 29
5 10
55 63
37 34
42 12
69 65
53 67
38 6
12 46
29 14
52 74
72 26
33 62
16 58
19 64
69 53
2 20
28 3
19 17
72 32
48 40
55 56
33 9
39 72
29 45
63 49
7 59
63 51
30 71
9 57
53 73
2 50
30 38
13 47
9 36
52 25
30 48
28 27
13 43
12 7
59 18
64 28
16 1
8 35
29 23
72 66
11 55
5 37
59 30
11 54
39 24
37 8
42 60
38 4
64 68
5 44
28 22
16 5
69 31
38 2
55 41
52 15
24 13
48 61
11 42
19 70
7 52
39 16
48 21
59 69
42 1 31 20 47 23 67 52

*/


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<utility>
#include<set>
#include<algorithm>

using namespace std;

typedef unsigned long long ULL;
typedef long long LL;

const int MAXN = 1237567;
const int MAXM = MAXN;

struct EDGE{
    int next, to;
}edge[MAXN * 2];

int first[MAXN];
int tot=-1;

void add_edge(int from, int to){
    edge[++tot] = (EDGE){first[from], to};
    first[from] = tot;
}

int n, m, root;
int dis[MAXN], len[MAXN];
int en[MAXM];

int dfs(int v, int pa){
    int tot = 0;
    dis[v] = 0;
    len[v] = 0;
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (pa != u) {
            int tmp = dfs(u, v);
            tot += tmp;
            if (tmp) dis[v] = max(dis[v], dis[u] + 1);
        }
    }
//  cout<<pa << ' '<<v<<' '<<dis[v]<<endl;
    if (en[v] || tot) return tot + 1;
    return 0;
}

void pawn(int v, int L, int pa){
    int fl = 0, fv = 0, sl = 0, sv = 0;
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (u == pa) continue;
        if (dis[u] > fl) {
            sl = fl;
            sv = fv;
            fl = dis[u];
            fv = u;
        } else if (dis[u] > sl) {
            sl = dis[u];
            sv = u;
        }
    }

//  cout<<v<<' '<<fl<<' '<<sl<<' '<<L<<endl;
    len[v] = max(L, fl+1);
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (u == pa) continue;
        if (u != fv) {
            pawn(u, max(L+1, fl+1), v);
        } else {
            pawn(u, max(L+1, sl+1), v);
        }
    }
}

int main(){
    memset(first, -1, sizeof(first));
    scanf("%d%d", &n, &m);
    for (int i = 1; i < n; ++i) {
        int a, b;
        scanf("%d%d", &a, &b);
        add_edge(a, b);
        add_edge(b, a);
    }
    for (int i = 0; i < m; ++i){
        int a;
        scanf("%d", &a);
        en[a] = 1;
        root = a;
    }
    int k = dfs(root, -1) - 1;
//  for (int i = 1; i <= n; i++) {
//      cout<<dis[i]<<' ';
//  }cout<<endl;
    //cout<<k*2<<' '<<dis[root]-1<<endl;
    pawn(root, 0, -1);
//  for (int i = 1; i <= n; ++i) {
//      cout<<len[i]<<' ';
//  }cout<<endl;
    int ans = 0;
    len[ans] = -1;
    for (int i = 1; i<=n; i++) {
        if (!en[i]) continue;
        if (len[i] > len[ans]) ans = i;
    }
//  cout<<len[4]<<' '<<len[ans]<<endl;
//  if (n > 1000) cout<<k*2<<endl;
    cout<<ans<<endl<<k*2-len[ans]<<endl;
    return 0;
}

/*

7 2
1 2
1 3
1 4
3 5
3 6
3 7
2 7

6 4
1 2
2 3
2 4
4 5
4 6
2 4 5 6

10 4
8 7
7 6
1 2
3 2
3 4
6 5
10 7
7 9
5 4
9 5 10 4

74 8
24 33
39 11
2 19
64 29
5 10
55 63
37 34
42 12
69 65
53 67
38 6
12 46
29 14
52 74
72 26
33 62
16 58
19 64
69 53
2 20
28 3
19 17
72 32
48 40
55 56
33 9
39 72
29 45
63 49
7 59
63 51
30 71
9 57
53 73
2 50
30 38
13 47
9 36
52 25
30 48
28 27
13 43
12 7
59 18
64 28
16 1
8 35
29 23
72 66
11 55
5 37
59 30
11 54
39 24
37 8
42 60
38 4
64 68
5 44
28 22
16 5
69 31
38 2
55 41
52 15
24 13
48 61
11 42
19 70
7 52
39 16
48 21
59 69
42 1 31 20 47 23 67 52

*/


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<utility>
#include<set>
#include<algorithm>

using namespace std;

typedef unsigned long long ULL;
typedef long long LL;

const int MAXN = 123756;
const int MAXM = MAXN;

struct EDGE{
    int next, to;
}edge[MAXN * 2];

int first[MAXN];
int tot=-1;

void add_edge(int from, int to){
    edge[++tot] = (EDGE){first[from], to};
    first[from] = tot;
}

int n, m, root;
int dis[MAXN], len[MAXN];
int en[MAXM];

int dfs(int v, int pa){
    int tot = 0;
    dis[v] = 0;
    len[v] = 0;
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (pa != u) {
            int tmp = dfs(u, v);
            tot += tmp;
            if (tmp) dis[v] = max(dis[v], dis[u] + 1);
        }
    }
//  cout<<v<<' '<<dis[v]<<endl;
    if (en[v] || tot) return tot + 1;
    return 0;
}

void pawn(int v, int L, int pa){
    int fl = 0, fv = 0, sl = 0, sv = 0;
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (u == pa) continue;
        if (dis[u] > fl) {
            sl = fl;
            sv = fv;
            fl = dis[u];
            fv = u;
        } else if (dis[u] > sl) {
            sl = dis[u];
            sv = u;
        }
    }

//  cout<<v<<' '<<fl<<' '<<sl<<' '<<L<<endl;
    fl++;
    sl++;
    len[v] = max(L, fl);
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (u == pa) continue;
        if (u != fv) {
            pawn(u, max(L+1, fv+1), v);
        } else {
            pawn(u, max(L+1, sv+1), v);
        }
    }
}

int main(){
    memset(first, -1, sizeof(first));
    scanf("%d%d", &n, &m);
    for (int i = 1; i < n; ++i) {
        int a, b;
        scanf("%d%d", &a, &b);
        add_edge(a, b);
        add_edge(b, a);
    }
    for (int i = 0; i < m; ++i){
        int a;
        scanf("%d", &a);
        en[a] = 1;
        root = a;
    }
    int k = dfs(root, -1) - 1;
//  for (int i = 1; i <= n; i++) {
//      cout<<dis[i]<<' ';
//  }cout<<endl;
    //cout<<k*2<<' '<<dis[root]-1<<endl;
    pawn(root, 0, -1);
//  for (int i = 1; i <= n; ++i) {
//      cout<<len[i]<<' ';
//  }cout<<endl;
    int ans = 0;
    len[ans] = -1;
    for (int i = 1; i<=n; i++) {
        if (!en[i]) continue;
        if (len[i] > len[ans]) ans = i;
    }
//  cout<<len[4]<<' '<<len[ans]<<endl;
    cout<<ans<<endl<<k*2-len[ans]<<endl;
    return 0;
}

/*

7 2
1 2
1 3
1 4
3 5
3 6
3 7
2 7

6 4
1 2
2 3
2 4
4 5
4 6
2 4 5 6

10 4
8 7
7 6
1 2
3 2
3 4
6 5
10 7
7 9
5 4
9 5 10 4

*/


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<utility>
#include<set>
#include<algorithm>

using namespace std;

typedef unsigned long long ULL;
typedef long long LL;

const int MAXN = 1237567;
const int MAXM = MAXN;

struct EDGE{
    int next, to;
}edge[MAXN * 2];

int first[MAXN];
int tot=-1;

void add_edge(int from, int to){
    edge[++tot] = (EDGE){first[from], to};
    first[from] = tot;
}

int n, m, root;
int dis[MAXN], len[MAXN];
int en[MAXM];

int dfs(int v, int pa){
    int tot = 0;
    dis[v] = 0;
    len[v] = 0;
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (pa != u) {
            int tmp = dfs(u, v);
            tot += tmp;
            if (tmp) dis[v] = max(dis[v], dis[u] + 1);
        }
    }
//  cout<<pa << ' '<<v<<' '<<dis[v]<<endl;
    if (en[v] || tot) return tot + 1;
    return 0;
}

void pawn(int v, int L, int pa){
    int fl = 0, fv = 0, sl = 0, sv = 0;
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (u == pa) continue;
        if (dis[u] > fl) {
            sl = fl;
            sv = fv;
            fl = dis[u];
            fv = u;
        } else if (dis[u] > sl) {
            sl = dis[u];
            sv = u;
        }
    }

//  cout<<v<<' '<<fl<<' '<<sl<<' '<<L<<endl;
    len[v] = max(L+1, fl+1);
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (u == pa) continue;
        if (u != fv) {
            pawn(u, max(L+1, fl+1), v);
        } else {
            pawn(u, max(L+1, sl+1), v);
        }
    }
}

int main(){
    memset(first, -1, sizeof(first));
    scanf("%d%d", &n, &m);
    for (int i = 1; i < n; ++i) {
        int a, b;
        scanf("%d%d", &a, &b);
        add_edge(a, b);
        add_edge(b, a);
    }
    for (int i = 0; i < m; ++i){
        int a;
        scanf("%d", &a);
        en[a] = 1;
        root = a;
    }
    int k = dfs(root, -1) - 1;
//  for (int i = 1; i <= n; i++) {
//      cout<<dis[i]<<' ';
//  }cout<<endl;
    //cout<<k*2<<' '<<dis[root]-1<<endl;
    pawn(root, 0, -1);
//  for (int i = 1; i <= n; ++i) {
//      cout<<len[i]<<' ';
//  }cout<<endl;
    int ans = 0;
    len[ans] = -1;
    for (int i = 1; i<=n; i++) {
        if (!en[i]) continue;
        if (len[i] > len[ans]) ans = i;
    }
//  cout<<len[4]<<' '<<len[ans]<<endl;
//  if (n > 1000) cout<<k*2<<endl;
    cout<<ans<<endl<<k*2-len[ans]<<endl;
    return 0;
}

/*

7 2
1 2
1 3
1 4
3 5
3 6
3 7
2 7

6 4
1 2
2 3
2 4
4 5
4 6
2 4 5 6

10 4
8 7
7 6
1 2
3 2
3 4
6 5
10 7
7 9
5 4
9 5 10 4

74 8
24 33
39 11
2 19
64 29
5 10
55 63
37 34
42 12
69 65
53 67
38 6
12 46
29 14
52 74
72 26
33 62
16 58
19 64
69 53
2 20
28 3
19 17
72 32
48 40
55 56
33 9
39 72
29 45
63 49
7 59
63 51
30 71
9 57
53 73
2 50
30 38
13 47
9 36
52 25
30 48
28 27
13 43
12 7
59 18
64 28
16 1
8 35
29 23
72 66
11 55
5 37
59 30
11 54
39 24
37 8
42 60
38 4
64 68
5 44
28 22
16 5
69 31
38 2
55 41
52 15
24 13
48 61
11 42
19 70
7 52
39 16
48 21
59 69
42 1 31 20 47 23 67 52

*/


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<utility>
#include<set>
#include<algorithm>

using namespace std;

typedef unsigned long long ULL;
typedef long long LL;

const int MAXN = 123756;
const int MAXM = MAXN;

struct EDGE{
	int next, to;
}edge[MAXN * 2];

int first[MAXN];
int tot=-1;

void add_edge(int from, int to){
	edge[++tot] = (EDGE){first[from], to};
	first[from] = tot;
}

int n, m, root;
int dis[MAXN], len[MAXN];
int en[MAXM];

int dfs(int v, int pa){
	int tot = 0;
	dis[v] = 0;
	len[v] = 0;
	for (int i = first[v]; i != -1; i = edge[i].next){
		int u = edge[i].to;
		if (pa != u) {
			int tmp = dfs(u, v);
			tot += tmp;
			if (tmp) dis[v] = max(dis[v], dis[u] + 1);
		}
	}
	if (en[v] || tot) return tot + 1;
	return 0;
}

void pawn(int v, int L, int pa){
	int fl = 0, fv = 0, sl = 0, sv = 0;
	for (int i = first[v]; i != -1; i = edge[i].next){
		int u = edge[i].to;
		if (u == pa) continue;
		if (dis[u] > fl) {
			sl = fl;
			sv = fv;
			fl = dis[u];
			fv = u;
		} else if (dis[u] > sl) {
			sl = dis[u];
			sv = u;
		}
	}

	len[v] = max(L, fl);
	for (int i = first[v]; i != -1; i = edge[i].next){
		int u = edge[i].to;
		if (u == pa) continue;
		if (u != fv) {
			pawn(u, max(L+1, fv+1), v);
		} else {
			pawn(u, max(L+1, sv+1), v);
		}
	}
}

int main(){
	memset(first, -1, sizeof(first));
	scanf("%d%d", &n, &m);
	for (int i = 1; i < n; ++i) {
		int a, b;
		scanf("%d%d", &a, &b);
		add_edge(a, b);
		add_edge(b, a);
	}
	for (int i = 0; i < m; ++i){
		int a;
		scanf("%d", &a);
		en[a] = 1;
		root = a;
	}
	int k = dfs(root, -1) - 1;
	//cout<<k*2<<' '<<dis[root]-1<<endl;
	pawn(root, 0, -1);
//	for (int i = 1; i <= n; ++i) {
//		cout<<len[i]<<' ';
//	}cout<<endl;
	int ans = 0;
	len[ans] = -1;
	for (int i = 1; i<=n; i++) {
		if (!en[i]) continue;
		if (len[i] > len[ans]) ans = i;
	}
	cout<<ans<<endl<<k*2-len[ans]<<endl;
	return 0;
}

/*

7 2
1 2
1 3
1 4
3 5
3 6
3 7
2 7

6 4
1 2
2 3
2 4
4 5
4 6
2 4 5 6

*/


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<utility>
#include<set>
#include<algorithm>

using namespace std;

typedef unsigned long long ULL;
typedef long long LL;

const int MAXN = 1237567;
const int MAXM = MAXN;

struct EDGE{
    int next, to;
}edge[MAXN * 2];

int first[MAXN];
int tot=-1;

void add_edge(int from, int to){
    edge[++tot] = (EDGE){first[from], to};
    first[from] = tot;
}

int n, m, root;
int dis[MAXN], len[MAXN];
int en[MAXM];

int dfs(int v, int pa){
    int tot = 0;
    dis[v] = 0;
    len[v] = 0;
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (pa != u) {
            int tmp = dfs(u, v);
            tot += tmp;
            if (tmp) dis[v] = max(dis[v], dis[u] + 1);
        }
    }
//  cout<<pa << ' '<<v<<' '<<dis[v]<<endl;
    if (en[v] || tot) return tot + 1;
    return 0;
}

void pawn(int v, int L, int pa){
    int fl = -1, fv = 0, sl = -1, sv = 0;
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (u == pa) continue;
        if (dis[u] > fl) {
            sl = fl;
            sv = fv;
            fl = dis[u];
            fv = u;
        } else if (dis[u] > sl) {
            sl = dis[u];
            sv = u;
        }
    }

//  cout<<v<<' '<<fl<<' '<<sl<<' '<<L<<endl;
    if (fl > 0 || en[fv]) len[v] = max(L, fl+1);
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (u == pa) continue;
        if (u != fv) {
            pawn(u, max(L+1, fl+2), v);
        } else {
            pawn(u, max(L+1, sl+2), v);
        }
    }
}

int main(){
    memset(first, -1, sizeof(first));
    scanf("%d%d", &n, &m);
    for (int i = 1; i < n; ++i) {
        int a, b;
        scanf("%d%d", &a, &b);
        add_edge(a, b);
        add_edge(b, a);
    }
    for (int i = 0; i < m; ++i){
        int a;
        scanf("%d", &a);
        en[a] = 1;
        root = a;
    }
    int k = dfs(root, -1) - 1;
//  for (int i = 1; i <= n; i++) {
//      cout<<dis[i]<<' ';
//  }cout<<endl;
    //cout<<k*2<<' '<<dis[root]-1<<endl;
    pawn(root, 0, -1);
//  for (int i = 1; i <= n; ++i) {
//      cout<<len[i]<<' ';
//  }cout<<endl;
    int ans = 0;
    len[ans] = -1;
    for (int i = 1; i<=n; i++) {
        if (!en[i]) continue;
        if (len[i] > len[ans]) ans = i;
    }
//  cout<<len[4]<<' '<<len[ans]<<endl;
//  if (n > 1000) cout<<k*2<<endl;
//  if (m == 1) len[ans] = 0;
    cout<<ans<<endl<<k*2-len[ans]<<endl;
    return 0;
}

/*

7 2
1 2
1 3
1 4
3 5
3 6
3 7
2 7

6 4
1 2
2 3
2 4
4 5
4 6
2 4 5 6

10 4
8 7
7 6
1 2
3 2
3 4
6 5
10 7
7 9
5 4
9 5 10 4

74 8
24 33
39 11
2 19
64 29
5 10
55 63
37 34
42 12
69 65
53 67
38 6
12 46
29 14
52 74
72 26
33 62
16 58
19 64
69 53
2 20
28 3
19 17
72 32
48 40
55 56
33 9
39 72
29 45
63 49
7 59
63 51
30 71
9 57
53 73
2 50
30 38
13 47
9 36
52 25
30 48
28 27
13 43
12 7
59 18
64 28
16 1
8 35
29 23
72 66
11 55
5 37
59 30
11 54
39 24
37 8
42 60
38 4
64 68
5 44
28 22
16 5
69 31
38 2
55 41
52 15
24 13
48 61
11 42
19 70
7 52
39 16
48 21
59 69
42 1 31 20 47 23 67 52

*/


#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<utility>
#include<set>
#include<algorithm>

using namespace std;

typedef unsigned long long ULL;
typedef long long LL;

const int MAXN = 1237567;
const int MAXM = MAXN;

struct EDGE{
    int next, to;
}edge[MAXN * 2];

int first[MAXN];
int tot=-1;

void add_edge(int from, int to){
    edge[++tot] = (EDGE){first[from], to};
    first[from] = tot;
}

int n, m, root;
int dis[MAXN], len[MAXN];
int en[MAXM];

int dfs(int v, int pa){
    int tot = 0;
    dis[v] = 0;
    len[v] = 0;
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (pa != u) {
            int tmp = dfs(u, v);
            tot += tmp;
            if (tmp) dis[v] = max(dis[v], dis[u] + 1);
        }
    }
//  cout<<pa << ' '<<v<<' '<<dis[v]<<endl;
    if (en[v] || tot) return tot + 1;
    return 0;
}

void pawn(int v, int L, int pa){
    int fl = 0, fv = 0, sl = 0, sv = 0;
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (u == pa) continue;
        if (dis[u] > fl) {
            sl = fl;
            sv = fv;
            fl = dis[u];
            fv = u;
        } else if (dis[u] > sl) {
            sl = dis[u];
            sv = u;
        }
    }

//  cout<<v<<' '<<fl<<' '<<sl<<' '<<L<<endl;
    len[v] = max(L, fl);
    for (int i = first[v]; i != -1; i = edge[i].next){
        int u = edge[i].to;
        if (u == pa) continue;
        if (u != fv) {
            pawn(u, max(L+1, fl+1), v);
        } else {
            pawn(u, max(L+1, sl+1), v);
        }
    }
}

int main(){
    memset(first, -1, sizeof(first));
    scanf("%d%d", &n, &m);
    for (int i = 1; i < n; ++i) {
        int a, b;
        scanf("%d%d", &a, &b);
        add_edge(a, b);
        add_edge(b, a);
    }
    for (int i = 0; i < m; ++i){
        int a;
        scanf("%d", &a);
        en[a] = 1;
        root = a;
    }
    int k = dfs(root, -1) - 1;
//  for (int i = 1; i <= n; i++) {
//      cout<<dis[i]<<' ';
//  }cout<<endl;
    //cout<<k*2<<' '<<dis[root]-1<<endl;
    pawn(root, 0, -1);
//  for (int i = 1; i <= n; ++i) {
//      cout<<len[i]<<' ';
//  }cout<<endl;
    int ans = 0;
    len[ans] = -1;
    for (int i = 1; i<=n; i++) {
        if (!en[i]) continue;
        if (len[i] > len[ans]) ans = i;
    }
//  cout<<len[4]<<' '<<len[ans]<<endl;
//  if (n > 1000) cout<<k*2<<endl;
    cout<<ans<<endl<<k*2-len[ans]<<endl;
    return 0;
}

/*

7 2
1 2
1 3
1 4
3 5
3 6
3 7
2 7

6 4
1 2
2 3
2 4
4 5
4 6
2 4 5 6

10 4
8 7
7 6
1 2
3 2
3 4
6 5
10 7
7 9
5 4
9 5 10 4

74 8
24 33
39 11
2 19
64 29
5 10
55 63
37 34
42 12
69 65
53 67
38 6
12 46
29 14
52 74
72 26
33 62
16 58
19 64
69 53
2 20
28 3
19 17
72 32
48 40
55 56
33 9
39 72
29 45
63 49
7 59
63 51
30 71
9 57
53 73
2 50
30 38
13 47
9 36
52 25
30 48
28 27
13 43
12 7
59 18
64 28
16 1
8 35
29 23
72 66
11 55
5 37
59 30
11 54
39 24
37 8
42 60
38 4
64 68
5 44
28 22
16 5
69 31
38 2
55 41
52 15
24 13
48 61
11 42
19 70
7 52
39 16
48 21
59 69
42 1 31 20 47 23 67 52

*/


//yogesh sharma

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

int main()
{
    int n,m;
    char input[n+1],ref[27];
    scanf("%d %d",&n,&m);
    scanf("%s",input);
    for(int i=0;i<26;i++) ref[i]= i+'a';
   // printf("%s\n",ref);

    for(int i=0;i<m;i++)
    {
        scanf("%*c");
        char temp1,temp2;
        //cin>>temp1>>temp2;
        scanf("%c %c",&temp1,&temp2);
        for(int j=0;j<26;j++)
        {
            if(ref[j]==temp1) ref[j]=temp2;
            else if(ref[j]==temp2) ref[j]=temp1;
        }

    }
    for(int i=0;i<n;i++) input[i]= ref[input[i]-'a'];
    printf("%s\n",input);
    return 0;
}


//yogesh sharma

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

int main()
{
    int n,m;
    char arr[1000000];
    vector <vector<int> > hash;
    cin>>n>>m;
    scanf("%s",arr);
    hash.resize(30);
    for(int i=0;i<n;i++)
    {
    //    printf("%d\n",arr[i]-'a');
        hash[arr[i]-'a'].push_back(i);
    }

    vector <int> a;
    vector <int> b;
   // a.resize(1000000);
   // b.resize(1000000);
    for(int i=0;i<m;i++)
    {
        scanf("%*c");
        char temp, temp2;
        scanf("%c %c",&temp,&temp2);
       // printf("%c %c\n",temp,temp2);
        int val= temp-'a';
        int size= hash[val].size();

        for(int j=size-1; j>=0; j--)
        {
            int index= hash[val][j];
            a.push_back(index);
            hash[val].pop_back();
        }
        size= hash[temp2-'a'].size();
       // printf("%d--size\n",size);
        for(int j=size-1; j>=0; j--)
        {
            b.push_back(hash[temp2-'a'][j]);
            hash[temp2-'a'].pop_back();
        }
        size= a.size();
        for(int j=size-1; j>=0; j--)
        {
            arr[a[j]]=temp2;
            hash[temp2-'a'].push_back(a[j]);
            a.pop_back();

        }
        size= b.size();
      //  printf("%d-size\n",size);
        for(int j=size-1;j>=0 ;j--)
        {
            arr[b[j]]=temp;
            hash[temp-'a'].push_back(b[j]);
            b.pop_back();
        }
   // printf("%s\n",arr);
    }

    printf("%s\n",arr);
    return 0;

}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include<cstring>
#include<string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>

typedef unsigned long long ull;
typedef unsigned long ul;
typedef long long ll;

#define MAX 1000007

using namespace std;

char str[MAX];
char str2[MAX];
char str3[MAX];

int main()
{
    int i,j;
    int n,m;
    cin>>n>>m;
    cin>>str;

    for(j = 0; j<m; j++){
        cin>>str2[j]>>str3[j];

        for(i = 0; i<26; i++){
            if(str[i]==str2[j]){
                str[i] = str3[j];
            }
            else if(str[i]==str3[j]){
                str[i] = str2[j];
            }
        }
    }
    cout<<str<<endl;

    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include<cstring>
#include<string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>

typedef unsigned long long ull;
typedef unsigned long ul;
typedef long long ll;

#define MAX 1000007

using namespace std;

char str[200005];
char ch;
char ch1;

int main()
{
    int i,j;
    int n,m;
    cin>>n>>m;
    cin>>str;

    for(j = 0; j<m; j++){
        cin>>ch>>ch1;

        for(i = 0; i<n; i++){
            if(str[i]==ch){
                str[i] = ch1;
            }
            else if(str[i]==ch1){
                str[i] = ch;
            }
        }
    }
    cout<<str<<endl;

    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include<cstring>
#include<string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>

typedef unsigned long long ull;
typedef unsigned long ul;
typedef long long ll;

#define MAX 1000007

using namespace std;

char str[200005], temp[101];
char ch;
char ch1;

int main()
{

    int i,j;
    for(i = 0; i<26; i++)
        temp[i] = 'a' + i ;
    int n,m;
    cin>>n>>m;
    cin>>str;

    for(j = 0; j<m; j++){
        cin>>ch>>ch1;
        for(i = 0; i<26; i++){
            if(temp[i]==ch)
                temp[i] = ch1;
            else if(temp[i]==ch1)
                temp[i] = ch;
        }

    }
    for(i = 0; i<n; i++)
        str[i] = temp[str[i]-'a'];

    cout<<str<<endl;

    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include<cstring>
#include<string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>

typedef unsigned long long ull;
typedef unsigned long ul;
typedef long long ll;

#define MAX 1000007

using namespace std;

char str[MAX];
char str2[MAX];
char str3[MAX];

int main()
{
    int i,j;
    int n,m;
    cin>>n>>m;
    cin>>str;

    for(i = 0; i<m; i++)
        cin>>str2[i]>>str3[i];

    for(j = 0; j<m; j++){
        for(i = 0; i<n; i++){
            if(str[i]==str2[j]){
                str[i] = str3[j];
            }
            else if(str[i]==str3[j]){
                str[i] = str2[j];
            }
        }
    }
    cout<<str<<endl;

    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include<cstring>
#include<string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>

typedef unsigned long long ull;
typedef unsigned long ul;
typedef long long ll;

#define MAX 1000007

using namespace std;

char str[200001];
char str2[101];
char str3[101];

int main()
{
    int i,j;
    int n,m;
    cin>>n>>m;
    cin>>str;

    for(j = 0; j<m; j++){
        cin>>str2[j]>>str3[j];

        for(i = 0; i<n; i++){
            if(str[i]==str2[j]){
                str[i] = str3[j];
            }
            else if(str[i]==str3[j]){
                str[i] = str2[j];
            }
        }
    }
    cout<<str<<endl;

    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include<cstring>
#include<string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>

typedef unsigned long long ull;
typedef unsigned long ul;
typedef long long ll;

#define MAX 1000007

using namespace std;

char str[MAX];
char str2[MAX];
char str3[MAX];

int main()
{
    int i,j;
    int n,m;
    cin>>n>>m;
    cin>>str;

    for(i = 0; i<m; i++)
        cin>>str2[i]>>str3[i];

    for(j = 0; j<m; j++){
        for(i = 0; i<26; i++){
            if(str[i]==str2[j]){
                str[i] = str3[j];
            }
            else if(str[i]==str3[j]){
                str[i] = str2[j];
            }
        }
    }
    cout<<str<<endl;

    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include<cstring>
#include<string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>

typedef unsigned long long ull;
typedef unsigned long ul;
typedef long long ll;

#define MAX 1000007

using namespace std;

char str[200001];
char str2[101];
char str3[101];

int main()
{
    int i,j;
    int n,m;
    cin>>n>>m;
    cin>>str;

    for(j = 0; j<m; j++){
        cin>>str2[j]>>str3[j];

        for(i = 0; i<26; i++){
            if(str[i]==str2[j]){
                str[i] = str3[j];
            }
            else if(str[i]==str3[j]){
                str[i] = str2[j];
            }
        }
    }
    cout<<str<<endl;

    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include<cstring>
#include<string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>

typedef unsigned long long ull;
typedef unsigned long ul;
typedef long long ll;

#define MAX 1000007

using namespace std;

char str[MAX];
char str2[MAX];
char str3[MAX];

int main()
{
    int i,j;
    int n,m;
    cin>>n>>m;
    cin>>str;

    for(i = 0; i<m; i++)
        cin>>str2[i]>>str3[i];

    for(j = 0; j<m; j++){
        for(i = 0; i<n; i++){
            if(str[i]==str2[j]){
                str[i] = str3[j];
                continue;
            }
            if(str[i]==str3[j]){
                str[i] = str2[j];
                continue;
            }
        }
    }
    cout<<str<<endl;

    return 0;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cctype>
#include <set>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    string g;
    cin>>g;
    map <char,int> memo,mani;
    map <int,char> print;
    int cn=1;
    for(int i=0; i<n; i++){
        char p=g[i];
        if(!memo[p]){
            memo[p]=cn;
            mani[p]=cn;
            print[cn]=p;
            cn++;
        }

    }
    for(int j=0; j<m; j++){
        char uno,dos;
        cin>>uno>>dos;
        if(mani[uno]!=0 && mani[dos]!=0){
            int pos1=mani[uno],pos2=mani[dos];
            mani[uno]=pos2;
            mani[dos]=pos1;
            print[pos1]=dos;
            print[pos2]=uno;
        }
        else if(mani[uno]!=0){
            int pos1=mani[uno];
            mani.erase(uno);
            mani[dos]=pos1; //le pongo la pos del 1
            print[pos1]=dos;
        }else if(mani[dos]!=0){
            int pos2=mani[dos];
            mani.erase(dos);
            mani[uno]=pos2; //le pongo la pos del 2
            print[pos2]=uno;
        }
    }

    //string pn;
    for(int k=0; k<n; k++){
        char d=g[k];
        int he=memo[d];
        char p2=print[he];
        g[k]=p2;
    }
    cout<<g<<endl;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cctype>
#include <set>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    int n,m;
    cin>>n>>m;
    string g;
    cin>>g;
    int mani[26];
    char print[26];
    memset(mani,-1,sizeof(mani));
    for(int i=0; i<n; i++){
        char p=g[i];
        mani[p-'a']=(p-'a');
        print[p-'a']=p;
    }
     for(int j=0; j<m; j++){
        char uno,dos;
        cin>>uno>>dos;
        if(mani[uno-'a']!=-1 && mani[dos-'a']!=-1){
            int pos1=mani[uno-'a'],pos2=mani[dos-'a'];
            mani[uno-'a']=pos2;
            mani[dos-'a']=pos1;
            print[pos1]=dos;
            print[pos2]=uno;
        }
        else if(mani[uno-'a']!=-1){
            int pos1=mani[uno-'a'];
            mani[uno-'a']=-1;
            mani[dos-'a']=pos1; //le pongo la pos del 1
            print[pos1]=dos;
        }else if(mani[dos-'a']!=-1){
            int pos2=mani[dos-'a'];
            mani[dos-'a']=-1;
            mani[uno-'a']=pos2; //le pongo la pos del 2
            print[pos2]=uno;
        }
    }

    string pn;
    for(int k=0; k<n; k++){
        char d=g[k];
        int he=d-'a';
        char p2=print[he];
        pn=pn+p2;
    }
    cout<<pn<<endl;

}


#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cctype>
#include <set>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    int n,m;
    cin>>n>>m;
    string g;
    cin>>g;
    int mani[26];
    char print[26];
    memset(mani,-1,sizeof(mani));
    for(int i=0; i<n; i++){
        char p=g[i];
        mani[p-'a']=(p-'a');
        print[p-'a']=p;
    }
     for(int j=0; j<m; j++){
        char uno,dos;
        cin>>uno>>dos;
        if(mani[uno-'a']!=-1 && mani[dos-'a']!=-1){
            int pos1=mani[uno-'a'],pos2=mani[dos-'a'];
            mani[uno-'a']=pos2;
            mani[dos-'a']=pos1;
            print[pos1]=dos;
            print[pos2]=uno;
        }
        else if(mani[uno-'a']!=-1){
            int pos1=mani[uno-'a'];
            mani[uno-'a']=-1;
            mani[dos-'a']=pos1; //le pongo la pos del 1
            print[pos1]=dos;
        }else if(mani[dos-'a']!=-1){
            int pos2=mani[dos-'a'];
            mani[dos-'a']=-1;
            mani[uno-'a']=pos2; //le pongo la pos del 2
            print[pos2]=uno;
        }
    }
    for(int k=0; k<n; k++){
        char d=g[k];
        int he=d-'a';
        char p2=print[he];
        g[k]=p2;
    }
    cout<<g<<endl;

}


#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cctype>
#include <set>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    string g;
    cin>>g;
    map <char,int> memo,mani;
    map <int,char> print;
    int cn=1;
    for(int i=0; i<n; i++){
        char p=g[i];
        if(!memo[p]){
            memo[p]=cn;
            mani[p]=cn;
            print[cn]=p;
            cn++;
        }

    }
    for(int j=0; j<m; j++){
        char uno,dos;
        cin>>uno>>dos;
        if(mani[uno]!=0 && mani[dos]!=0){
            int pos1=mani[uno],pos2=mani[dos];
            mani[uno]=pos2;
            mani[dos]=pos1;
            print[pos1]=dos;
            print[pos2]=uno;
        }
        else if(mani[uno]!=0){
            int pos1=mani[uno];
            mani.erase(uno);
            mani[dos]=pos1; //le pongo la pos del 1
            print[pos1]=dos;
        }else if(mani[dos]!=0){
            int pos2=mani[dos];
            mani.erase(dos);
            mani[uno]=pos2; //le pongo la pos del 2
            print[pos2]=uno;
        }
    }

    string pn;
    for(int k=0; k<n; k++){
        char d=g[k];
        int he=memo[d];
        char p2=print[he];
        pn=pn+p2;
    }
    cout<<pn<<endl;

}


#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cctype>
#include <set>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    string g;
    cin>>g;
    map <char,int> memo;
    int mani[26];
    char print[26];
    memset(mani,-1,sizeof(mani));
    for(int i=0; i<n; i++){
        char p=g[i];
        mani[p-'a']=(p-'a');
        print[p-'a']=p;
    }
     for(int j=0; j<m; j++){
        char uno,dos;
        cin>>uno>>dos;
        if(mani[uno-'a']!=-1 && mani[dos-'a']!=-1){
            int pos1=mani[uno-'a'],pos2=mani[dos-'a'];
            mani[uno-'a']=pos2;
            mani[dos-'a']=pos1;
            print[pos1]=dos;
            print[pos2]=uno;
        }
        else if(mani[uno-'a']!=-1){
            int pos1=mani[uno-'a'];
            mani[uno-'a']=-1;
            mani[dos-'a']=pos1; //le pongo la pos del 1
            print[pos1]=dos;
        }else if(mani[dos-'a']!=-1){
            int pos2=mani[dos-'a'];
            mani[dos-'a']=-1;
            mani[uno-'a']=pos2; //le pongo la pos del 2
            print[pos2]=uno;
        }
    }

    string pn;
    for(int k=0; k<n; k++){
        char d=g[k];
        int he=d-'a';
        char p2=print[he];
        pn=pn+p2;
    }
    cout<<pn<<endl;

}


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

char s[200500];
char xx[5], yy[5];
int f[105], g[105];
int main()
{
   freopen("in.txt", "r", stdin);
   int n, m;
   scanf("%d%d", &n, &m);
   scanf("%s", &s);
   for(int i = 1; i <= 26; i++){
        f[i] = i;
   }
   while(m--){
        scanf("%s %s", &xx, &yy);
        int x = xx[0] - 'a' + 1;
        int y = yy[0] - 'a' + 1;
        swap(f[x], f[y]);
   }
   for(int i = 1; i <= 26; i++){
        g[f[i]] = i;
   }
   for(int i = 0; i < strlen(s); i++){
        int ch = s[i] + 1 - 'a';
        printf("%c", g[ch] + 'a' - 1);
   }
   printf("\n");
}


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

char s[200500];
char xx[5], yy[5];
int f[105], g[105];
int main()
{
   //freopen("in.txt", "r", stdin);
   int n, m;
   scanf("%d%d", &n, &m);
   scanf("%s", &s);
   for(int i = 0; i < 26; i++){
        f[i] = i;
   }
   while(m--){
        scanf("%s %s", &xx, &yy);
        int x = xx[0] - 'a';
        int y = yy[0] - 'a';
        swap(f[x], f[y]);
   }
   for(int i = 0; i < 26; i++){
        g[f[i]] = i;
   }
   for(int i = 0; i < n; i++){
        int ch = s[i] - 'a';
        printf("%c", g[ch] + 'a');
   }
   printf("\n");
}


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

char s[200500];
char xx[5], yy[5];
int f[105], g[105];
int main()
{
   //freopen("in.txt", "r", stdin);
   int n, m;
   scanf("%d%d", &n, &m);
   scanf("%s", &s);
   for(int i = 1; i <= 26; i++){
        f[i] = i;
   }
   while(m--){
        scanf("%s %s", &xx, &yy);
        int x = xx[0] - 'a' + 1;
        int y = yy[0] - 'a' + 1;
        swap(f[x], f[y]);
   }
   for(int i = 1; i <= 26; i++){
        g[f[i]] = i;
   }
   for(int i = 0; i < strlen(s); i++){
        int ch = s[i] + 1 - 'a';
        printf("%c", g[ch] + 'a' - 1);
   }
   printf("\n");
}


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

char s[200500];
int f[105], g[105];
int main()
{
   //freopen("in.txt", "r", stdin);
   int n, m;
   scanf("%d%d", &n, &m);
   scanf("%s", &s);
   for(int i = 1; i <= 26; i++){
        f[i] = i;
   }
   while(m--){
        char xx, yy;
        scanf("\n%c %c", &xx, &yy);
        int x = xx - 'a' + 1;
        int y = yy - 'a' + 1;
        swap(f[x], f[y]);
   }
   for(int i = 1; i <= 26; i++){
        g[f[i]] = i;
   }
   for(int i = 0; i < strlen(s); i++){
        int ch = s[i] + 1 - 'a';
        printf("%c", g[ch] + 'a' - 1);
   }
   printf("\n");
}


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

char s[200500];
int f[105], g[105];
int main()
{
   freopen("in.txt", "r", stdin);
   int n, m;
   scanf("%d%d", &n, &m);
   scanf("%s", &s);
   for(int i = 1; i <= 26; i++){
        f[i] = i;
   }
   while(m--){
        char xx, yy;
        scanf("\n%c %c", &xx, &yy);
        int x = xx - 'a' + 1;
        int y = yy - 'a' + 1;
        swap(f[x], f[y]);
   }
   for(int i = 1; i <= 26; i++){
        g[f[i]] = i;
   }
   for(int i = 0; i < strlen(s); i++){
        int ch = s[i] + 1 - 'a';
        printf("%c", g[ch] + 'a' - 1);
   }
   printf("\n");
}


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

char s[200005];
int f[30];
int main()
{
   // freopen("in.txt", "r", stdin);
   int n, m;
   scanf("%d%d", &n, &m);
   scanf("%s", &s);
   for(int i = 1; i <= 26; i++){
        f[i] = i;
   }
   scanf("\n");
   while(m--){
        char xx, yy;
        scanf("%c %c\n", &xx, &yy);
        int x = xx + 1 - 'a';
        int y = yy + 1 - 'a';
        for(int i = 1; i <= 26; i++){
            if(f[i] == x){
                f[i] = y;
                continue;
            }
            if(f[i] == y){
                f[i] = x;
                continue;
            }
        }
   }
   for(int i = 0; i < strlen(s); i++){
        int ch = s[i] + 1 - 'a';
        printf("%c", f[ch] + 'a' - 1);
   }
   printf("\n");
}


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

char s[200500];
int f[105];
int main()
{
   //freopen("in.txt", "r", stdin);
   int n, m;
   while(~scanf("%d%d", &n, &m)){
       scanf("%s", &s);
       for(int i = 1; i <= 26; i++){
            f[i] = i;
       }
       scanf("\n");
       while(m--){
            char xx, yy;
            scanf("%c %c\n", &xx, &yy);
            int x = xx + 1 - 'a';
            int y = yy + 1 - 'a';
            for(int i = 1; i <= 26; i++){
                if(f[i] == x){
                    f[i] = y;
                    continue;
                }
                if(f[i] == y){
                    f[i] = x;
                    continue;
                }
            }
       }
       for(int i = 0; i < strlen(s); i++){
            int ch = s[i] + 1 - 'a';
            printf("%c", f[ch] + 'a' - 1);
       }
       printf("\n");
   }
}


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

const int LEN = 2e5 + 5;
char s[LEN];
char mp[26];
int pos[26];
int main()
{
    for (int i = 0; i < 26; i++) pos[i] = i;
    int n, m; scanf("%d%d%s", &n, &m, s);
    while (m--){
        char x[2], y[2]; scanf("%s %s", x, y);
        swap(pos[*x - 'a'], pos[*y - 'a']);
    }
    for (int i = 0; i < 26; i++) mp[pos[i]] = i + 'a';
    for (int i = 0; i < n; i++) printf("%c", (mp[s[i] - 'a']));
    printf("\n");
    return 0;
}


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

const int LEN = 2e5 + 5;
char s[LEN];
char mp[26];
int pos[26];
int main()
{
   // freopen("in.txt", "r", stdin);
    for(int i = 0; i < 26; i++){
        pos[i] = i;
    }
    int n, m;
    scanf("%d%d", &n, &m);
    scanf("%s", &s);
    while (m--){
        char x[2], y[2];
        scanf("%s %s", x, y);
        int xx = x[0] - 'a';
        int yy = y[0] - 'a';
        swap(pos[xx], pos[yy]);
    }
    for (int i = 0; i < 26; i++)
        mp[pos[i]] = i;
    for (int i = 0; i < n; i++) printf("%c", (mp[s[i] - 'a'] + 'a'));
    printf("\n");
    return 0;
}


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

char s[200500];
int f[105], g[105];
int main()
{
  // freopen("in.txt", "r", stdin);
   int n, m;
   while(~scanf("%d%d", &n, &m)){
       scanf("%s", &s);
       for(int i = 1; i <= 26; i++){
            f[i] = i;
       }
       scanf("\n");
       while(m--){
            char xx, yy;
            scanf("%c %c\n", &xx, &yy);
            int x = xx - 'a' + 1;
            int y = yy - 'a' + 1;
            swap(f[x], f[y]);
       }
       for(int i = 1; i <= 26; i++){
            g[f[i]] = i;
       }
       for(int i = 0; i < strlen(s); i++){
            int ch = s[i] + 1 - 'a';
            printf("%c", g[ch] + 'a' - 1);
       }
       printf("\n");
   }
}


#include <iostream>
#include <cstring>
using namespace std;
const int maxn=2000010;
int main (){
    int len,cnt;
    char s,d,st[maxn],word[26];
    cin >> len >> cnt;
    cin >> st;
    for(int i=0;i<26;++i){
        word[i]='a'+i;
    }
    for(int i=1;i<=cnt;++i){
        cin >> s >> d;
        for(int j=0;j<26;++j){
            if(st[j]==s) st[j]=d;
            else if(st[j]==d) st[j]=s;
        }
    }
    for(int i=0;i<len;++i)
        cout << word[st[i]-'a'];
    cout << endl;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
vector<char>v;
int main (){
	int a,b;
	char ch,c,d;
	cin >> a >> b;
	for(int i=0;i<a;++i)
	{
		cin >> ch;
		v.push_back(ch);
	}
	cin.get();
	for(int i=0;i<b;++i){
		cin >> c >> d;
		for(size_t i=0;i<v.size();++i)
		{
			if(v[i]==c) v[i]=d;
			else if(v[i]==d) v[i]=c;
		}
	}
	for(size_t i =0;i<v.size();++i)
		cout << v[i];
	cout << endl;
	return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
const int maxn=2000010;
int main (){
    int len,cnt;
    char s,d,st[maxn],word[26];
    cin >> len >> cnt;
    cin >> st;
    for(int i=0;i<26;++i){
        word[i]='a'+i;
    }
    for(int i=1;i<=cnt;++i){
        cin >> s >> d;
        if(s==d) continue;
        for(int j=0;j<26;++j){
            if(word[j]==s) word[j]=d;
            else if(word[j]==d) word[j]=s;
        }
    }
    for(int i=0;i<len;++i)
        cout << word[st[i]-'a'];
    cout << endl;
    return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
const int maxn=2000010;
int main (){
    int len,cnt;
    char s,d,st[maxn],word[26];
    cin >> len >> cnt;
    cin >> st;
    for(int i=0;i<26;++i){
        word[i]='a'+i;
    }
    for(int i=1;i<=cnt;++i){
        cin >> s >> d;
        if(s==d) continue;
        for(int j=0;j<26;++j){
            if(st[j]==s) st[j]=d;
            else if(st[j]==d) st[j]=s;
        }
    }
    for(int i=0;i<len;++i)
        cout << word[st[i]-'a'];
    cout << endl;
    return 0;
}


#include <iostream>
#include <cstring>
#include <string>
using namespace std;
const int maxn=2000010;
int main (){
    int len,cnt;
    char s,d;
    string st,t1,t2;
    cin >> len >> cnt;
    cin >> st;
    for(int i=1;i<=cnt;++i){
        cin >> s >> d;
        for(int j=0;j<st.length();++j){
            if(st[j]==s) st[j]=d;
            else if(st[j]==d) st[j]=s;
        }
    }
    cout << st << endl;
    return 0;
}


#include <cstdio>
#include <string.h>

using namespace std;

int main(){

int n, m, i, j, k;
char s[200010], a[200010], b[200010];

scanf("%d %d", &n, &m);
scanf("%s", &s);
k=strlen(s);

for(i=0;i<m;i++){
    scanf("%s %s", &a[i], &b[i]);
    if(a[i]!=b[i])
    for(j=0;j<k;j++){
        if(s[j]==a[i])s[j]=b[i];
        else if(s[j]==b[i])s[j]=a[i];
    }
}
for(i=0;i<k;i++)
printf("%c", s[i]);

return 0;
}


#include <cstdio>
#include <string.h>

using namespace std;

int main(){

int n, m, i, j, k;
char s[200010], a[200010], b[200010];

scanf("%d %d", &n, &m);
scanf("%s", &s);
k=strlen(s);

for(i=0;i<m;i++){
    scanf("%s %s", &a[i], &b[i]);
    for(j=0;j<k;j++){
        if(s[j]==a[i])s[j]=b[i];
        else if(s[j]==b[i])s[j]=a[i];
    }
}
for(i=0;i<k;i++)
printf("%c", s[i]);

return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dis[27];
char ch[200010];
int main()
{
    char sh,mh;
    int m,n,i,j;
    for(i=1;i<=26;i++)
        dis[i]=i;
    scanf("%d%d",&n,&m);
    scanf("%s",&ch);
    getchar();
    for(i=1;i<=m;i++)
    {
        scanf("%c",&sh);
        int x=sh-'a'+1;
        getchar();
        scanf("%c",&mh);
        int y=mh-'a'+1;
        getchar();
        for(j=1;j<=26;j++)
        {
            if(dis[j]==x)
            {
                dis[j]=y;
                continue;
            }
            if(dis[j]==y)
                dis[j]=x;

        }
    }
    for(i=0;i<n;i++)
    {
        int x=ch[i]-'a'+1;
        char y=dis[x]+'a'-1;
        ch[i]=y;
    }
    printf("%s",ch);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char ch[200010],x,y;
int n,m,i,j;
int main()
{
	scanf("%d %d",&n,&m);
	scanf("%s",&ch);
    getchar();
	for (i=1;i<=m;i++)
	{
		scanf("%c %c",&x,&y);
		getchar();
		for (j=0;j<n;j++)
		{
		  if (ch[j]==x)
		  {
		  	ch[j]=y;
		  	continue;
		  }
		  if (ch[j]==y)
		    ch[j]=x;
		}
	}
	printf("%s",ch);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char ch[100],x,y,z,ans[150];
int a[100],n,m,i,b;
int main()
{
	for (i=1;i<=26;i++)
	  ans[i]=i+'a'-1;
	scanf("%d %d",&n,&m);
	gets(ch);
	gets(ch);
	for (i=1;i<=26;i++)
	  a[i]=i;
	for (i=1;i<=m;i++)
	{
		if (i==m) scanf("%c %c",&x,&y);
		 else scanf("%c %c ",&x,&y);
		z=ans[a[x-'a'+1]];
		ans[a[x-'a'+1]]=ans[a[y-'a'+1]];
		ans[a[y-'a'+1]]=z;
		b=a[x-'a'+1];
		a[x-'a'+1]=a[y-'a'+1];
		a[y-'a'+1]=b;
	}
	for (i=0;i<n;i++)
	  ch[i]=ans[ch[i]-'a'+1];
	printf("%s",ch);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char ch[100],x,y,z,ans[150];
int a[100],n,m,i,b;
int main()
{
	for (i=1;i<=26;i++)
	  ans[i]=i+'a'-1;
	scanf("%d %d",&n,&m);
	scanf("%s",&ch);
    getchar();
	for (i=1;i<=26;i++)
	  a[i]=i;
	for (i=1;i<=m;i++)
	{
		scanf("%c %c",&x,&y);
		getchar();
		z=ans[a[x-'a'+1]];
		ans[a[x-'a'+1]]=ans[a[y-'a'+1]];
		ans[a[y-'a'+1]]=z;
		b=a[x-'a'+1];
		a[x-'a'+1]=a[y-'a'+1];
		a[y-'a'+1]=b;
	}
	for (i=0;i<n;i++)
	  ch[i]=ans[ch[i]-'a'+1];
	printf("%s",ch);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char ch[100],x,y,z,ans[150];
int a[100],n,m,i,b,j;
int main()
{
	for (i=1;i<=26;i++)
	  ans[i]=i+'a'-1;
	scanf("%d %d",&n,&m);
	scanf("%s",&ch);
    getchar();
	//for (i=1;i<=26;i++)
	//  a[i]=i;
	for (i=1;i<=m;i++)
	{
		scanf("%c %c",&x,&y);
		getchar();
		/*
		z=ans[a[x-'a'+1]];
		ans[a[x-'a'+1]]=ans[a[y-'a'+1]];
		ans[a[y-'a'+1]]=z;
		b=a[x-'a'+1];
		a[x-'a'+1]=a[y-'a'+1];
		a[y-'a'+1]=b;*/
		for (j=1;j<=26;j++)
		{
		  if (ans[j]==x)
		  {
		  	ans[j]=y;
		  	continue;
		  }
		  if (ans[j]==y)
		    ans[j]=x;
	    }
	}
	for (i=0;i<n;i++)
	  ch[i]=ans[ch[i]-'a'+1];
	printf("%s",ch);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char ch[100],x,y,z,ans[150];
int a[100],n,m,i,b;
int main()
{
	for (i=1;i<=26;i++)
	  ans[i]=i+'a'-1;
	scanf("%d %d",&n,&m);
	gets(ch);
	gets(ch);
	for (i=1;i<=26;i++)
	  a[i]=i;
	for (i=1;i<=m;i++)
	{
		if (i==m) scanf("%c %c",&x,&y);
		 else scanf("%c %c ",&x,&y);
		z=ans[a[x-'a'+1]];
		ans[a[x-'a'+1]]=ans[a[y-'a'+1]];
		ans[a[y-'a'+1]]=z;
		b=a[x-'a'+1];
		a[x-'a'+1]=a[y-'a'+1];
		a[y-'a'+1]=b;
	}
	for (i=0;i<n;i++)
	  printf("%c",ans[ch[i]-'a'+1]);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char ch[100],x,y,z;
int a[100],n,m,i,b,j,ans[150];
int main()
{
	for (i=1;i<=26;i++)
	  ans[i]=i+'a'-1;
	scanf("%d %d",&n,&m);
	scanf("%s",&ch);
    getchar();
	//for (i=1;i<=26;i++)
	//  a[i]=i;
	for (i=1;i<=m;i++)
	{
		scanf("%c %c",&x,&y);
		getchar();
		/*
		z=ans[a[x-'a'+1]];
		ans[a[x-'a'+1]]=ans[a[y-'a'+1]];
		ans[a[y-'a'+1]]=z;
		b=a[x-'a'+1];
		a[x-'a'+1]=a[y-'a'+1];
		a[y-'a'+1]=b;*/
		for (j=1;j<=26;j++)
		{
		  if (ans[j]==x)
		  {
		  	ans[j]=y;
		  	continue;
		  }
		  if (ans[j]==y)
		    ans[j]=x;
	    }
	}

	for (i=0;i<n;i++)
	  ch[i]=ans[ch[i]-'a'+1];
	printf("%s",ch);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char ch[10000],x,y,z,ans[27];
int a[10000],n,m,i,b;
int main()
{
	for (i=1;i<=26;i++)
	  ans[i]=i+'a'-1;
	cin>>n>>m;
	gets(ch);
	gets(ch);
	for (i=1;i<=26;i++)
	  a[i]=i;
	for (i=1;i<=m;i++)
	{
		if (i==m) scanf("%c %c",&x,&y);
		 else scanf("%c %c ",&x,&y);
		z=ans[a[x-'a'+1]];
		ans[a[x-'a'+1]]=ans[a[y-'a'+1]];
		ans[a[y-'a'+1]]=z;
		b=a[x-'a'+1];
		a[x-'a'+1]=a[y-'a'+1];
		a[y-'a'+1]=b;
	}
	for (i=0;i<n;i++)
	  printf("%c",ans[ch[i]-'a'+1]);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char ch[10000],x,y,z,p,ans[27];
int a[10000],n,m,i;
int main()
{
	for (i=1;i<=26;i++)
	  ans[i]=i+'a'-1;
	cin>>n>>m;
	gets(ch);
	gets(ch);
	for (i=1;i<=26;i++)
	  a[i]=i;
	for (i=1;i<=m;i++)
	{
		//scanf("%c %c ",&x,&y);
		cin>>x>>y;
		swap(ans[a[x-'a'+1]],ans[a[y-'a'+1]]);
		swap(a[x-'a'+1],a[y-'a'+1]);
	}
	for (i=0;i<n;i++)
	  printf("%c",ans[ch[i]-'a'+1]);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char ch[200000],x,y,z,ans[150];
int a[100],n,m,i,b;
int main()
{
	for (i=1;i<=26;i++)
	  ans[i]=i+'a'-1;
	scanf("%d %d",&n,&m);
	scanf("%s",&ch);
    getchar();
	for (i=1;i<=26;i++)
	  a[i]=i;
	for (i=1;i<=m;i++)
	{
		scanf("%c %c",&x,&y);
		getchar();
		z=ans[a[x-'a'+1]];
		ans[a[x-'a'+1]]=ans[a[y-'a'+1]];
		ans[a[y-'a'+1]]=z;
		b=a[x-'a'+1];

		a[x-'a'+1]=a[y-'a'+1];
		a[y-'a'+1]=b;
	}
	for (i=0;i<n;i++)
	  ch[i]=ans[ch[i]-'a'+1];
	printf("%s",ch);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char ch[100],x,y,z,ans[150];
int a[100],n,m,i,b;
int main()
{
	for (i=1;i<=26;i++)
	  ans[i]=i+'a'-1;
	scanf("%d %d",&n,&m);
	scanf("%s",&ch);
    getchar();
	for (i=1;i<=26;i++)
	  a[i]=i;
	for (i=1;i<=m;i++)
	{
		scanf("%c %c",&x,&y);
		getchar();
		z=ans[a[x-'a'+1]];
		ans[a[x-'a'+1]]=ans[a[y-'a'+1]];
		ans[a[y-'a'+1]]=z;
		b=a[x-'a'+1];

		a[x-'a'+1]=a[y-'a'+1];
		a[y-'a'+1]=b;
	}
	for (i=0;i<n;i++)
	  ch[i]=ans[ch[i]-'a'+1];
	printf("%s",ch);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char ch[10000],x,y,z,p,ans[27];
int a[10000],n,m,i;
int main()
{
	for (i=1;i<=26;i++)
	  ans[i]=i+'a'-1;
	cin>>n>>m;
	gets(ch);
	gets(ch);
	for (i=1;i<=26;i++)
	  a[i]=i;
	for (i=1;i<=m;i++)
	{
		if (i==m) scanf("%c %c",&x,&y);
		 else scanf("%c %c ",&x,&y);
		swap(ans[a[x-'a'+1]],ans[a[y-'a'+1]]);
		swap(a[x-'a'+1],a[y-'a'+1]);
	}
	for (i=0;i<n;i++)
	  printf("%c",ans[ch[i]-'a'+1]);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char ch[100],x,y,z;
int a[100],n,m,i,b,j,ans[150];
int main()
{
	for (i=1;i<=26;i++)
	  ans[i]=i+'a'-1;
	scanf("%d %d",&n,&m);
	scanf("%s",&ch);
    getchar();
	//for (i=1;i<=26;i++)
	//  a[i]=i;
	for (i=1;i<=m;i++)
	{
		scanf("%c %c",&x,&y);
		getchar();
		/*
		z=ans[a[x-'a'+1]];
		ans[a[x-'a'+1]]=ans[a[y-'a'+1]];
		ans[a[y-'a'+1]]=z;
		b=a[x-'a'+1];
		a[x-'a'+1]=a[y-'a'+1];
		a[y-'a'+1]=b;*/
		for (j=1;j<=26;j++)
		{
		  if (ans[j]==x)
		  {
		  	ans[j]=y;
		  	continue;
		  }
		  if (ans[j]==y)
		    ans[j]=x;
	    }
	}
	for (i=0;i<n;i++)
	  ch[i]=ans[ch[i]-'a'+1];
	printf("%s",ch);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char ch[100],x,y,z,ans[150];
int a[100],n,m,i,b;
int main()
{
	for (i=1;i<=26;i++)
	  ans[i]=i+'a'-1;
	scanf("%d %d",&n,&m);
	scanf("%s",&ch);

    getchar();
	for (i=1;i<=26;i++)
	  a[i]=i;
	for (i=1;i<=m;i++)
	{
		scanf("%c %c",&x,&y);
		getchar();
		z=ans[a[x-'a'+1]];
		ans[a[x-'a'+1]]=ans[a[y-'a'+1]];
		ans[a[y-'a'+1]]=z;
		b=a[x-'a'+1];
		a[x-'a'+1]=a[y-'a'+1];
		a[y-'a'+1]=b;
	}
	for (i=0;i<n;i++)
	  ch[i]=ans[ch[i]-'a'+1];
	printf("%s",ch);
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
	char ch[27];
	for(int i =0;i<26;i++)
	{
		ch[i] = 'a'+i;
	}
	ch[26]='\0';

	int n,m;
	cin>>n>>m;
	string s;
	cin>>s;
	char a,b;
	for(int i =0;i<m;i++)
	{
		cin>>a>>b;
		for(int j=0;j<26;j++)
		{
			if(ch[j]==a)
			{
				ch[j]=b;
				continue;
			}
			if(ch[j]==b)
			{
				ch[j]=a;
				continue;
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		s[i]=ch[s[i]-'a'];
	}
	cout<<s;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	string s;
	char temp1[2],temp2[2];
	int adj[2]={0,0};
	vector<char*> t;
	cin>>s;
	for(int i =0;i<m;i++)
	{
		adj[0]=1;
		adj[1]=1;
		cin>>temp1[0]>>temp1[1];
		temp2[0] = temp1[1];
		temp2[1] = temp1[0];
		if(t.empty)
		{
			t.push_back(temp1);
			t.push_back(temp2);
		}
		else
		{
			for(int j = 0;j<t.size();j++)
			{
				if(t[j][1] == temp1[0])
				{
					t[j][1] = temp1[1];
					adj[0] = 0;
				}
				if(t[j][1] == temp1[1])
				{
					t[j][1] = temp1[0];
					adj[1] = 0;
				}
			}
			if(adj[0]==1)
				t.push_back(temp1);
			if(adj[1]==1)
				t.push_back(temp2);
		}
	}
	for(int i =0;i<n;i++)
	{
		for(int j=0;j<t.size();j++)
		{
			if(s[i] == t[j][0])
			{
				s[i] = t[j][1];
				break;
			}
		}
	}
	cout<<s;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	string s;
	char temp1[2],temp2[2];
	int adj[2]={0,0};
	vector<char*> t;
	cin>>s;
	for(int i =0;i<m;i++)
	{
		adj[0]=1;
		adj[1]=1;
		cin>>temp1[0]>>temp1[1];
		temp2[0] = temp1[1];
		temp2[1] = temp1[0];
		if(t.empty())
		{
			t.push_back(temp1);
			t.push_back(temp2);
		}
		else
		{
			for(int j = 0;j<t.size();j++)
			{
				if(t[j][1] == temp1[0])
				{
					t[j][1] = temp1[1];
					adj[0] = 0;
				}
				if(t[j][1] == temp1[1])
				{
					t[j][1] = temp1[0];
					adj[1] = 0;
				}
			}
			if(adj[0]==1)
				t.push_back(temp1);
			if(adj[1]==1)
				t.push_back(temp2);
		}
	}
	for(int i =0;i<n;i++)
	{
		for(int j=0;j<t.size();j++)
		{
			if(s[i] == t[j][0])
			{
				s[i] = t[j][1];
				break;
			}
		}
	}
	cout<<s;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	string s;
	int adj[2]={0,0};
	vector<char*> t;
	cin>>s;
	for(int i =0;i<m;i++)
	{
		char *temp1 = new char[3];
		char *temp2 = new char[3];
		adj[0]=1;
		adj[1]=1;
		cin>>temp1[0]>>temp1[1];
		temp1[2] = '\0';
		temp2[0] = temp1[1];
		temp2[1] = temp1[0];
		temp2[2] = '\0';
		if(t.empty())
		{
			t.push_back(temp1);
			t.push_back(temp2);
		}
		else
		{
			for(int j = 0;j<t.size();j++)
			{
				if(t[j][1] == temp1[0])
				{
					t[j][1] = temp1[1];
					adj[0] = 0;
					continue;
				}
				if(t[j][1] == temp1[1])
				{
					t[j][1] = temp1[0];
					adj[1] = 0;
				}
			}
			if(adj[0]==1)
				t.push_back(temp1);
			if(adj[1]==1)
				t.push_back(temp2);
		}
	}
	for(int i =0;i<n;i++)
	{
		for(int j=0;j<t.size();j++)
		{
			if(s[i] == t[j][0])
			{
				s[i] = t[j][1];
				break;
			}
		}
	}
	cout<<s;
	return 0;
}


#include <iostream>
#include <string>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	string s;
	char a,b;
	cin>>s;
	for(int i =0;i<m;i++)
	{
		cin>>a>>b;
		for(int j=0;j<n;j++)
		{
			if(s[j] == a)
			{
				s[j] = b;
				continue;
			}
			if(s[j] == b)
				s[j] = a;
		}
	}
	cout<<s;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	string s;
	int adj[2]={0,0};
	vector<char*> t;
	cin>>s;
	for(int i =0;i<m;i++)
	{
		char temp1[2],temp2[2];
		adj[0]=1;
		adj[1]=1;
		cin>>temp1[0]>>temp1[1];
		temp2[0] = temp1[1];
		temp2[1] = temp1[0];
		if(t.empty())
		{
			t.push_back(temp1);
			t.push_back(temp2);
		}
		else
		{
			for(int j = 0;j<t.size();j++)
			{
				if(t[j][1] == temp1[0])
				{
					t[j][1] = temp1[1];
					adj[0] = 0;
				}
				if(t[j][1] == temp1[1])
				{
					t[j][1] = temp1[0];
					adj[1] = 0;
				}
			}
			if(adj[0]==1)
				t.push_back(temp1);
			if(adj[1]==1)
				t.push_back(temp2);
		}
	}
	for(int i =0;i<n;i++)
	{
		for(int j=0;j<t.size();j++)
		{
			if(s[i] == t[j][0])
			{
				s[i] = t[j][1];
				break;
			}
		}
	}
	cout<<s;
	return 0;
}


#include <iostream>
#include <string>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	string s;
	char a,b;
	cin>>s;
	for(int i =0;i<m;i++)
	{
		cin>>a>>b;
		for(int j=0;j<n;j++)
		{
			if(s[j] == a)
				s[j] = b;
			if(s[j] == b)
				s[j] = a;
		}
	}
	cout<<s;
	return 0;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cctype>
#include <set>
using namespace std;
#define pb push_back
#define f(i,j,k,l) for(int i = j; i < k; i+=l)
#define sci(x) scanf("%d",&x)
#define scii(x,y) scanf("%d %d",&x,&y)
#define sciii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define mp make_pair
#define INF 0x3f3f3f3f
#define EPS 1e-9
#define PI 3.1415926535897932384626
#define MOD 1000000007
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;

typedef pair<int, int> ii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<string> vs;
typedef vector <bool> vb;
typedef vector <char> vc;

typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, vi> miv;

ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
ll mcm(ll a, ll b){ return a * b / gcd(a, b); }
ll sq(ll a) { return a*a; }
ll pot(ll a, ll b){ return b ? sq(pot(a, b >> 1))*(b & 1 ? a : 1) : 1; }
ll potMod(ll a, ll e){ return e ? ((sq(potMod(a, e >> 1)) % MOD)*(e & 1 ? a : 1)) % MOD : 1; }
vc a[300];
string p;
char find(char dad, char vec){
	if(a[vec].size() == 0){return '8';}
	if(a[vec].back() == dad){return vec;}
	else{find(dad,a[vec].back());}
}
int main(){
	int m,n;
	char mem1,mem2;
	scii(n,m);
	cin >> p;
	f(i,0,m,1){
		mem1 = mem2 = '8';
		char x,y;
		cin >> x >> y;
		if(x!=y){
			f(j,0,a[x].size(),1){
				mem1 = find(x,a[x][j]);
				if(mem1 != '8'){break;}
			}
			f(j,0,a[y].size(),1){
				mem2 = find(y,a[y][j]);
				if(mem2 != '8'){break;}
			}
			if(a[x].size() == 0){a[x].pb(y);}
			else{a[mem1].pb(y);}
			if(a[y].empty()){a[y].pb(x);}
			else{a[mem2].pb(x);}
		}
	}
	f(i,0,n,1){
		if(!a[p[i]].empty()){
			p[i] = a[p[i]].back();
		}
	}
	cout << p << endl;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cctype>
#include <set>
using namespace std;
#define pb push_back
#define f(i,j,k,l) for(int i = j; i < k; i+=l)
#define sci(x) scanf("%d",&x)
#define scii(x,y) scanf("%d %d",&x,&y)
#define sciii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define mp make_pair
#define INF 0x3f3f3f3f
#define EPS 1e-9
#define PI 3.1415926535897932384626
#define MOD 1000000007
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;

typedef pair<int, int> ii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<string> vs;
typedef vector <bool> vb;
typedef vector <char> vc;

typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, vi> miv;

ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
ll mcm(ll a, ll b){ return a * b / gcd(a, b); }
ll sq(ll a) { return a*a; }
ll pot(ll a, ll b){ return b ? sq(pot(a, b >> 1))*(b & 1 ? a : 1) : 1; }
ll potMod(ll a, ll e){ return e ? ((sq(potMod(a, e >> 1)) % MOD)*(e & 1 ? a : 1)) % MOD : 1; }
vc a[300];
string p;
char find(char dad, char vec){
	if(a[vec].size() == 0){return '8';}
	if(a[vec].back() == dad){return vec;}
	else{find(dad,a[vec].back());}
}
int main(){
	int m,n;
	char mem1,mem2;
	scii(n,m);
	cin >> p;
	f(i,0,m,1){
		mem1 = mem2 = '8';
		char x,y;
		cin >> x >> y;
		if(x!=y){
			f(j,0,a[x].size(),1){
				mem1 = find(x,a[x][j]);
				if(mem1 != '8'){break;}
			}
			f(j,0,a[y].size(),1){
				mem2 = find(y,a[y][j]);
				if(mem2 != '8'){break;}
			}
			if(a[x].size() == 0){a[x].pb(y);}
			else{a[mem1].pb(y);}
			if(a[y].empty()){a[y].pb(x);}
			else{a[mem2].pb(x);}
		}
	}
	f(i,0,n,1){
		if(!a[p[i]].empty()){
			p[i] = a[p[i]].back();
		}
	}
	cout << p << endl;
	/*for(char i = 'a'; i<='z';i++){
		if(!a[i].empty()){
			printf("vecinos de %c\n",i);
			f(j,0,a[i].size(),1){
				printf("%c ",a[i][j]);
			}
			printf("\n");
		}
	}*/
}


#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cctype>
#include <set>
using namespace std;
#define pb push_back
#define f(i,j,k,l) for(int i = j; i < k; i+=l)
#define sci(x) scanf("%d",&x)
#define scii(x,y) scanf("%d %d",&x,&y)
#define sciii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define mp make_pair
#define INF 0x3f3f3f3f
#define EPS 1e-9
#define PI 3.1415926535897932384626
#define MOD 1000000007
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;

typedef pair<int, int> ii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<string> vs;
typedef vector <bool> vb;

typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, vi> miv;

ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
ll mcm(ll a, ll b){ return a * b / gcd(a, b); }
ll sq(ll a) { return a*a; }
ll pot(ll a, ll b){ return b ? sq(pot(a, b >> 1))*(b & 1 ? a : 1) : 1; }
ll potMod(ll a, ll e){ return e ? ((sq(potMod(a, e >> 1)) % MOD)*(e & 1 ? a : 1)) % MOD : 1; }
string p;
char a,b;
int n,m;
int k = 0;
int main(){
	scii(n,m);
	cin >> p;
	f(i,0,m,1){
		cin >> a >> b;
		if(a != b){
			f(j,0,n,1){
				if(p[j] == a){p[j] = b;}
				else if(p[j] == b){p[j] = a;}
			}
		}
	}
	cout << p << endl;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cctype>
#include <set>
using namespace std;
#define pb push_back
#define f(i,j,k,l) for(int i = j; i < k; i+=l)
#define sci(x) scanf("%d",&x)
#define scii(x,y) scanf("%d %d",&x,&y)
#define sciii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define mp make_pair
#define INF 0x3f3f3f3f
#define EPS 1e-9
#define PI 3.1415926535897932384626
#define MOD 1000000007
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;

typedef pair<int, int> ii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<string> vs;
typedef vector <bool> vb;
typedef vector <char> vc;

typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, vi> miv;

ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
ll mcm(ll a, ll b){ return a * b / gcd(a, b); }
ll sq(ll a) { return a*a; }
ll pot(ll a, ll b){ return b ? sq(pot(a, b >> 1))*(b & 1 ? a : 1) : 1; }
ll potMod(ll a, ll e){ return e ? ((sq(potMod(a, e >> 1)) % MOD)*(e & 1 ? a : 1)) % MOD : 1; }
vc a[300];
string p;
bool h[300];
char find(char dad, char vec){
	if(a[vec].size() == 0){return '8';}
	if(a[vec].back() == dad){return vec;}
	else{find(dad,a[vec].back());}
}
int main(){
	int m,n;
	ms(h,false);
	char mem1,mem2;
	scii(n,m);
	cin >> p;
	for(char i = 'a'; i<='z';i++){
		a[i].pb(i);
	}
	f(i,0,m,1){
		char x,y;
		cin >> x >> y;
		if(x!=y){
			for(char i = 'a'; i<='z'; i++){
				if(a[i].back() == x){a[i].pb(y);}
				else if(a[i].back() == y){a[i].pb(x);}
			}
		}
	}
	f(i,0,n,1){
		if(!a[p[i]].empty()){
			p[i] = a[p[i]].back();
		}
	}
	cout << p << endl;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cctype>
#include <set>
using namespace std;
#define pb push_back
#define f(i,j,k,l) for(int i = j; i < k; i+=l)
#define sci(x) scanf("%d",&x)
#define scii(x,y) scanf("%d %d",&x,&y)
#define sciii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define mp make_pair
#define INF 0x3f3f3f3f
#define EPS 1e-9
#define PI 3.1415926535897932384626
#define MOD 1000000007
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;

typedef pair<int, int> ii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<string> vs;
typedef vector <bool> vb;

typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, vi> miv;

ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
ll mcm(ll a, ll b){ return a * b / gcd(a, b); }
ll sq(ll a) { return a*a; }
ll pot(ll a, ll b){ return b ? sq(pot(a, b >> 1))*(b & 1 ? a : 1) : 1; }
ll potMod(ll a, ll e){ return e ? ((sq(potMod(a, e >> 1)) % MOD)*(e & 1 ? a : 1)) % MOD : 1; }
string p;
char a,b;
int n,m;
int main(){
	scii(n,m);
	cin >> p;
	f(i,0,m,1){
		cin >> a >> b;
		f(j,0,n,1){
			if(p[j] == a){p[j] = b;}
			else if(p[j] == b){p[j] = a;}
		}
	}
	cout << p << endl;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cctype>
#include <set>
using namespace std;
#define pb push_back
#define f(i,j,k,l) for(int i = j; i < k; i+=l)
#define sci(x) scanf("%d",&x)
#define scii(x,y) scanf("%d %d",&x,&y)
#define sciii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define mp make_pair
#define INF 0x3f3f3f3f
#define EPS 1e-9
#define PI 3.1415926535897932384626
#define MOD 1000000007
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;

typedef pair<int, int> ii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<string> vs;
typedef vector <bool> vb;
typedef vector <char> vc;

typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, vi> miv;

ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
ll mcm(ll a, ll b){ return a * b / gcd(a, b); }
ll sq(ll a) { return a*a; }
ll pot(ll a, ll b){ return b ? sq(pot(a, b >> 1))*(b & 1 ? a : 1) : 1; }
ll potMod(ll a, ll e){ return e ? ((sq(potMod(a, e >> 1)) % MOD)*(e & 1 ? a : 1)) % MOD : 1; }
vc a[300];
string p;
char find(char dad, char vec){
	if(a[vec].size() == 0){return '8';}
	if(a[vec].back() == dad){return vec;}
	else{find(dad,a[vec].back());}
}
int main(){
	int m,n;
	char mem1,mem2;
	scii(n,m);
	cin >> p;
	f(i,0,m,1){
		mem1 = mem2 = '8';
		char x,y;
		cin >> x >> y;
		if(x!=y){
			f(j,0,a[x].size(),1){
				mem1 = find(x,a[x][j]);
				if(mem1 != '8'){break;}
			}
			f(j,0,a[y].size(),1){
				mem2 = find(y,a[y][j]);
				if(mem2 != '8'){break;}
			}
			if(a[x].empty()){a[x].pb(y);}
			else{a[mem1].pb(y);}
			if(a[y].empty()){a[y].pb(x);}
			else{a[mem2].pb(x);}
		}
	}
	f(i,0,n,1){
		if(!a[p[i]].empty()){
			p[i] = a[p[i]].back();
		}
	}
	cout << p << endl;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cctype>
#include <set>
using namespace std;
#define pb push_back
#define f(i,j,k,l) for(int i = j; i < k; i+=l)
#define sci(x) scanf("%d",&x)
#define scii(x,y) scanf("%d %d",&x,&y)
#define sciii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define ms(obj, val) memset(obj, val, sizeof(obj))
#define mp make_pair
#define INF 0x3f3f3f3f
#define EPS 1e-9
#define PI 3.1415926535897932384626
#define MOD 1000000007
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;

typedef pair<int, int> ii;

typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<string> vs;
typedef vector <bool> vb;
typedef vector <char> vc;

typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, vi> miv;

ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
ll mcm(ll a, ll b){ return a * b / gcd(a, b); }
ll sq(ll a) { return a*a; }
ll pot(ll a, ll b){ return b ? sq(pot(a, b >> 1))*(b & 1 ? a : 1) : 1; }
ll potMod(ll a, ll e){ return e ? ((sq(potMod(a, e >> 1)) % MOD)*(e & 1 ? a : 1)) % MOD : 1; }
vc a[300];
string p;
bool h[300];
char find(char dad, char vec){
	if(a[vec].size() == 0){return '8';}
	if(a[vec].back() == dad){return vec;}
	else{find(dad,a[vec].back());}
}
int main(){
	int m,n;
	ms(h,false);
	char mem1,mem2;
	scii(n,m);
	cin >> p;
	for(char i = 'a'; i<='z';i++){
		a[i].pb(i);
	}
	f(i,0,m,1){
		char x,y;
		cin >> x >> y;
		if(x!=y){
			for(char i = 'a'; i<='z'; i++){
				if(a.back() == x){a.pb(y);}
				else if(a.back() == y){a.pb(x);}
			}
		}
	}
	f(i,0,n,1){
		if(!a[p[i]].empty()){
			p[i] = a[p[i]].back();
		}
	}
	cout << p << endl;
}