Problem:
Score : 1500 points

Problem Statement

Takahashi loves walking on a tree.
The tree where Takahashi walks has N vertices numbered 1 through N.
The i-th of the N-1 edges connects Vertex a_i and Vertex b_i.

Takahashi has scheduled M walks. The i-th walk is done as follows:

The walk involves two vertices u_i and v_i that are fixed beforehand.

Takahashi will walk from u_i to v_i or from v_i to u_i along the shortest path.

The happiness gained from the i-th walk is defined as follows:

The happiness gained is the number of the edges traversed during the i-th walk that satisfies one of the following conditions:

In the previous walks, the edge has never been traversed.

In the previous walks, the edge has only been traversed in the direction opposite to the direction taken in the i-th walk.

Takahashi would like to decide the direction of each walk so that the total happiness gained from the M walks is maximized.
Find the maximum total happiness that can be gained, and one specific way to choose the directions of the walks that maximizes the total happiness.

Constraints

1 ≤ N,M ≤ 2000

1 ≤ a_i , b_i ≤ N

1 ≤ u_i , v_i ≤ N

a_i \neq b_i

u_i \neq v_i

The graph given as input is a tree.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_{N-1} b_{N-1}
u_1 v_1
:
u_M v_M

Output

Print the maximum total happiness T that can be gained, and one specific way to choose the directions of the walks that maximizes the total happiness, in the following format:

T
u^'_1 v^'_1
:
u^'_M v^'_M

where (u^'_i,v^'_i) is either (u_i,v_i) or (v_i,u_i), which means that the i-th walk is from vertex u^'_i to v^'_i.

Sample Input 1

4 3
2 1
3 1
4 1
2 3
3 4
4 2

Sample Output 1

6
2 3
3 4
4 2

If we decide the directions of the walks as above, he gains the happiness of 2 in each walk, and the total happiness will be 6.

Sample Input 2

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

Sample Output 2

6
2 4
3 5
5 1

Sample Input 3

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

Sample Output 3

9
2 4
6 3
5 6
4 5

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

//minamoto
#include<bits/stdc++.h>
#define R register
#define fi first
#define se second
#define pb insert
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
inline void swap(R int &x,R int &y){R int t=x;x=y,y=t;}
typedef pair<int,int> pi;
const int N=1e6+5;
struct eg{int v,nx;}e[N<<1];int head[N],tot=1;
inline void add(R int u,R int v){e[++tot]={v,head[u]},head[u]=tot;}
int deg[N],q[N],lasx[N],lasy[N],vis[N],dep[N],fa[N],sum[N],col[N],fv[N];
set<int>pos[N];
pi p[N],rp[N];int n,m,rt,top,res;
inline int opp(R int x,R int y){return p[x].fi==y?p[x].se:p[x].fi;}
void dfs(int u){dep[u]=dep[fa[u]]+1;go(u)if(v!=fa[u])fa[v]=u,fv[u]=(i>>1),dfs(v);}
void fsd(int u){go(u)if(v!=fa[u])fsd(v),sum[u]+=sum[v];res+=min(sum[u],2);}
inline int LCA(R int u,R int v){
	while(u!=v){
		if(dep[u]<dep[v])swap(u,v);
		u=fa[u];
	}
	return u;
}
inline void clr(R int x){
	R int u=p[x].fi,v=p[x].se,lca=LCA(u,v);
	while(u!=lca)col[fv[u]]=0,u=fa[u];
	while(v!=lca)col[fv[v]]=0,v=fa[v];
}
inline void jump(R int x){
	R int u=p[x].fi,v=p[x].se,lca=LCA(u,v);
	while(u!=lca)col[fv[u]]=1,u=fa[u];
	while(v!=lca)col[fv[v]]=2,v=fa[v];
}
inline bool ck(R int x){
	R int u=p[x].fi,v=p[x].se,lca=LCA(u,v);
	while(u!=lca){
		if(col[fv[u]]==1)return 1;
		u=fa[u];
	}
	while(v!=lca){
		if(col[fv[v]]==2)return 1;
		v=fa[v];
	}
	return 0;
}
void solve(){
	int h=1,t=0,u,sz=0,now,v,x,y,lca,xx,yy,tmp;
	fp(i,1,n)if(deg[i]==1)q[++t]=i;
	top=m;
	while(h<=t&&sz<n-2){
		u=q[h++],++sz,vis[u]=1;
//		printf("Now %d %d %d\n",u,sz,pos[u].size());
		go(u)if(!vis[v]&&(--deg[now=v])==1)q[++t]=v;
		if(pos[u].empty())continue;
		if(pos[u].size()==1){
//			puts("QAQ");
			for(auto v:pos[u])if(p[v].fi!=now&&p[v].se!=now){
				++top,p[top]=pi(now,tmp=opp(v,u));
				pos[now].pb(top),lasx[top]=-v;
				pos[tmp].erase(v),pos[tmp].pb(top);
			}
			continue;
		}
		x=y=0;
		for(auto v:pos[u]){
			if(!x){x=v;continue;}
			if(!y){
				y=v,xx=opp(x,u),yy=opp(y,u);
				if(xx==yy){
//					puts("QAQ");
//					printf("%d %d %d %d\n",p[x].fi,p[x].se,p[y].fi,p[y].se);
//					printf("%d\n",(jump(x),ck(y)));
//					clr(x),clr(y);
					if(jump(x),ck(y))swap(p[y].fi,p[y].se),swap(lasx[y],lasy[y]);
					clr(x),clr(y);
					pos[xx].erase(x),pos[xx].erase(y);
//					printf("%d %d %d %d\n",p[x].fi,p[x].se,p[y].fi,p[y].se);
					continue;
				}
				++top,lasx[top]=x,lasy[top]=y;
//				printf("%d %d %d %d\n",x,y,xx,yy);
				p[top]=pi(xx,yy);
				pos[xx].erase(x),pos[yy].erase(y);
				pos[xx].pb(top),pos[yy].pb(top);
//				er(x,u,now),er(y,u,now);
//				printf("%d %d %d %d %d\n",u,np[x].fi,np[x].se,np[y].fi,np[y].se);
				continue;
			}
			x=v;
			if(p[x].fi!=now&&p[x].se!=now){
				++top,p[top]=pi(now,tmp=opp(x,u));
				pos[now].pb(top),lasx[top]=-x;
				pos[tmp].erase(x),pos[tmp].pb(top);
			}
		}
	}
//	printf("%d %d\n",h,t);
//	puts("---------------------------");
	int fl=0;
	u=q[h],v=q[t];
	for(auto x:pos[u]){
//			printf("%d %d %d %d\n",p[x].fi,p[x].se,u,v);
		if(p[x].fi==u&&p[x].se==v||p[x].fi==v&&p[x].se==u){
			if(!fl){
				if(p[x]!=pi(u,v))p[x]=pi(u,v),swap(lasx[x],lasy[x]);
				fl=1;
			}else{
				if(p[x]!=pi(v,u))p[x]=pi(v,u),swap(lasx[x],lasy[x]);
			}
	//		printf("%d %d %d %d\n",p[x].fi,p[x].se,u,v);
//			p[x]=fl?(fl=2,pi(u,v)):(fl=1,pi(v,u));
		}
	}
//	res+=fl;
	fd(i,top,1){
//		printf("%d %d %d %d %d\n",i,p[i].fi,p[i].se,lasx[i],lasy[i]);
		R int x=lasx[i],y=lasy[i];
		if(x||y){
			if(x<0||y<0){
				x=-x,y=-y;if(y)x=y;
				if(p[i].fi==p[x].se||p[i].se==p[x].fi){
					
					swap(p[x].fi,p[x].se),swap(lasx[x],lasy[x]);
					
//					printf("Change %d %d %d\n",x,p[x].fi,p[x].se);
				}
			}else{
				if(p[x].fi!=p[i].fi)swap(p[x].fi,p[x].se),swap(lasx[x],lasy[x]);
				if(p[y].se!=p[i].se)swap(p[y].fi,p[y].se),swap(lasx[y],lasy[y]);
			}
		}
	}
}
int main(){
//	freopen("testdata.in","r",stdin);
	scanf("%d%d",&n,&m);
	for(R int i=1,u,v;i<n;++i){
		scanf("%d%d",&u,&v),add(u,v),add(v,u);
		++deg[u],++deg[v];
	}
	dfs(1);
	fp(i,1,m){
		scanf("%d%d",&p[i].fi,&p[i].se);
		pos[p[i].fi].pb(i),pos[p[i].se].pb(i);
		++sum[p[i].fi],++sum[p[i].se],sum[LCA(p[i].fi,p[i].se)]-=2;
	}
	fsd(1);
	solve();
	printf("%d\n",res);
	fp(i,1,m)printf("%d %d\n",p[i].fi,p[i].se);
	return 0;
}