#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
int n,m,weight;
int no[1010];
vector <int>f[1010];
int w[1010],b[1010],pre[1010];
long long maxnw[1010],maxb[1010];
long long d[1010];
int find_ds(int a){
	return a== pre[a]?a:pre[a] = find_ds(pre[a]);
}
void combine(int a,int b){
	int fa = find_ds(a),fb = find_ds(b);
	if(fa != fb) pre[fa] = fb;
}
int main(){
	int u,v;
	scanf("%d%d%d",&n,&m,&weight);
	for(int i = 1;i <= n;i ++) {
		scanf("%d",&w[i]);
		pre[i] = i;
	}
	for(int i= 1;i <= n;i ++) scanf("%d",&b[i]);
	for(int i = 1;i <= m;i ++){
		scanf("%d%d",&u,&v);
		combine(u,v);
	}
	int maxn = 0;
	for(int i = 1;i <= n;i ++){
		int tmp = find_ds(i);
		if(!no[tmp]) no[tmp] = ++maxn;
		f[no[tmp]].push_back(i);
		maxnw[no[tmp]] += w[i];
		maxb[no[tmp]] += b[i];
	}
	for(int k = 0;k < f[1].size();k ++){
		int nowno = f[1][k];
		if(w[nowno] <= weight) d[w[nowno]] = max(d[w[nowno]],(long long)b[nowno]);
	}
	if(maxnw[1] <= weight) d[maxnw[1]] = max(d[maxnw[1]],maxb[1]);
	for(int i = 1;i < maxn;i ++){
		for(int j = weight;j >= 0;j --){
			for(int k = 0;k < f[i + 1].size();k++){
				int nowno = f[i + 1][k];
				if(w[nowno] + j <= weight) d[j + w[nowno]] = max(d[j + w[nowno]],d[j] + b[nowno]);
			}
			if(maxnw[i + 1] + j <= weight) d[j + maxnw[i + 1]] = max(d[j + maxnw[i + 1]],d[j] + maxb[i+1]);
		}
	}
	long long ans = 0;
	for(int i = 0;i <= weight;i++) ans = max(ans,d[i]);
	printf("%I64d\n",ans );
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
int n,m,weight;
int no[1010];
vector <int>f[1010];
int w[1010],b[1010],pre[1010];
long long maxnw[1010],maxb[1010];
long long d[1010][1010];
int find_ds(int a){
	return a== pre[a]?a:pre[a] = find_ds(pre[a]);
}
void combine(int a,int b){
	int fa = find_ds(a),fb = find_ds(b);
	if(fa != fb) pre[fa] = fb;
}
int main(){
	int u,v;
	scanf("%d%d%d",&n,&m,&weight);
	for(int i = 1;i <= n;i ++) {
		scanf("%d",&w[i]);
		pre[i] = i;
	}
	for(int i= 1;i <= n;i ++) scanf("%d",&b[i]);
	for(int i = 1;i <= m;i ++){
		scanf("%d%d",&u,&v);
		combine(u,v);
	}
	int maxn = 0;
	for(int i = 1;i <= n;i ++){
		int tmp = find_ds(i);
		if(!no[tmp]) no[tmp] = ++maxn;
		f[no[tmp]].push_back(i);
		maxnw[no[tmp]] += w[i];
		maxb[no[tmp]] += b[i];
	}
	for(int k = 0;k < f[1].size();k ++){
		int nowno = f[1][k];
		if(w[nowno] <= weight) d[1][nowno] = max(d[1][w[nowno]],(long long)b[nowno]);
	}
	if(maxnw[1] <= weight) d[1][maxnw[1]] = max(d[1][maxnw[1]],maxb[1]);
	for(int i = 1;i < maxn;i ++){
		for(int j = weight;j >= 0;j --){
			if(d[i][j] != 0){
				d[i + 1][j] = d[i][j];
				for(int k = 0;k < f[i + 1].size();k++){
					int nowno = f[i + 1][k];
					if(w[nowno] + j <= weight) d[i + 1][j + w[nowno]] = max(d[i + 1][j + w[nowno]],d[i][j] + b[nowno]);
				}
				if(maxnw[i + 1] + j <= weight) d[i + 1][j + maxnw[i + 1]] = max(d[i + 1][j + maxnw[i + 1]],d[i][j] + maxb[i+1]);
			}
		}
	}
	long long ans = 0;
	for(int i = 0;i <= weight;i++) ans = max(ans,d[maxn][i]);
	printf("%I64d\n",ans );
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
int n,m,weight;
int no[1010];
vector <int>f[1010];
int w[1010],b[1010],pre[1010];
long long maxnw[1010],maxb[1010];
long long d[1010];
int find_ds(int a){
	return a== pre[a]?a:pre[a] = find_ds(pre[a]);
}
void combine(int a,int b){
	int fa = find_ds(a),fb = find_ds(b);
	if(fa != fb) pre[fa] = fb;
}
int main(){
	int u,v;
	scanf("%d%d%d",&n,&m,&weight);
	for(int i = 1;i <= n;i ++) {
		scanf("%d",&w[i]);
		pre[i] = i;
	}
	for(int i= 1;i <= n;i ++) scanf("%d",&b[i]);
	for(int i = 1;i <= m;i ++){
		scanf("%d%d",&u,&v);
		combine(u,v);
	}
	int maxn = 0;
	for(int i = 1;i <= n;i ++){
		int tmp = find_ds(i);
		if(!no[tmp]) no[tmp] = ++maxn;
		f[no[tmp]].push_back(i);
		maxnw[no[tmp]] += w[i];
		maxb[no[tmp]] += b[i];
	}
	for(int k = 0;k < f[1].size();k ++){
		int nowno = f[1][k];
		if(w[nowno] <= weight) d[nowno] = max(d[w[nowno]],(long long)b[nowno]);
	}
	if(maxnw[1] <= weight) d[maxnw[1]] = max(d[maxnw[1]],maxb[1]);
	for(int i = 1;i < maxn;i ++){
		for(int j = weight;j >= 0;j --){
			for(int k = 0;k < f[i + 1].size();k++){
				int nowno = f[i + 1][k];
				if(w[nowno] + j <= weight) d[j + w[nowno]] = max(d[j + w[nowno]],d[j] + b[nowno]);
			}
			if(maxnw[i + 1] + j <= weight) d[j + maxnw[i + 1]] = max(d[j + maxnw[i + 1]],d[j] + maxb[i+1]);
		}
	}
	long long ans = 0;
	for(int i = 0;i <= weight;i++) ans = max(ans,d[i]);
	printf("%I64d\n",ans );
	return 0;
}


#include <bits/stdc++.h>
#define MAXN 1010
#define pb push_back
using namespace std;

typedef pair<int,int> pii;
int n, m, w_max, w[MAXN], b[MAXN];
int rez[MAXN], rez1[MAXN];
bool pos[MAXN];
vector<int> a[MAXN];
vector<pii> c; //weight->beauty
void dfs(int s);
int main() {
    freopen("41", "r", stdin);
    scanf("%d%d%d", &n, &m, &w_max);
    for(int i = 1; i <= n; i++) scanf("%d", &w[i]);
    for(int i = 1; i <= n; i++) scanf("%d", &b[i]);
    while(m--) {
        int x, y; scanf("%d%d", &x, &y);
        a[x].pb(y); a[y].pb(x);
    }
    for(int i = 1; i <= n; i++)
        if(!pos[i])
            dfs(i);

    int maxb = 0;
    for(int i = 0; i <= w_max; i++) maxb = max(rez[i], maxb);
    printf("%d", maxb);
    return 0;
}
stack<int> dfsq;
void dfs(int s) {
    c.clear();
    int wu = 0, bu = 0;
    dfsq.push(s);
    while(!dfsq.empty()) {
        int tmp = dfsq.top(); dfsq.pop();
        if(!pos[tmp]) {
            pos[tmp] = 1;
            wu += w[tmp]; bu += b[tmp];
            c.pb({w[tmp], b[tmp]});
            int s = a[tmp].size();
            for(int i = 0; i < s; i++) {
                int cur = a[tmp][i];
                if(!pos[cur]) dfsq.push(cur);
            }
        }
    }
    c.pb({wu, bu});
    int siz = c.size();
    for(int j = 0; j < siz; j++) {
        pii cur = c[j];
        int cur_w = cur.first, cur_b = cur.second;
        //printf("%d %d\n", cur_w, cur_b);
        for(int i = MAXN-1; i >= 0; i--) {
            if(i+cur_w < MAXN && rez[i])
                rez1[i+cur_w] = max(rez1[i+cur_w], rez[i]+cur_b);
        }
        if(cur_w < MAXN) rez1[cur_w] = max(rez1[cur_w], cur_b);
    }
    for(int i = 0; i < MAXN; i++)
        rez[i] = rez1[i];
}


#include <bits/stdc++.h>
#define MAXN 1010
#define pb push_back
using namespace std;
typedef pair<int,int> pii;
int n, m, w_max, w[MAXN], b[MAXN], rez[MAXN], rez1[MAXN];
bool pos[MAXN];
vector<int> a[MAXN];
vector<pii> c; //weight->beauty
void dfs(int s);
int main() {
    //freopen("21.txt", "r", stdin);
    scanf("%d%d%d", &n, &m, &w_max);
    for(int i = 1; i <= n; i++) scanf("%d", &w[i]);
    for(int i = 1; i <= n; i++) scanf("%d", &b[i]);
    while(m--) {
        int x, y; scanf("%d%d", &x, &y);
        a[x].pb(y); a[y].pb(x);
    }
    for(int i = 1; i <= n; i++)
        if(!pos[i])
            dfs(i);
    int maxb = 0;
    for(int i = 0; i <= w_max; i++) maxb = max(rez[i], maxb);
    printf("%d", maxb);
    return 0;
}
stack<int> dfsq;
void dfs(int s) {
    c.clear();
    int wu = 0, bu = 0;
    dfsq.push(s);
    while(!dfsq.empty()) {
        int tmp = dfsq.top(); dfsq.pop();
        pos[tmp] = 1;
        wu += w[tmp]; bu += b[tmp];
        c.pb({w[tmp], b[tmp]});
        int siz = a[tmp].size();
        for(int i = 0; i < siz; i++) {
            int cur = a[tmp][i];
            if(!pos[cur]) dfsq.push(cur);
        }
    }
    c.pb({wu, bu});
    int siz = c.size();
    for(int j = 0; j < siz; j++) {
        pii cur = c[j];
        int cur_w = cur.first, cur_b = cur.second;
        //printf("%d %d\n", cur_w, cur_b);
        for(int i = MAXN-1; i >= 0; i--) {
            if(i+cur_w < MAXN && rez[i])
                rez1[i+cur_w] = max(rez1[i+cur_w], rez[i]+cur_b);
            }
        if(cur_w < MAXN) rez1[cur_w] = max(rez1[cur_w], cur_b);
        for(int i = 0; i < MAXN; i++)
            rez[i] = rez1[i];
    }
}


#include <bits/stdc++.h>
#define MAXN 1010
#define pb push_back
using namespace std;
typedef pair<int,int> pii;
int n, m, w_max, w[MAXN], b[MAXN], cntc;
int rez[MAXN], rez1[MAXN];
bool pos[MAXN];
vector<int> a[MAXN];
vector<pii> c[MAXN]; //weight->beauty
void dfs(int s);
int main() {
    scanf("%d%d%d", &n, &m, &w_max);
    for(int i = 1; i <= n; i++) scanf("%d", &w[i]);
    for(int i = 1; i <= n; i++) scanf("%d", &b[i]);
    while(m--) {
        int x, y; scanf("%d%d", &x, &y);
        a[x].pb(y); a[y].pb(x);
    }
    for(int i = 1; i <= n; i++) {
        if(!pos[i]) {
            dfs(i);
        }
    }
    for(int cc = 0; cc < cntc; cc++) {
        int s = c[cc].size();
        for(int j = 0; j < s; j++) {
            pii cur = c[cc][j];
            int cur_w = cur.first, cur_b = cur.second;
            for(int i = MAXN-1; i >= 0; i--) {
                if(i+cur_w < MAXN && rez[i])
                    rez1[i+cur_w] = max(rez1[i+cur_w], rez[i]+cur_b);
            }
            rez1[cur_w] = max(rez1[cur_w], cur_b);
        }
        for(int i = 0; i < MAXN; i++)
            rez[i] = rez1[i];
    }
    int maxb = 0;
    for(int i = 0; i <= w_max; i++) maxb = max(rez[i], maxb);
    printf("%d", maxb);
    return 0;
}

void dfs(int s) {
    int wu = 0, bu = 0;
    stack<int> dfsq;
    dfsq.push(s);
    while(!dfsq.empty()) {
        int tmp = dfsq.top(); dfsq.pop();
        pos[tmp] = 1;
        wu += w[tmp]; bu += b[tmp];
        c[cntc].pb({w[tmp], b[tmp]});
        int s = a[tmp].size();
        for(int i = 0; i < s; i++) {
            int cur = a[tmp][i];
            if(!pos[cur]) dfsq.push(cur);
        }
    }
    c[cntc++].pb({wu, bu});
}


#include <bits/stdc++.h>
#define MAXN 1010
#define pb push_back
using namespace std;

typedef pair<int,int> pii;
int n, m, w_max, w[MAXN], b[MAXN];
int rez[MAXN], rez1[MAXN];
bool pos[MAXN];
vector<int> a[MAXN];
vector<pii> c; //weight->beauty
void dfs(int s);
int main() {
    //freopen("41", "r", stdin);
    scanf("%d%d%d", &n, &m, &w_max);
    for(int i = 1; i <= n; i++) scanf("%d", &w[i]);
    for(int i = 1; i <= n; i++) scanf("%d", &b[i]);
    while(m--) {
        int x, y; scanf("%d%d", &x, &y);
        a[x].pb(y); a[y].pb(x);
    }
    for(int i = 1; i <= n; i++)
        if(!pos[i])
            dfs(i);

    int maxb = 0;
    for(int i = 0; i <= w_max; i++) maxb = max(rez[i], maxb);
    printf("%d", maxb);
    return 0;
}
stack<int> dfsq;
void dfs(int s) {
    c.clear();
    int wu = 0, bu = 0;
    dfsq.push(s);
    while(!dfsq.empty()) {
        int tmp = dfsq.top(); dfsq.pop();
        if(!pos[tmp]) {
            pos[tmp] = 1;
            wu += w[tmp]; bu += b[tmp];
            c.pb({w[tmp], b[tmp]});
            int s = a[tmp].size();
            for(int i = 0; i < s; i++) {
                int cur = a[tmp][i];
                if(!pos[cur]) dfsq.push(cur);
            }
        }
    }
    c.pb({wu, bu});
    int siz = c.size();
    for(int j = 0; j < siz; j++) {
        pii cur = c[j];
        int cur_w = cur.first, cur_b = cur.second;
        //printf("%d %d\n", cur_w, cur_b);
        for(int i = MAXN-1; i >= 0; i--) {
            if(i+cur_w < MAXN && rez[i])
                rez1[i+cur_w] = max(rez1[i+cur_w], rez[i]+cur_b);
        }
        if(cur_w < MAXN) rez1[cur_w] = max(rez1[cur_w], cur_b);
    }
    for(int i = 0; i < MAXN; i++)
        rez[i] = rez1[i];
}


#include <bits/stdc++.h>
#define MAXN 1010
#define pb push_back
using namespace std;
typedef pair<int,int> pii;
int n, m, w_max, w[MAXN], b[MAXN], cntc;
int rez[MAXN], rez1[MAXN];
bool pos[MAXN];
vector<int> a[MAXN];
vector<pii> c[MAXN]; //weight->beauty
void dfs(int s);
int main() {
    //freopen("21.txt", "r", stdin);
    scanf("%d%d%d", &n, &m, &w_max);
    for(int i = 1; i <= n; i++) scanf("%d", &w[i]);
    for(int i = 1; i <= n; i++) scanf("%d", &b[i]);
    while(m--) {
        int x, y; scanf("%d%d", &x, &y);
        a[x].pb(y); a[y].pb(x);
    }
    for(int i = 1; i <= n; i++)
        if(!pos[i])
            dfs(i);

    //printf("STIZE");
    for(int cc = 0; cc < cntc; cc++) {
        int s = c[cc].size();
        for(int j = 0; j < s; j++) {
            pii cur = c[cc][j];
            int cur_w = cur.first, cur_b = cur.second;
            //printf("%d %d\n", cur_w, cur_b);
            for(int i = MAXN-1; i >= 0; i--) {
                if(i+cur_w < MAXN && rez[i])
                    rez1[i+cur_w] = max(rez1[i+cur_w], rez[i]+cur_b);
            }
            if(cur_w < MAXN) rez1[cur_w] = max(rez1[cur_w], cur_b);
        }
        for(int i = 0; i < MAXN; i++)
            rez[i] = rez1[i];
    }
    //printf("STIZE");
    int maxb = 0;
    for(int i = 0; i <= w_max; i++) maxb = max(rez[i], maxb);
    printf("%d", maxb);
    return 0;
}
stack<int> dfsq;
void dfs(int s) {
    int wu = 0, bu = 0;
    dfsq.push(s);
    while(!dfsq.empty()) {
        int tmp = dfsq.top(); dfsq.pop();
        pos[tmp] = 1;
        wu += w[tmp]; bu += b[tmp];
        c[cntc].pb({w[tmp], b[tmp]});
        int s = a[tmp].size();
        for(int i = 0; i < s; i++) {
            int cur = a[tmp][i];
            if(!pos[cur]) dfsq.push(cur);
        }
    }
    c[cntc++].pb({wu, bu});
}


// adt@game ::>> Aditya Raj
#include<bits/stdc++.h>

using namespace std;

typedef pair<int,int> pr;
typedef vector< pr > vec2;
typedef vector<int> vec;
typedef vector< vec > vvec;
typedef long long int lint;

#define PB push_back
#define MP make_pair
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end

#define SET(a,b) memset(a,b,sizeof(a))
#define scan(n) scanf("%d",&n)
#define print(n) printf("%d\n",n)
#define scanl(n) scanf("%lld",&n)
#define scans(s) cin>>s
#define printl(n) printf("%lld\n",n)
#define fr(i,s,n) for(int i=s;i<n;i++)

//pow
/*int fun(int a,int b,int m)
{
        if(b==0) return 1;
        int d,x;
        d=fun(a,b/2,m);
        x=(d*d)%m;
        if(b%2==0) return x;
        else return (x*a%m)%m;
}
*/

//dfs + graph
/*
vec ar[100001];
void insert(int a,int b)
{
       ar[a].PB(b);
}

//dfs
int state[100001];
void dfs(int a)
{
        state[a]=1;
        int x=SZ(ar[a]);
        fr(i,0,x) if(state[ar[a][i]]!=1) dfs(ar[a][i],t);
        return;
}
*/

//-------------------------------------------------------------------------------------------------------------------------------------

lint ar[1005];
lint find(lint a)
{
       if(ar[a]==a) return a;
       return ar[a]=find(ar[a]);
}
void un(lint a,lint b)
{
       if(a==b) return;
       ar[a]=b;
}
lint n,m,w;
vec v[1005];
map<pair<lint,pair<lint,lint>>,lint> dp;
lint we[1005];
lint b[1005];

lint fun(lint i,lint w,lint bt)
{
       if(w<0) return -1000;
       if(i>n-1) return bt;
       if(dp[MP(i,MP(w,bt))]) return dp[MP(i,MP(w,bt))];
       lint ret=fun(i+1,w,bt);
       lint s1=0,s2=0;
       if(SZ(v[i])==0) return dp[MP(i,MP(w,bt))]=ret;
       fr(x,0,SZ(v[i]))
       {
              if(w-we[v[i][x]]>=0) ret=max(ret,fun(i+1,w-we[v[i][x]],bt+b[v[i][x]]));
              s1+=we[v[i][x]];
              s2+=b[v[i][x]];
       }
       if(w-s1>=0) ret=max(ret,fun(i+1,w-s1,bt+s2));
       return dp[MP(i,MP(w,bt))]=ret;
}

int main()
{
       lint aa,bb;
       scanl(n);
       scanl(m);
       scanl(w);
       fr(i,0,n) ar[i]=i;
       fr(i,0,n) scanl(we[i]);
       fr(i,0,n) scanl(b[i]);
       fr(i,0,m)
       {
              scanl(aa);
              scanl(bb);
              un(find(aa-1),find(bb-1));
       }
       fr(a,0,n)
       {
              ar[a]=find(a);
              v[ar[a]].PB(a);
       }
       lint ans=fun(0,w,0);
       printl(ans);
       return 0;
}


// adt@game ::>> Aditya Raj
#include<bits/stdc++.h>

using namespace std;

typedef pair<int,int> pr;
typedef vector< pr > vec2;
typedef vector<int> vec;
typedef vector< vec > vvec;
typedef long long int lint;

#define PB push_back
#define MP make_pair
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end

#define SET(a,b) memset(a,b,sizeof(a))
#define scan(n) scanf("%d",&n)
#define print(n) printf("%d\n",n)
#define scanl(n) scanf("%lld",&n)
#define scans(s) cin>>s
#define printl(n) printf("%lld\n",n)
#define fr(i,s,n) for(int i=s;i<n;i++)

//pow
/*int fun(int a,int b,int m)
{
        if(b==0) return 1;
        int d,x;
        d=fun(a,b/2,m);
        x=(d*d)%m;
        if(b%2==0) return x;
        else return (x*a%m)%m;
}
*/

//dfs + graph
/*
vec ar[100001];
void insert(int a,int b)
{
       ar[a].PB(b);
}

//dfs
int state[100001];
void dfs(int a)
{
        state[a]=1;
        int x=SZ(ar[a]);
        fr(i,0,x) if(state[ar[a][i]]!=1) dfs(ar[a][i],t);
        return;
}
*/

//-------------------------------------------------------------------------------------------------------------------------------------

lint ar[1005];
lint find(lint a)
{
       if(ar[a]==a) return a;
       return ar[a]=find(ar[a]);
}
void un(lint a,lint b)
{
       if(a==b) return;
       ar[a]=b;
}
lint n,m,w;
vec v[1005];
lint dp[1005][1005];
lint we[1005];
lint b[1005];

lint fun(lint i,lint w,lint bt)
{
       if(w<0) return 0;
       if(i>n-1) return bt;
       if(dp[i][w]) return dp[i][w];
       lint ret=fun(i+1,w,bt);
       lint s1=0,s2=0;
       if(SZ(v[i])==0) return dp[i][w]=ret;
       fr(x,0,SZ(v[i]))
       {
              ret=max(ret,fun(i+1,w-we[v[i][x]],bt+b[v[i][x]]));
              s1+=we[v[i][x]];
              s2+=b[v[i][x]];
       }
       ret=max(ret,fun(i+1,w-s1,bt+s2));
       return dp[i][w]=ret;
}

int main()
{
       lint aa,bb;
       scanl(n);
       scanl(m);
       scanl(w);
       fr(i,0,n) ar[i]=i;
       fr(i,0,n) scanl(we[i]);
       fr(i,0,n) scanl(b[i]);
       fr(i,0,m)
       {
              scanl(aa);
              scanl(bb);
              un(find(aa-1),find(bb-1));
       }
       fr(a,0,n)
       {
              ar[a]=find(a);
              v[ar[a]].PB(a);
       }
       int ans=fun(0,w,0);
       printl(ans);
       return 0;
}


// adt@game ::>> Aditya Raj
#include<bits/stdc++.h>

using namespace std;

typedef pair<int,int> pr;
typedef vector< pr > vec2;
typedef vector<int> vec;
typedef vector< vec > vvec;
typedef long long int lint;

#define PB push_back
#define MP make_pair
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end

#define SET(a,b) memset(a,b,sizeof(a))
#define scan(n) scanf("%d",&n)
#define print(n) printf("%d\n",n)
#define scanl(n) scanf("%lld",&n)
#define scans(s) cin>>s
#define printl(n) printf("%lld\n",n)
#define fr(i,s,n) for(int i=s;i<n;i++)

//pow
/*int fun(int a,int b,int m)
{
        if(b==0) return 1;
        int d,x;
        d=fun(a,b/2,m);
        x=(d*d)%m;
        if(b%2==0) return x;
        else return (x*a%m)%m;
}
*/

//dfs + graph
/*
vec ar[100001];
void insert(int a,int b)
{
       ar[a].PB(b);
}

//dfs
int state[100001];
void dfs(int a)
{
        state[a]=1;
        int x=SZ(ar[a]);
        fr(i,0,x) if(state[ar[a][i]]!=1) dfs(ar[a][i],t);
        return;
}
*/

//-------------------------------------------------------------------------------------------------------------------------------------

lint ar[1005];
lint find(lint a)
{
       if(ar[a]==a) return a;
       return ar[a]=find(ar[a]);
}
void un(lint a,lint b)
{
       if(a==b) return;
       ar[a]=b;
}
lint n,m,w;
vec v[1005];
lint dp[1005][1005];
lint we[1005];
lint b[1005];

lint fun(lint i,lint w,lint bt)
{
       if(w<0) return -1000;
       if(i>n-1) return bt;
       lint ret=fun(i+1,w,bt);
       lint s1=0,s2=0;
       if(SZ(v[i])==0) return ret;
       fr(x,0,SZ(v[i]))
       {
              if(w-we[v[i][x]]>=0) ret=max(ret,fun(i+1,w-we[v[i][x]],bt+b[v[i][x]]));
              s1+=we[v[i][x]];
              s2+=b[v[i][x]];
       }
       if(w-s1>=0) ret=max(ret,fun(i+1,w-s1,bt+s2));
       return ret;
}

int main()
{
       lint aa,bb;
       scanl(n);
       scanl(m);
       scanl(w);
       fr(i,0,n) ar[i]=i;
       fr(i,0,n) scanl(we[i]);
       fr(i,0,n) scanl(b[i]);
       fr(i,0,m)
       {
              scanl(aa);
              scanl(bb);
              un(find(aa-1),find(bb-1));
       }
       fr(a,0,n)
       {
              ar[a]=find(a);
              v[ar[a]].PB(a);
       }
       lint ans=fun(0,w,0);
       printl(ans);
       return 0;
}


// adt@game ::>> Aditya Raj
#include<bits/stdc++.h>

using namespace std;

typedef pair<int,int> pr;
typedef vector< pr > vec2;
typedef vector<int> vec;
typedef vector< vec > vvec;
typedef long long int lint;

#define PB push_back
#define MP make_pair
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end

#define SET(a,b) memset(a,b,sizeof(a))
#define scan(n) scanf("%d",&n)
#define print(n) printf("%d\n",n)
#define scanl(n) scanf("%lld",&n)
#define scans(s) cin>>s
#define printl(n) printf("%lld\n",n)
#define fr(i,s,n) for(int i=s;i<n;i++)

//pow
/*int fun(int a,int b,int m)
{
        if(b==0) return 1;
        int d,x;
        d=fun(a,b/2,m);
        x=(d*d)%m;
        if(b%2==0) return x;
        else return (x*a%m)%m;
}
*/

//dfs + graph
/*
vec ar[100001];
void insert(int a,int b)
{
       ar[a].PB(b);
}

//dfs
int state[100001];
void dfs(int a)
{
        state[a]=1;
        int x=SZ(ar[a]);
        fr(i,0,x) if(state[ar[a][i]]!=1) dfs(ar[a][i],t);
        return;
}
*/

//-------------------------------------------------------------------------------------------------------------------------------------

lint ar[1005];
lint find(lint a)
{
       if(ar[a]==a) return a;
       return ar[a]=find(ar[a]);
}
void un(lint a,lint b)
{
       if(a==b) return;
       ar[a]=b;
}
lint n,m,w;
vec v[1005];
lint dp[1005][1005];
lint we[1005];
lint b[1005];

lint fun(lint i,lint w,lint bt)
{
       if(w<0) return 0;
       if(i>n-1) return bt;
       if(dp[i][w]!=-1) return dp[i][w];
       lint ret=fun(i+1,w,bt);
       lint s1=0,s2=0;
       if(SZ(v[i])==0) return dp[i][w]=ret;
       fr(x,0,SZ(v[i]))
       {
              ret=max(ret,fun(i+1,w-we[v[i][x]],bt+b[v[i][x]]));
              s1+=we[v[i][x]];
              s2+=b[v[i][x]];
       }
       ret=max(ret,fun(i+1,w-s1,bt+s2));
       return dp[i][w]=ret;
}

int main()
{
       lint aa,bb;
       scanl(n);
       scanl(m);
       scanl(w);
       fr(i,0,n) fr(j,0,1005) dp[i][j]=-1;
       fr(i,0,n) ar[i]=i;
       fr(i,0,n) scanl(we[i]);
       fr(i,0,n) scanl(b[i]);
       fr(i,0,m)
       {
              scanl(aa);
              scanl(bb);
              un(find(aa-1),find(bb-1));
       }
       fr(a,0,n)
       {
              ar[a]=find(a);
              v[ar[a]].PB(a);
       }
       lint ans=fun(0,w,0);
       printl(ans);
       return 0;
}


// adt@game ::>> Aditya Raj
#include<bits/stdc++.h>

using namespace std;

typedef pair<int,int> pr;
typedef vector< pr > vec2;
typedef vector<int> vec;
typedef vector< vec > vvec;
typedef long long int lint;

#define PB push_back
#define MP make_pair
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end

#define SET(a,b) memset(a,b,sizeof(a))
#define scan(n) scanf("%d",&n)
#define print(n) printf("%d\n",n)
#define scanl(n) scanf("%lld",&n)
#define scans(s) cin>>s
#define printl(n) printf("%lld\n",n)
#define fr(i,s,n) for(int i=s;i<n;i++)

//pow
/*int fun(int a,int b,int m)
{
        if(b==0) return 1;
        int d,x;
        d=fun(a,b/2,m);
        x=(d*d)%m;
        if(b%2==0) return x;
        else return (x*a%m)%m;
}
*/

//dfs + graph
/*
vec ar[100001];
void insert(int a,int b)
{
       ar[a].PB(b);
}

//dfs
int state[100001];
void dfs(int a)
{
        state[a]=1;
        int x=SZ(ar[a]);
        fr(i,0,x) if(state[ar[a][i]]!=1) dfs(ar[a][i],t);
        return;
}
*/

//-------------------------------------------------------------------------------------------------------------------------------------

lint ar[1005];
lint find(lint a)
{
       if(ar[a]==a) return a;
       return ar[a]=find(ar[a]);
}
void un(lint a,lint b)
{
       if(a==b) return;
       ar[a]=b;
}
lint n,m,w;
vec v[1005];
lint dp[1005][1005];
lint we[1005];
lint b[1005];

lint fun(lint i,lint w,lint bt)
{
       if(w<0) return 0;
       if(i>n-1) return bt;
       if(dp[i][w]) return dp[i][w];
       lint ret=fun(i+1,w,bt);
       lint s1=0,s2=0;
       if(SZ(v[i])==0) return dp[i][w]=ret;
       fr(x,0,SZ(v[i]))
       {
              ret=max(ret,fun(i+1,w-we[v[i][x]],bt+b[v[i][x]]));
              s1+=we[v[i][x]];
              s2+=b[v[i][x]];
       }
       ret=max(ret,fun(i+1,w-s1,bt+s2));
       return dp[i][w]=ret;
}

int main()
{
       lint aa,bb;
       scanl(n);
       scanl(m);
       scanl(w);
       fr(i,0,n) ar[i]=i;
       fr(i,0,n) scanl(we[i]);
       fr(i,0,n) scanl(b[i]);
       fr(i,0,m)
       {
              scanl(aa);
              scanl(bb);
              un(find(aa-1),find(bb-1));
       }
       fr(a,0,n)
       {
              ar[a]=find(a);
              v[ar[a]].PB(a);
       }
       lint ans=fun(0,w,0);
       printl(ans);
       return 0;
}


#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <complex>
#include <numeric>
#include <valarray>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

#define pi 3.14159265358979323846264338327950
const int dr[]={0,-1,0,1,-1,1, 1,-1};
const int dc[]={1,0,-1,0, 1,1,-1,-1};
const double eps=1e-9;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef pair<double, double> pdd;
typedef vector<pdd> vpdd;
typedef complex<double> pnt;
typedef vector<pnt> vpnt;
typedef vector<vector<pair<int,int> > > vvpii;
typedef pair<string, int> psi;
typedef vector<psi> vpsi;
typedef map<int,int> mii;
typedef map<string,int> msi;
typedef map<int,string> mis;

#define inf (1<<29)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define mem(x,a) memset(x,a,sizeof(x))
#define reps(i,a) repi(i,0,len(a))
#define rep(i,n) for(int i(0),_n(n);i<_n;++i)
#define repi(i,a,b) for(int i(a),_b(b);i<_b;++i)
#define repd(i,a,b) for(int i(a),_b(b);i>=_b;--i)
#define uniq(c) (c).resize(unique(c.begin(),c.end())-(c).begin());
#define repe(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define x first
#define y second
#define chk(a,k) ((bool)(a&(1<<(k))))
#define off(a,k) (a&(~(1<<(k))))
#define on(a,k) (a|(1<<(k)))
#define cbit(a) __builtin_popcount(a)
#define tbit(a) __builtin_ctz(a)
#define lbit(a) __builtin_clz(a)
#define sqr(a) ((a)*(a))
#define mod 1000000007

template<class T> int len(const T&c){return (int)c.size();}
template<class T> void cmin(T &a,T b){if(b<a) a=b;}
template<class T> void cmax(T &a,T b){if(b>a) a=b;}
template<class T, class U> T cast (U x) { T y; ostringstream a; a<<x; istringstream b(a.str()); b>>y; return y; }
vs split(string s, string x=" ") {vs res; rep(i,len(s)){string a; while(i<(int)s.size()&&x.find(s[i])==string::npos)a+=s[i++]; if(!a.empty())res.push_back((a));} return res;}
template<class T> inline T gcd(T a,T b) {if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
template<class T> inline T Pow(T b,T p){T ret=1;rep(i,p){ret*=b;}return ret;}

#define MAX 1000000
#define is_it(n) (flag[n>>6]&(1<<((n>>1)&31)))
#define set_it(n) (flag[n>>6]|=(1<<((n>>1)&31)))
int flag[MAX/64];
vector<int> sieve(){vi ret;unsigned i,j,k;flag[0]|=0;for(i=3;i<sqrt(MAX)+1;i+=2){if(!is_it(i)){for(j=i*i,k=i<<1;j<MAX;j+=k)set_it(j);}}if(MAX>1)ret.pb(2);for(i=3;i<MAX;i+=2){if(!is_it(i)){ret.pb(i);}}return ret;}

#define DEBUG 1
#if DEBUG && !ONLINE_JUDGE
	#define debug(args...) (Debugger()) , args
	class Debugger { public: Debugger(const std::string& _separator = ", ") : first(true), separator(_separator){} template<typename ObjectType> Debugger& operator , (const ObjectType& v) { if(!first) std::cerr << separator; std::cerr << v; first = false; return *this; } ~Debugger() { std::cerr << endl;} private: bool first; std::string separator; }; template <typename T1, typename T2> inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v) { bool first = true; os << "["; for(unsigned int i = 0; i < v.size(); i++) { if(!first) os << ", "; os << v[i]; first = false; } return os << "]"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::set<T>& v) { bool first = true; os << "["; for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii; first = false; } return os << "]"; } template<typename T1, typename T2> inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v) { bool first = true; os << "["; for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii ; first = false; } return os << "]"; }
#else
		#define debug(args...)
#endif

int n,m,t1,t2;
vi v1,v2;

int prcnt1[10000];
int prcnt2[10000];
mii mi1,mi2;

int main(){
	ios_base::sync_with_stdio(0);

	vi v=sieve();

	//~ mem(prcnt1,0);
	//~ mem(prcnt2,0);

	vi pr;

	rep(i,len(v)){
		if(sqr(v[i])>10000000)break;
		pr.pb(v[i]);
	}

	cin>>n>>m;

	rep(j,n){
		cin>>t1;
		v1.pb(t1);
		rep(i,len(pr)){
			while(t1%pr[i]==0){
				t1/=pr[i];
				prcnt1[pr[i]]++;
			}
		}
		if(t1>1)mi1[t1]++;
	}

	rep(j,m){
		cin>>t1;
		v2.pb(t1);
		rep(i,len(pr)){
			while(t1%pr[i]==0){
				t1/=pr[i];
				prcnt2[pr[i]]++;

			}
		}
		if(t1>1)mi2[t1]++;
	}

	rep(i,len(pr))prcnt1[pr[i]]=prcnt2[pr[i]]=min(prcnt1[pr[i]],prcnt2[pr[i]]);

	//~ cout<<prcnt1[2]<<"# "<<prcnt1[5]<<endl;
	//~ cout<<prcnt2[2]<<"# "<<prcnt2[5]<<endl;

	repe(i,mi1){
		mi2[i->x]=mi1[i->x]=min(mi1[i->x],mi2[i->x]);
	}
	repe(i,mi2){
		mi2[i->x]=mi1[i->x]=min(mi1[i->x],mi2[i->x]);
	}

	//debug(mi2);

	rep(j,n){

		rep(i,len(pr)){
			while(v1[j]%pr[i]==0&&prcnt1[pr[i]]){
				v1[j]/=pr[i];
				prcnt1[pr[i]]--;
			}
		}
		if(mi1[v1[j]]){
			mi1[v1[j]]--;
			v1[j]=1;
		}

	}

	rep(i,10000)assert(prcnt1[i]==0);

	rep(j,m){

		rep(i,len(pr)){

			while(v2[j]%pr[i]==0&&prcnt2[pr[i]]){
				v2[j]/=pr[i];
				prcnt2[pr[i]]--;
			}
		}

		if(mi2[v2[j]]){
			mi2[v2[j]]--;
			v2[j]=1;
		}
	}

	rep(i,10000)assert(prcnt2[i]==0);

	cout<<n<<" "<<m<<endl;

	rep(i,n)cout<<v1[i]<<" ";
	cout<<endl;
	rep(i,m)cout<<v2[i]<<" ";

}


#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <complex>
#include <numeric>
#include <valarray>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

#define pi 3.14159265358979323846264338327950
const int dr[]={0,-1,0,1,-1,1, 1,-1};
const int dc[]={1,0,-1,0, 1,1,-1,-1};
const double eps=1e-9;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef pair<double, double> pdd;
typedef vector<pdd> vpdd;
typedef complex<double> pnt;
typedef vector<pnt> vpnt;
typedef vector<vector<pair<int,int> > > vvpii;
typedef pair<string, int> psi;
typedef vector<psi> vpsi;
typedef map<int,int> mii;
typedef map<string,int> msi;
typedef map<int,string> mis;

#define inf (1<<29)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define mem(x,a) memset(x,a,sizeof(x))
#define reps(i,a) repi(i,0,len(a))
#define rep(i,n) for(int i(0),_n(n);i<_n;++i)
#define repi(i,a,b) for(int i(a),_b(b);i<_b;++i)
#define repd(i,a,b) for(int i(a),_b(b);i>=_b;--i)
#define uniq(c) (c).resize(unique(c.begin(),c.end())-(c).begin());
#define repe(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define x first
#define y second
#define chk(a,k) ((bool)(a&(1<<(k))))
#define off(a,k) (a&(~(1<<(k))))
#define on(a,k) (a|(1<<(k)))
#define cbit(a) __builtin_popcount(a)
#define tbit(a) __builtin_ctz(a)
#define lbit(a) __builtin_clz(a)
#define sqr(a) ((a)*(a))
#define mod 1000000007

template<class T> int len(const T&c){return (int)c.size();}
template<class T> void cmin(T &a,T b){if(b<a) a=b;}
template<class T> void cmax(T &a,T b){if(b>a) a=b;}
template<class T, class U> T cast (U x) { T y; ostringstream a; a<<x; istringstream b(a.str()); b>>y; return y; }
vs split(string s, string x=" ") {vs res; rep(i,len(s)){string a; while(i<(int)s.size()&&x.find(s[i])==string::npos)a+=s[i++]; if(!a.empty())res.push_back((a));} return res;}
template<class T> inline T gcd(T a,T b) {if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
template<class T> inline T Pow(T b,T p){T ret=1;rep(i,p){ret*=b;}return ret;}

#define MAX 10000000
#define is_it(n) (flag[n>>6]&(1<<((n>>1)&31)))
#define set_it(n) (flag[n>>6]|=(1<<((n>>1)&31)))
int flag[MAX/64];
vector<int> sieve(){vi ret;unsigned i,j,k;flag[0]|=0;for(i=3;i<sqrt(MAX)+1;i+=2){if(!is_it(i)){for(j=i*i,k=i<<1;j<MAX;j+=k)set_it(j);}}if(MAX>1)ret.pb(2);for(i=3;i<MAX;i+=2){if(!is_it(i)){ret.pb(i);}}return ret;}

#define DEBUG 1
#if DEBUG && !ONLINE_JUDGE
	#define debug(args...) (Debugger()) , args
	class Debugger { public: Debugger(const std::string& _separator = ", ") : first(true), separator(_separator){} template<typename ObjectType> Debugger& operator , (const ObjectType& v) { if(!first) std::cerr << separator; std::cerr << v; first = false; return *this; } ~Debugger() { std::cerr << endl;} private: bool first; std::string separator; }; template <typename T1, typename T2> inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v) { bool first = true; os << "["; for(unsigned int i = 0; i < v.size(); i++) { if(!first) os << ", "; os << v[i]; first = false; } return os << "]"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::set<T>& v) { bool first = true; os << "["; for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii; first = false; } return os << "]"; } template<typename T1, typename T2> inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v) { bool first = true; os << "["; for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii ; first = false; } return os << "]"; }
#else
		#define debug(args...)
#endif

int n,m,t1,t2;
vi v1,v2;

int prcnt1[10000];
int prcnt2[10000];
mii mi1,mi2;

int main(){
	ios_base::sync_with_stdio(0);

	vi v=sieve();

	vi pr;

	rep(i,len(v)){
		if(sqr(v[i])>10000000)break;
		pr.pb(v[i]);
	}

	cin>>n>>m;

	rep(i,n){
		cin>>t1;
		v1.pb(t1);
		rep(i,len(pr)){
			while(t1%pr[i]==0){
				t1/=pr[i];
				prcnt1[pr[i]]++;
			}
		}
		if(t1>1)mi1[t1]++;
	}

	rep(i,m){
		cin>>t1;
		v2.pb(t1);
		rep(i,len(pr)){
			while(t1%pr[i]==0){
				t1/=pr[i];
				prcnt2[pr[i]]++;

			}
		}
		if(t1>1)mi2[t1]++;
	}

	rep(i,10000)prcnt2[i]=prcnt1[i]=min(prcnt1[i],prcnt2[i]);

	//~ cout<<prcnt1[2]<<"# "<<prcnt1[5]<<endl;
	//~ cout<<prcnt2[2]<<"# "<<prcnt2[5]<<endl;

	repe(i,mi1){
		mi2[i->x]=mi1[i->x]=min(mi1[i->x],mi2[i->x]);
	}
	repe(i,mi2){
		mi2[i->x]=mi1[i->x]=min(mi1[i->x],mi2[i->x]);
	}

	//debug(mi2);

	rep(j,n){

		rep(i,len(pr)){
			while(v1[j]%pr[i]==0&&prcnt1[pr[i]]){
				v1[j]/=pr[i];
				prcnt1[pr[i]]--;
			}
		}
		if(mi1[v1[j]]){
			mi1[v1[j]]--;
			v1[j]=1;
		}

	}

	rep(j,m){

		rep(i,len(pr)){

			while(v2[j]%pr[i]==0&&prcnt2[pr[i]]){
				v2[j]/=pr[i];
				prcnt2[pr[i]]--;
			}
		}

		if(mi2[v2[j]]){
			mi2[v2[j]]--;
			v2[j]=1;
		}
	}

	cout<<n<<" "<<m<<endl;

	rep(i,n)cout<<v1[i]<<" ";
	cout<<endl;
	rep(i,m)cout<<v2[i]<<" ";
	cout<<endl;

}


#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <complex>
#include <numeric>
#include <valarray>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

#define pi 3.14159265358979323846264338327950
const int dr[]={0,-1,0,1,-1,1, 1,-1};
const int dc[]={1,0,-1,0, 1,1,-1,-1};
const double eps=1e-9;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef pair<double, double> pdd;
typedef vector<pdd> vpdd;
typedef complex<double> pnt;
typedef vector<pnt> vpnt;
typedef vector<vector<pair<int,int> > > vvpii;
typedef pair<string, int> psi;
typedef vector<psi> vpsi;
typedef map<int,int> mii;
typedef map<string,int> msi;
typedef map<int,string> mis;

#define inf (1<<29)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define mem(x,a) memset(x,a,sizeof(x))
#define reps(i,a) repi(i,0,len(a))
#define rep(i,n) for(int i(0),_n(n);i<_n;++i)
#define repi(i,a,b) for(int i(a),_b(b);i<_b;++i)
#define repd(i,a,b) for(int i(a),_b(b);i>=_b;--i)
#define uniq(c) (c).resize(unique(c.begin(),c.end())-(c).begin());
#define repe(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define x first
#define y second
#define chk(a,k) ((bool)(a&(1<<(k))))
#define off(a,k) (a&(~(1<<(k))))
#define on(a,k) (a|(1<<(k)))
#define cbit(a) __builtin_popcount(a)
#define tbit(a) __builtin_ctz(a)
#define lbit(a) __builtin_clz(a)
#define sqr(a) ((a)*(a))
#define mod 1000000007

template<class T> int len(const T&c){return (int)c.size();}
template<class T> void cmin(T &a,T b){if(b<a) a=b;}
template<class T> void cmax(T &a,T b){if(b>a) a=b;}
template<class T, class U> T cast (U x) { T y; ostringstream a; a<<x; istringstream b(a.str()); b>>y; return y; }
vs split(string s, string x=" ") {vs res; rep(i,len(s)){string a; while(i<(int)s.size()&&x.find(s[i])==string::npos)a+=s[i++]; if(!a.empty())res.push_back((a));} return res;}
template<class T> inline T gcd(T a,T b) {if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
template<class T> inline T Pow(T b,T p){T ret=1;rep(i,p){ret*=b;}return ret;}

#define MAX 1000000
#define is_it(n) (flag[n>>6]&(1<<((n>>1)&31)))
#define set_it(n) (flag[n>>6]|=(1<<((n>>1)&31)))
int flag[MAX/64];
vector<int> sieve(){vi ret;unsigned i,j,k;flag[0]|=0;for(i=3;i<sqrt(MAX)+1;i+=2){if(!is_it(i)){for(j=i*i,k=i<<1;j<MAX;j+=k)set_it(j);}}if(MAX>1)ret.pb(2);for(i=3;i<MAX;i+=2){if(!is_it(i)){ret.pb(i);}}return ret;}

#define DEBUG 1
#if DEBUG && !ONLINE_JUDGE
	#define debug(args...) (Debugger()) , args
	class Debugger { public: Debugger(const std::string& _separator = ", ") : first(true), separator(_separator){} template<typename ObjectType> Debugger& operator , (const ObjectType& v) { if(!first) std::cerr << separator; std::cerr << v; first = false; return *this; } ~Debugger() { std::cerr << endl;} private: bool first; std::string separator; }; template <typename T1, typename T2> inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v) { bool first = true; os << "["; for(unsigned int i = 0; i < v.size(); i++) { if(!first) os << ", "; os << v[i]; first = false; } return os << "]"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::set<T>& v) { bool first = true; os << "["; for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii; first = false; } return os << "]"; } template<typename T1, typename T2> inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v) { bool first = true; os << "["; for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii ; first = false; } return os << "]"; }
#else
		#define debug(args...)
#endif

int n,m,t1,t2;
vi v1,v2;

int prcnt1[10000];
int prcnt2[10000];
mii mi1,mi2;

int main(){
	ios_base::sync_with_stdio(0);

	vi v=sieve();

	vi pr;

	rep(i,len(v)){
		if(sqr(v[i])>10000000)break;
		pr.pb(v[i]);
	}

	cin>>n>>m;

	rep(j,n){
		cin>>t1;
		v1.pb(t1);
		rep(i,len(pr)){
			while(t1%pr[i]==0){
				t1/=pr[i];
				prcnt1[pr[i]]++;
			}
		}
		if(t1>1)mi1[t1]+=1;
	}

	rep(j,m){
		cin>>t1;
		v2.pb(t1);
		rep(i,len(pr)){
			while(t1%pr[i]==0){
				t1/=pr[i];
				prcnt2[pr[i]]++;

			}
		}
		if(t1>1)mi2[t1]+=1;
	}

	rep(i,10000)prcnt2[i]=prcnt1[i]=min(prcnt1[i],prcnt2[i]);

	//~ cout<<prcnt1[2]<<"# "<<prcnt1[5]<<endl;
	//~ cout<<prcnt2[2]<<"# "<<prcnt2[5]<<endl;

	repe(i,mi1){
		mi2[i->x]=mi1[i->x]=min(mi1[i->x],mi2[i->x]);
	}
	repe(i,mi2){
		mi2[i->x]=mi1[i->x]=min(mi1[i->x],mi2[i->x]);
	}

	//debug(mi2);

	rep(j,n){

		rep(i,len(pr)){
			while(v1[j]%pr[i]==0&&prcnt1[pr[i]]){
				v1[j]/=pr[i];
				prcnt1[pr[i]]--;
			}
		}
		if(mi1[v1[j]]){
			mi1[v1[j]]-=1;
			v1[j]=1;
		}

	}

	rep(j,m){

		rep(i,len(pr)){

			while(v2[j]%pr[i]==0&&prcnt2[pr[i]]){
				v2[j]/=pr[i];
				prcnt2[pr[i]]--;
			}
		}

		if(mi2[v2[j]]){
			mi2[v2[j]]-=1;
			v2[j]=1;
		}
	}

	cout<<n<<" "<<m<<endl;

	rep(i,n)cout<<v1[i]<<" ";
	cout<<endl;
	rep(i,m)cout<<v2[i]<<" ";
	cout<<endl;

}


#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <complex>
#include <numeric>
#include <valarray>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

#define pi 3.14159265358979323846264338327950
const int dr[]={0,-1,0,1,-1,1, 1,-1};
const int dc[]={1,0,-1,0, 1,1,-1,-1};
const double eps=1e-9;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef pair<double, double> pdd;
typedef vector<pdd> vpdd;
typedef complex<double> pnt;
typedef vector<pnt> vpnt;
typedef vector<vector<pair<int,int> > > vvpii;
typedef pair<string, int> psi;
typedef vector<psi> vpsi;
typedef map<int,int> mii;
typedef map<string,int> msi;
typedef map<int,string> mis;

#define inf (1<<29)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define mem(x,a) memset(x,a,sizeof(x))
#define reps(i,a) repi(i,0,len(a))
#define rep(i,n) for(int i(0),_n(n);i<_n;++i)
#define repi(i,a,b) for(int i(a),_b(b);i<_b;++i)
#define repd(i,a,b) for(int i(a),_b(b);i>=_b;--i)
#define uniq(c) (c).resize(unique(c.begin(),c.end())-(c).begin());
#define repe(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define x first
#define y second
#define chk(a,k) ((bool)(a&(1<<(k))))
#define off(a,k) (a&(~(1<<(k))))
#define on(a,k) (a|(1<<(k)))
#define cbit(a) __builtin_popcount(a)
#define tbit(a) __builtin_ctz(a)
#define lbit(a) __builtin_clz(a)
#define sqr(a) ((a)*(a))
#define mod 1000000007

template<class T> int len(const T&c){return (int)c.size();}
template<class T> void cmin(T &a,T b){if(b<a) a=b;}
template<class T> void cmax(T &a,T b){if(b>a) a=b;}
template<class T, class U> T cast (U x) { T y; ostringstream a; a<<x; istringstream b(a.str()); b>>y; return y; }
vs split(string s, string x=" ") {vs res; rep(i,len(s)){string a; while(i<(int)s.size()&&x.find(s[i])==string::npos)a+=s[i++]; if(!a.empty())res.push_back((a));} return res;}
template<class T> inline T gcd(T a,T b) {if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
template<class T> inline T Pow(T b,T p){T ret=1;rep(i,p){ret*=b;}return ret;}

#define MAX 1000000
#define is_it(n) (flag[n>>6]&(1<<((n>>1)&31)))
#define set_it(n) (flag[n>>6]|=(1<<((n>>1)&31)))
int flag[MAX/64];
vector<int> sieve(){vi ret;unsigned i,j,k;flag[0]|=0;for(i=3;i<sqrt(MAX)+1;i+=2){if(!is_it(i)){for(j=i*i,k=i<<1;j<MAX;j+=k)set_it(j);}}if(MAX>1)ret.pb(2);for(i=3;i<MAX;i+=2){if(!is_it(i)){ret.pb(i);}}return ret;}

#define DEBUG 1
#if DEBUG && !ONLINE_JUDGE
	#define debug(args...) (Debugger()) , args
	class Debugger { public: Debugger(const std::string& _separator = ", ") : first(true), separator(_separator){} template<typename ObjectType> Debugger& operator , (const ObjectType& v) { if(!first) std::cerr << separator; std::cerr << v; first = false; return *this; } ~Debugger() { std::cerr << endl;} private: bool first; std::string separator; }; template <typename T1, typename T2> inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v) { bool first = true; os << "["; for(unsigned int i = 0; i < v.size(); i++) { if(!first) os << ", "; os << v[i]; first = false; } return os << "]"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::set<T>& v) { bool first = true; os << "["; for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii; first = false; } return os << "]"; } template<typename T1, typename T2> inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v) { bool first = true; os << "["; for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii ; first = false; } return os << "]"; }
#else
		#define debug(args...)
#endif

int n,m,t1,t2;
vi v1,v2;

int prcnt1[10000];
int prcnt2[10000];
mii mi1,mi2;

int main(){
	ios_base::sync_with_stdio(0);

	vi v=sieve();

	vi pr;

	rep(i,len(v)){
		if(sqr(v[i])>10000000)break;
		pr.pb(v[i]);
	}

	cin>>n>>m;

	rep(j,n){
		cin>>t1;
		v1.pb(t1);
		rep(i,len(pr)){
			while(t1%pr[i]==0){
				t1/=pr[i];
				prcnt1[pr[i]]++;
			}
		}
		if(t1>1)mi1[t1]++;
	}

	rep(j,m){
		cin>>t1;
		v2.pb(t1);
		rep(i,len(pr)){
			while(t1%pr[i]==0){
				t1/=pr[i];
				prcnt2[pr[i]]++;

			}
		}
		if(t1>1)mi2[t1]++;
	}

	rep(i,10000)prcnt2[i]=prcnt1[i]=min(prcnt1[i],prcnt2[i]);

	//~ cout<<prcnt1[2]<<"# "<<prcnt1[5]<<endl;
	//~ cout<<prcnt2[2]<<"# "<<prcnt2[5]<<endl;

	repe(i,mi1){
		mi2[i->x]=mi1[i->x]=min(mi1[i->x],mi2[i->x]);
	}
	repe(i,mi2){
		mi2[i->x]=mi1[i->x]=min(mi1[i->x],mi2[i->x]);
	}

	//debug(mi2);

	rep(j,n){

		rep(i,len(pr)){
			while(v1[j]%pr[i]==0&&prcnt1[pr[i]]){
				v1[j]/=pr[i];
				prcnt1[pr[i]]--;
			}
		}
		if(mi1[v1[j]]){
			mi1[v1[j]]-=1;
			v1[j]=1;
		}

	}

	rep(j,m){

		rep(i,len(pr)){

			while(v2[j]%pr[i]==0&&prcnt2[pr[i]]){
				v2[j]/=pr[i];
				prcnt2[pr[i]]--;
			}
		}

		if(mi2[v2[j]]){
			mi2[v2[j]]-=1;
			v2[j]=1;
		}
	}

	cout<<n<<" "<<m<<endl;

	rep(i,n)cout<<v1[i]<<" ";
	cout<<endl;
	rep(i,m)cout<<v2[i]<<" ";
	cout<<endl;

}


#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int nmax = 1e5+5;
vector<int> primes;
bool prime[10000];
vector<int> adivs[nmax],bdivs[nmax];
int main(){
	ios_base::sync_with_stdio(false);
	for(int i = 2; i < 10000; ++i)
		prime[i]=true;
	for(int i = 2; i < 10000; ++i)
		if(prime[i]){
			if(i*i<=10000000)
				primes.push_back(i);
			for(int j = 2*i; j < 10000; j+=i)
				prime[j]=false;
		}
	//cout << primes.size() << '\n';
	int n,m;
	cin >> n >> m;
	map<int,int> cnt;
	for(int i = 0; i < n; ++i){
		int a;
		cin >> a;
		for(int j = 0; j < primes.size(); ++j){
			while(a%primes[j]==0){
				cnt[primes[j]]++;
				adivs[i].push_back(primes[j]);
				a/=primes[j];
			}
		}
		if(a>1){
			cnt[a]++;
			adivs[i].push_back(a);
		}
	}
	for(int i = 0; i < m; ++i){
		int b;
		cin >> b;
		for(int j = 0; j < primes.size(); ++j){
			while(b%primes[j]==0){
				cnt[primes[j]]--;
				bdivs[i].push_back(primes[j]);
				b/=primes[j];
			}
		}
		if(b>1){
			cnt[b]--;
			bdivs[i].push_back(b);
		}
	}
	cout << n << ' ' << m << '\n';
	for(int i = 0; i < n; ++i){
		int a = 1;
		for(int j = 0; j < adivs[i].size(); ++j)
			if(cnt[adivs[i][j]]>0){
				cnt[adivs[i][j]]--;
				a*=adivs[i][j];
			}
		cout << a << " \n"[i==n-1];
	}
	for(int i = 0; i < m; ++i){
		int b = 1;
		for(int j = 0; j < bdivs[i].size(); ++j)
			if(cnt[bdivs[i][j]]<0){
				cnt[bdivs[i][j]]++;
				b*=bdivs[i][j];
			}
		cout << b << " \n"[i==m-1];
	}
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;

typedef pair<int,int> PII;
typedef long long LL;
const int PRIMES = 10000005;
const int INF = 1000000007;

int lo[PRIMES];

const vector<int>& sieve(int n){
    static vector<int> pl;
    lo[1]=1;
    for(static int x=2;x<n;x++){
        if(!lo[x]) pl.push_back(lo[x]=x);
        for(size_t i=0;i<pl.size() && x*pl[i]<n;i++){
            lo[x*pl[i]]=pl[i];
            if(x%pl[i]==0) break;
        }
    }
    return pl;
}

int a[100005],A[100005];
int b[100005],B[100005];
vector<int> u[100005];
vector<int> v[100005];

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    map<int,int> s;
    vector<int> pl=sieve(PRIMES);
    for(int i=0;i<n;i++){
        scanf("%d",a+i);
        A[i]=a[i];
        for(int x=a[i];x!=1;x/=lo[x]) s[lo[x]]++;
    }
    for(int i=0;i<m;i++){
        scanf("%d",b+i);
        B[i]=b[i];
        for(int x=b[i];x!=1;x/=lo[x]) if(s[lo[x]]-->0) B[i]/=lo[x];
    }
    s.clear();
    for(int i=0;i<m;i++)
        for(int x=b[i];x!=1;x/=lo[x]) s[lo[x]]--;
    for(int i=0;i<n;i++)
        for(int x=a[i];x!=1;x/=lo[x]) if(s[lo[x]]++<0) A[i]/=lo[x];
    printf("%d %d\n",n,m);
    for(int i=0;i<n;i++) printf("%d ",A[i]); puts("");
    for(int i=0;i<m;i++) printf("%d ",B[i]); puts("");
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;

typedef pair<int,int> PII;
typedef long long LL;
const int PRIMES = 1000005;
const int INF = 1000000007;

const vector<int>& sieve(int n){
    static vector<int> pl;
    static int lo[PRIMES],eu[PRIMES],rv[PRIMES];
    lo[1]=eu[1]=rv[1]=1;
    for(static int x=2;x<n;x++){
        rv[x]=rv[INF%x]*LL(INF-INF/x)%INF;
        if(!lo[x]) pl.push_back(lo[x]=x),eu[x]=x-1;
        for(size_t i=0;i<pl.size() && x*pl[i]<n;i++){
            lo[x*pl[i]]=pl[i];
            eu[x*pl[i]]=eu[x]*(pl[i]-(x%pl[i]==0));
            if(x%pl[i]==0) break;
        }
    }
    return pl;
}

vector<LL> factorize(LL n, const vector<int>& pl){
    vector<LL> ret;
    for(int i=0;pl[i]*LL(pl[i])<=n;i++)
        while(n%pl[i]==0) n/=pl[i],ret.push_back(pl[i]);
    if(n>1) ret.push_back(n);
    return ret;
}

int main(){
    int n,m,x;
    scanf("%d%d",&n,&m);
    map<int,int> s;
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        auto u=factorize(x,sieve(PRIMES));
        for(auto& c:u) s[c]++;
    }
    for(int i=0;i<m;i++){
        scanf("%d",&x);
        auto u=factorize(x,sieve(PRIMES));
        for(auto& c:u) s[c]--;
    }
    vector<int> num,den;
    for(auto i=s.begin();i!=s.end();++i){
        int x=i->second;
        for(int j=0;j<+x;j++) num.push_back(i->first);
        for(int j=0;j<-x;j++) den.push_back(i->first);
    }
    num.push_back(1);
    den.push_back(1);
    printf("%d %d\n",num.size(),den.size());
    for(auto& x:num) printf("%d ",x); puts("");
    for(auto& x:den) printf("%d ",x); puts("");
}


#include <cstdio>
#include <vector>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;

typedef pair<int,int> PII;
typedef long long LL;
const int PRIMES = 1000005;
const int INF = 1000000007;

const vector<int>& sieve(int n){
    static vector<int> pl;
    static int lo[PRIMES],eu[PRIMES],rv[PRIMES];
    lo[1]=eu[1]=rv[1]=1;
    for(static int x=2;x<n;x++){
        rv[x]=rv[INF%x]*LL(INF-INF/x)%INF;
        if(!lo[x]) pl.push_back(lo[x]=x),eu[x]=x-1;
        for(size_t i=0;i<pl.size() && x*pl[i]<n;i++){
            lo[x*pl[i]]=pl[i];
            eu[x*pl[i]]=eu[x]*(pl[i]-(x%pl[i]==0));
            if(x%pl[i]==0) break;
        }
    }
    return pl;
}

vector<LL> factorize(LL n, const vector<int>& pl){
    vector<LL> ret;
    for(int i=0;pl[i]*LL(pl[i])<=n;i++)
        while(n%pl[i]==0) n/=pl[i],ret.push_back(pl[i]);
    if(n>1) ret.push_back(n);
    return ret;
}

int a[100005],A[100005];
int b[100005],B[100005];

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    map<int,int> s;
    vector<int> pl=sieve(PRIMES);
    for(int i=0;i<n;i++){
        scanf("%d",a+i);
        auto u=factorize(a[i],pl);
        for(auto c:u) s[c]++;
    }
    for(int i=0;i<m;i++){
        scanf("%d",b+i);
        auto u=factorize(B[i]=b[i],pl);
        for(auto c:u) if(s[c]-->0) B[i]/=c;
    }
    s.clear();
    for(int i=0;i<m;i++){
        auto u=factorize(b[i],pl);
        for(auto c:u) s[c]--;
    }
    for(int i=0;i<n;i++){
        auto u=factorize(A[i]=a[i],pl);
        for(auto c:u) if(s[c]++<0) A[i]/=c;
    }
    printf("%d %d\n",n,m);
    for(int i=0;i<n;i++) printf("%d ",A[i]); puts("");
    for(int i=0;i<m;i++) printf("%d ",B[i]); puts("");
}


#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <typeinfo>
#include <set>
#include <cctype>
#include <locale>
#include <utility>
#include <map>
#include <iterator>
#include <cstring>
using namespace std;

#define sz size()
#define pb push_back
#define ppb pop_back
#define all(c) (c).begin(), (c).end()
#define sqr(a) (a)*(a)
#define mp(a,b) make_pair((a), (b))

typedef vector<int> vint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const double eps = 1e-6;

int in() {
    int a;
    scanf("%d", &a);
    return a;
}

double din() {
    double a;
    scanf("%lf", &a);
    return a;
}

ll gcd(ll a, ll b) {
    while(b){
        a %= b;
        swap(a,b);
    }
    return a;
}

int lcm(int a, int b) {
    return a / gcd(a, b) * b;
}

int main() {
   // freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n = in(), m = in();
    ll aa = 1, bb = 1;
    vector <int> a(n, 0), b(n, 0), res1, res2;
    for(int i = 0; i < n; ++i){
        a[i] = in();
        aa = aa * a[i];
    }
    for(int i = 0; i < m; ++i){
        b[i] = in();
        bb = bb * b[i];
    }
    ll g = gcd(aa, bb);
    aa /= g; bb /= g;
    for(int i=2;i*i<=aa;++i){
        if(!(aa%i)){
            while(!(aa%i)){
                res1.pb(i);
                aa/=i;
            }
        }
    }
    if(aa >= 1) res1.push_back(aa);
    for(int i=2;i*i<=bb;++i){
        if(!(bb%i)){
            while(!(bb%i)){
                res2.pb(i);
                bb/=i;
            }
        }
    }
    if(bb >= 1) res2.push_back(bb);
    printf("%d %d\n", res1.size(), res2.size());
    for(int i = 0; i < res1.size(); ++i)
        printf("%d ", res1[i]);
    printf("\n");
    for(int i = 0; i < res2.size(); ++i)
        printf("%d ", res2[i]);

    printf("\n");
    return 0;
}


#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <typeinfo>
#include <set>
#include <cctype>
#include <locale>
#include <utility>
#include <map>
#include <iterator>
#include <cstring>
using namespace std;

#define sz size()
#define pb push_back
#define ppb pop_back
#define all(c) (c).begin(), (c).end()
#define sqr(a) (a)*(a)
#define mp(a,b) make_pair((a), (b))

typedef vector<int> vint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const double eps = 1e-6;

int in() {
    int a;
    scanf("%d", &a);
    return a;
}

double din() {
    double a;
    scanf("%lf", &a);
    return a;
}

ll gcd(ll a, ll b) {
    while(b){
        a %= b;
        swap(a,b);
    }
    return a;
}

int lcm(int a, int b) {
    return a / gcd(a, b) * b;
}

int main() {
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n = in(), m = in();
    ll aa = 1, bb = 1;
    vector <int> a(n, 0), b(m, 0), res1, res2;
    for(int i = 0; i < n; ++i){
        a[i] = in();
        aa = aa * a[i];
    }
    for(int i = 0; i < m; ++i){
        b[i] = in();
        bb = bb * b[i];
    }
    ll g = gcd(aa, bb);
    aa /= g; bb /= g;
    for(int i=2;i*i<=aa;++i){
        if(!(aa%i)){
            while(!(aa%i)){
                res1.pb(i);
                aa/=i;
            }
        }
    }
    if(aa >= 1) res1.push_back(aa);
    for(int i=2;i*i<=bb;++i){
        if(!(bb%i)){
            while(!(bb%i)){
                res2.pb(i);
                bb/=i;
            }
        }
    }
    if(bb >= 1) res2.push_back(bb);
    printf("%d %d\n", res1.size(), res2.size());
    for(int i = 0; i < res1.size(); ++i)
        printf("%d ", res1[i]);
    printf("\n");
    for(int i = 0; i < res2.size(); ++i)
        printf("%d ", res2[i]);

    printf("\n");
    return 0;
}


//#pragma comment(linker, "/STACK:64777216")
#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <cctype>
#include <utility>
#include <map>
#include <iterator>
#include <complex>
#include <sstream>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <bitset>
using namespace std;

#define sz size()
#define pb push_back
#define ppb pop_back
#define all(c) (c).begin(), (c).end()
#define sqr(a) (a)*(a)
#define mp make_pair
#define rall(c) (c).rbegin(), (c).rend()
#define rmp reverse_make_pair

typedef vector<int> vint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<vector<int> > vvint;
typedef vector<char> vchar;
typedef vector<vector<int> > graph;
typedef unsigned char uchar;

const int INF=~(1<<31);
const double eps = 1e-9;

inline int in() {
    int a;
    scanf("%d", &a);
    return a;
}

ll gcd(ll a, ll b) {
    while(b){
        a%=b;
        swap(a,b);
    }
    return a;
}

ll binpow(ll  a, ll p, ll m){
    ll res = 1;
    while (p > 0) {
        if (p%2)
            res = (res * a) % m;
        a = (a * a) % m;
        p /= 2;
    }
    return res;
}

int MAXN = 10000005;
map <int, int> f[2], fn[2];
vector <int> nprime(MAXN, 0);
void fprime(){
    for(int i = 2; i < MAXN; ++i){
        if(nprime[i]) continue;
        for(int j = 2; j*i < MAXN; ++j){
            nprime[i*j] = 1;
            //printf("%d ", i*j);
        }
    }
}

void fact(int n, int ch){
    for(int i = 2; i*i <= n && nprime[n]; ++i){
        while(!(n % i)){
            f[ch][i]++;
            n /= i;
        }
    }
    if(n > 1){
        f[ch][n]++;
    }
    return;
}

int fact2(int n, bool ch){
    int res = 1;
    for(int i = 2; i*i <= n && nprime[n]; ++i){
        while(!(n%i)){
            if(fn[ch][i]){
                fn[ch][i]--;
                res *= i;
            }
            n /= i;
        }
    }
    if(n > 1){
        if(fn[ch][n]){
            fn[ch][n]--;
            res *= n;
        }
    }
    return res;
}

int main() {
    //freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    int n = in(), m = in();
    vector <int> a(n, 0), b(m,0);
    fprime();
    printf("\n");
    for(int i = 0; i < n; ++i){
        a[i]= in();
        fact(a[i], 0);
    }
    for(int i = 0; i < m; ++i){
        b[i] = in();
        fact(b[i], 1);
    }

    for(int i = 0; i < 2; ++i){
        for(map <int, int> :: iterator it = f[i].begin (); it != f[i].end (); ++it){
            fn[i][(*it).first] = f[i][(*it).first];
        }
        for(map <int, int> :: iterator it = fn[i].begin (); it != fn[i].end (); ++it){
            int x = (*it).first;
            fn[i][x] -=  min(f[i][x], f[!i][x]);
        }
    }
    printf("%d %d\n", n, m);
    for(int i = 0; i < n; ++i){
           printf("%d ", fact2(a[i], 0));
    }
    printf("\n");
    for(int i = 0; i < m; ++i){
           printf("%d ", fact2(b[i], 1));
    }
    return 0;
}


//#pragma comment(linker, "/STACK:64777216")
#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <cctype>
#include <utility>
#include <map>
#include <iterator>
#include <complex>
#include <sstream>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <bitset>
using namespace std;

#define sz size()
#define pb push_back
#define ppb pop_back
#define all(c) (c).begin(), (c).end()
#define sqr(a) (a)*(a)
#define mp make_pair
#define rall(c) (c).rbegin(), (c).rend()
#define rmp reverse_make_pair

typedef vector<int> vint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<vector<int> > vvint;
typedef vector<char> vchar;
typedef vector<vector<int> > graph;
typedef unsigned char uchar;

const int INF=~(1<<31);
const double eps = 1e-9;

inline int in() {
    int a;
    scanf("%d", &a);
    return a;
}

ll gcd(ll a, ll b) {
    while(b){
        a%=b;
        swap(a,b);
    }
    return a;
}

ll binpow(ll  a, ll p, ll m){
    ll res = 1;
    while (p > 0) {
        if (p%2)
            res = (res * a) % m;
        a = (a * a) % m;
        p /= 2;
    }
    return res;
}

int MAXN = 1000;
map <int, int> f[2], fn[2];
vector <int> nprime(MAXN, 0);
void fprime(){
    for(int i = 2; i < MAXN; ++i){
        if(nprime[i]) continue;
        for(int j = 2; j*i < MAXN; ++j){
            nprime[i*j] = 1;
            //printf("%d ", i*j);
        }
    }
}

void fact(int n, int ch){
    for(int i = 2; i*i <= n && nprime[n]; ++i){
        while(!(n % i)){
            f[ch][i]++;
            n /= i;
        }
    }
    if(n > 1){
        f[ch][n]++;
    }
    return;
}

int fact2(int n, bool ch){
    int res = 1;
    for(int i = 2; i*i <= n && nprime[n]; ++i){
        while(!(n%i)){
            if(fn[ch][i]){
                fn[ch][i]--;
                res *= i;
            }
            n /= i;
        }
    }
    if(n > 1){
        if(fn[ch][n]){
            fn[ch][n]--;
            res *= n;
        }
    }
    return res;
}

int main() {
    //freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    int n = in(), m = in();
    vector <int> a(n, 0), b(m,0);
    fprime();
    printf("\n");
    for(int i = 0; i < n; ++i){
        a[i]= in();
        fact(a[i], 0);
    }
    for(int i = 0; i < m; ++i){
        b[i] = in();
        fact(b[i], 1);
    }

    for(int i = 0; i < 2; ++i){
        for(map <int, int> :: iterator it = f[i].begin (); it != f[i].end (); ++it){
            fn[i][(*it).first] = f[i][(*it).first];
        }
        for(map <int, int> :: iterator it = fn[i].begin (); it != fn[i].end (); ++it){
            int x = (*it).first;
            fn[i][x] -=  min(f[i][x], f[!i][x]);
        }
    }
    printf("%d %d\n", n, m);
    for(int i = 0; i < n; ++i){
           printf("%d ", fact2(a[i], 0));
    }
    printf("\n");
    for(int i = 0; i < m; ++i){
           printf("%d ", fact2(b[i], 1));
    }
    return 0;
}


//#pragma comment(linker, "/STACK:64777216")
#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <cctype>
#include <utility>
#include <map>
#include <iterator>
#include <complex>
#include <sstream>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <bitset>
using namespace std;

#define sz size()
#define pb push_back
#define ppb pop_back
#define all(c) (c).begin(), (c).end()
#define sqr(a) (a)*(a)
#define mp make_pair
#define rall(c) (c).rbegin(), (c).rend()
#define rmp reverse_make_pair

typedef vector<int> vint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<vector<int> > vvint;
typedef vector<char> vchar;
typedef vector<vector<int> > graph;
typedef unsigned char uchar;

const int INF=~(1<<31);
const double eps = 1e-9;

inline int in() {
    int a;
    scanf("%d", &a);
    return a;
}

ll gcd(ll a, ll b) {
    while(b){
        a%=b;
        swap(a,b);
    }
    return a;
}

ll binpow(ll  a, ll p, ll m){
    ll res = 1;
    while (p > 0) {
        if (p%2)
            res = (res * a) % m;
        a = (a * a) % m;
        p /= 2;
    }
    return res;
}

int MAXN = 1000;
map <int, int> f[2], fn[2];
vector <int> nprime(MAXN, 0);
void fprime(){
    for(int i = 2; i < MAXN; ++i){
        if(nprime[i]) continue;
        for(int j = 2; j*i < MAXN; ++j){
            nprime[i*j] = 1;
            //printf("%d ", i*j);
        }
    }
}

void fact(int n, int ch){
    for(int i = 2; i*i <= n && nprime[n]; ++i){
        while(!(n % i)){
            f[ch][i]++;
            n /= i;
        }
    }
    if(n > 1){
        f[ch][n]++;
    }
    return;
}

int fact2(int n, bool ch){
    int res = 1;
    for(int i = 2; i*i <= n && nprime[n]; ++i){
        while(!(n%i)){
            if(fn[ch][i]){
                fn[ch][i]--;
                res *= i;
            }
            n /= i;
        }
    }
    if(n > 1){
        if(fn[ch][n]){
            fn[ch][n]--;
            res *= n;
        }
    }
    return res;
}

int main() {
    //freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    int n = in(), m = in();
    vector <int> a(n, 0), b(m,0);
    fprime();
    printf("\n");
    for(int i = 0; i < n; ++i){
        a[i]= in();
        fact(a[i], 0);
    }
    for(int i = 0; i < m; ++i){
        b[i] = in();
        fact(b[i], 1);
    }

    for(int i = 0; i < 2; ++i){
        for(map <int, int> :: iterator it = f[i].begin (); it != f[i].end (); ++it){
            fn[i][(*it).first] = f[i][(*it).first];
        }
        for(map <int, int> :: iterator it = fn[i].begin (); it != fn[i].end (); ++it){
            int x = (*it).first;
            fn[i][x] -=  min(f[i][x], f[!i][x]);
            printf("%d:%d ", x, (*it).second);
        }
        printf("\n");

    }
    printf("%d %d\n", n, m);
    for(int i = 0; i < n; ++i){
           printf("%d ", fact2(a[i], 0));
    }
    printf("\n");
    for(int i = 0; i < m; ++i){
           printf("%d ", fact2(b[i], 1));
    }
    return 0;
}


#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <typeinfo>
#include <set>
#include <cctype>
#include <locale>
#include <utility>
#include <map>
#include <iterator>
#include <cstring>
using namespace std;

#define sz size()
#define pb push_back
#define ppb pop_back
#define all(c) (c).begin(), (c).end()
#define sqr(a) (a)*(a)
#define mp(a,b) make_pair((a), (b))

typedef vector<int> vint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const double eps = 1e-6;

int in() {
    int a;
    scanf("%d", &a);
    return a;
}

double din() {
    double a;
    scanf("%lf", &a);
    return a;
}

ll gcd(ll a, ll b) {
    while(b){
        a %= b;
        swap(a,b);
    }
    return a;
}

int lcm(int a, int b) {
    return a / gcd(a, b) * b;
}

int main() {
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n = in(), m = in();
    ll aa = 1, bb = 1;
    vector <int> a(n, 0), b(m, 0), res1, res2;
    for(int i = 0; i < n; ++i){
        a[i] = in();
        aa = aa *(ll)a[i];
    }
    for(int i = 0; i < m; ++i){
        b[i] = in();
        bb = bb * (ll)b[i];
    }
    ll g = gcd(aa, bb);
    aa /= g; bb /= g;
    for(int i=2;i*(ll)i<=aa;++i){
        if(!(aa%(ll)i)){
            while(!(aa%(ll)i)){
                res1.pb(i);
                aa/=(ll)i;
            }
        }
    }
    if(aa >= 1) res1.push_back(aa);
    for(int i=2;i*(ll)i<=bb;++i){
        if(!(bb%(ll)i)){
            while(!(bb%(ll)i)){
                res2.pb(i);
                bb/=(ll)i;
            }
        }
    }
    if(bb >= 1) res2.push_back(bb);
    printf("%d %d\n", res1.size(), res2.size());
    for(int i = 0; i < res1.size(); ++i)
        printf("%d ", res1[i]);
    printf("\n");
    for(int i = 0; i < res2.size(); ++i)
        printf("%d ", res2[i]);

    printf("\n");
    return 0;
}


#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <typeinfo>
#include <set>
#include <cctype>
#include <locale>
#include <utility>
#include <map>
#include <iterator>
#include <cstring>
using namespace std;

#define sz size()
#define pb push_back
#define ppb pop_back
#define all(c) (c).begin(), (c).end()
#define sqr(a) (a)*(a)
#define mp(a,b) make_pair((a), (b))

typedef vector<int> vint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const double eps = 1e-6;

int in() {
    int a;
    scanf("%d", &a);
    return a;
}

double din() {
    double a;
    scanf("%lf", &a);
    return a;
}

ll gcd (ll a, ll b) {
    if (!b)
        return a;
    else
        return gcd (b, a % b);
}

int lcm(int a, int b) {
    return a / gcd(a, b) * b;
}

int main() {
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n = in(), m = in();
    ll aa = 1, bb = 1;
    vector <int> a(n, 0), b(m, 0), res1, res2;
    for(int i = 0; i < n; ++i){
        a[i] = in();
        aa = aa *(ll)a[i];
    }
    for(int i = 0; i < m; ++i){
        b[i] = in();
        bb = bb * (ll)b[i];
    }
    ll g = gcd(aa, bb);
    aa /= g; bb /= g;
    for(int i=2;i*(ll)i<=aa;++i){
        if(!(aa%(ll)i)){
            while(!(aa%(ll)i)){
                res1.pb(i);
                aa/=(ll)i;
            }
        }
    }
    if(aa >= 1) res1.push_back(aa);
    for(int i=2;i*(ll)i<=bb;++i){
        if(!(bb%(ll)i)){
            while(!(bb%(ll)i)){
                res2.pb(i);
                bb/=(ll)i;
            }
        }
    }
    if(bb >= 1) res2.push_back(bb);
    printf("%d %d\n", res1.size(), res2.size());
    for(int i = 0; i < res1.size(); ++i)
        printf("%d ", res1[i]);
    printf("\n");
    for(int i = 0; i < res2.size(); ++i)
        printf("%d ", res2[i]);

    printf("\n");
    return 0;
}


//#pragma comment(linker, "/STACK:64777216")
#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <cctype>
#include <utility>
#include <map>
#include <iterator>
#include <complex>
#include <sstream>
#include <ctime>
#include <cstring>
#include <iomanip>
#include <bitset>
using namespace std;

#define sz size()
#define pb push_back
#define ppb pop_back
#define all(c) (c).begin(), (c).end()
#define sqr(a) (a)*(a)
#define mp make_pair
#define rall(c) (c).rbegin(), (c).rend()
#define rmp reverse_make_pair

typedef vector<int> vint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<vector<int> > vvint;
typedef vector<char> vchar;
typedef vector<vector<int> > graph;
typedef unsigned char uchar;

const int INF=~(1<<31);
const double eps = 1e-9;

inline int in() {
    int a;
    scanf("%d", &a);
    return a;
}

ll gcd(ll a, ll b) {
    while(b){
        a%=b;
        swap(a,b);
    }
    return a;
}

ll binpow(ll  a, ll p, ll m){
    ll res = 1;
    while (p > 0) {
        if (p%2)
            res = (res * a) % m;
        a = (a * a) % m;
        p /= 2;
    }
    return res;
}

int MAXN = 100005;
map <int, int> f[2], fn[2];
vector <int> nprime(MAXN, 0);
void fprime(){
    for(int i = 2; i < MAXN; ++i){
        if(nprime[i]) continue;
        for(int j = 2; j*i < MAXN; ++j){
            nprime[i*j] = 1;
            //printf("%d ", i*j);
        }
    }
}

void fact(int n, int ch){
    for(int i = 2; i*i <= n && nprime[n]; ++i){
        while(!(n % i)){
            f[ch][i]++;
            n /= i;
        }
    }
    if(n > 1){
        f[ch][n]++;
    }
    return;
}

int fact2(int n, bool ch){
    int res = 1;
    for(int i = 2; i*i <= n && nprime[n]; ++i){
        while(!(n%i)){
            if(fn[ch][i]){
                fn[ch][i]--;
                res *= i;
            }
            n /= i;
        }
    }
    if(n > 1){
        if(fn[ch][n]){
            fn[ch][n]--;
            res *= n;
        }
    }
    return res;
}

int main() {
    //freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    int n = in(), m = in();
    vector <int> a(n, 0), b(m,0);
    fprime();
    printf("\n");
    for(int i = 0; i < n; ++i){
        a[i]= in();
        fact(a[i], 0);
    }
    for(int i = 0; i < m; ++i){
        b[i] = in();
        fact(b[i], 1);
    }

    for(int i = 0; i < 2; ++i){
        for(map <int, int> :: iterator it = f[i].begin (); it != f[i].end (); ++it){
            fn[i][(*it).first] = f[i][(*it).first];
        }
        for(map <int, int> :: iterator it = fn[i].begin (); it != fn[i].end (); ++it){
            int x = (*it).first;
            fn[i][x] -=  min(f[i][x], f[!i][x]);
        }
    }
    printf("%d %d\n", n, m);
    for(int i = 0; i < n; ++i){
           printf("%d ", fact2(a[i], 0));
    }
    printf("\n");
    for(int i = 0; i < m; ++i){
           printf("%d ", fact2(b[i], 1));
    }
    return 0;
}


#include <cstdio>
#include <map>
#include <vector>

#define MAX_PRIME 3162

bool Mark[3162];
int N, M, A[100005], B[100005];
using namespace std;
map<int,int> Num, Den;
vector<int> Prime;

void factorize(int n, map<int,int> &m)
{
    for(int p : Prime)
        while((n % p) == 0)
            n /= p, m[p]++;

    if(n > 1) m[n]++;
}

void output(int n, map<int,int> &m)
{
    int out = 1;

    for(int p : Prime)
        while((n % p) == 0 && m[p] > 0)
        {
            n /= p;
            m[p]--;
            out *= p;
        }

    if(n > 1 && m[n] > 0)
    {
        m[n]--;
        out *= n;
    }

    printf("%d ", out);
}

int main()
{
    for(int i = 3; i < MAX_PRIME; i++)
        if(!Mark[i])
            for(int k = 2*i; k < MAX_PRIME; k += i)
                Mark[k] = true;

    Prime.push_back(2);

    for(int i = 3; i < MAX_PRIME; i++)
        if(!Mark[i])
            Prime.push_back(i);

    scanf("%d %d", &N, &M);

    for(int i = 0; i < N; i++)
        scanf("%d", &A[i]);
    for(int i = 0; i < M; i++)
        scanf("%d", &B[i]);

    for(int i = 0; i < N; i++)
        factorize(A[i], Num);
    for(int i = 0; i < M; i++)
        factorize(B[i], Den);

    for(pair<int,int> p : Num)
    {
        map<int,int>::iterator it = Den.find(p.first);
        if(it == Den.end())
        {
            Den[p.first] = 0;
            it = Den.find(p.first);
        }

        int com = min(it->second, p.second);
        Num[p.first] -= com, Den[p.first] -= com;
    }

    printf("%d %d\n", N, M);

    for(int i = 0; i < N; i++)
        output(A[i], Num);
    printf("\n");
    for(int i = 0; i < M; i++)
        output(B[i], Den);
}


#include <cstdio>
#include <map>
#include <vector>
#define f(i,x) for(int i=0;i<x;i++)
using namespace std;
int N,M,A[100005],B[100005],k,Z[3162];
map<int,int> X,Y;
vector<int>P;
void g(int n, map<int,int> &m)
{
    for(int p:P)while(!(n%p))n/=p,m[p]++;
    if(n>1)m[n]++;
}
void o(int n, map<int,int> &m)
{
    int x=1;
    for(int p:P)while(!(n%p))
    {
        n/=p;
        if(m[p]>0)m[p]--,x*=p;
    }
    if(n>1&&m[n]>0)m[n]--,x*=n;
    printf("%d ",x);
}
int main()
{
    Z[0]=Z[1]=1;
    f(i,3162)if(!Z[i])for(k=2*i;k<3162;k+=i)Z[k]=1;
    f(i,3162)if(!Z[i])P.push_back(i);
    scanf("%d%d",&N,&M);
    f(i,N)scanf("%d",&A[i]);
    f(i,M)scanf("%d",&B[i]);
    f(i,N)g(A[i],X);
    f(i,M)g(B[i],Y);
    for(pair<int,int>p:X)
    {
        if(!Y[p.first]>0)Y[p.first]=0;
        int com = min(Y[p.first], p.second);
        X[p.first]-=com,Y[p.first]-=com;
    }
    printf("%d %d\n",N,M);
    f(i,N)o(A[i],X);
    printf("\n");
    f(i,M)o(B[i],Y);
}


#include <cstdio>
#include <map>
#include <vector>
#define f(i,x) for(int i=0;i<x;i++)
int N,M,A[100005],B[100005];
using namespace std;
map<int,int> X,Y;
void f(int n, map<int,int> &m)
{
    for(int p=2;p*p<=n;p++)while(!(n%p))n/=p,m[p]++;
    if(n>1)m[n]++;
}
void o(int n, map<int,int> &m)
{
    int out=1;
    for(int p=2;p*p<=n;p++)while(!(n%p))
    {
        n/=p;
        if(m[p]>0)m[p]--,out*=p;
    }
    if(n>1&&m[n]>0)m[n]--,out*=n;
    printf("%d ",out);
}
int main()
{
    scanf("%d%d",&N,&M);
    f(i,N)scanf("%d",&A[i]);
    f(i,M)scanf("%d",&B[i]);
    f(i,N)f(A[i],X);
    f(i,M)f(B[i],Y);
    for(pair<int,int>p:X)
    {
        if(!Y[p.first]>0)Y[p.first]=0;
        int com = min(Y[p.first], p.second);
        X[p.first]-=com,Y[p.first]-=com;
    }
    printf("%d %d\n",N,M);
    f(i,N)o(A[i],X);
    printf("\n");
    f(i,M)o(B[i],Y);
}


#include <cstdio>
#include <map>
#include <vector>

#define MAX_PRIME 3162

bool Mark[3162];
int N, M, A[100005], B[100005];
using namespace std;
map<int,int> Num, Den;
vector<int> Prime;

void factorize(int n, map<int,int> &m)
{
    for(int p : Prime)
        while(!(n % p))
            n /= p, m[p]++;

    if(n > 1) m[n]++;
}

void output(int n, map<int,int> &m)
{
    int out = 1;

    for(int p : Prime)
        while(!(n % p))
        {
            n /= p;

            if(m[p] > 0)
                m[p]--, out *= p;
        }

    if(n > 1 && m[n] > 0)
        m[n]--, out *= n;

    printf("%d ", out);
}

int main()
{
    for(int i = 3; i < MAX_PRIME; i++)
        if(!Mark[i])
            for(int k = 2*i; k < MAX_PRIME; k += i)
                Mark[k] = true;

    Prime.push_back(2);

    for(int i = 3; i < MAX_PRIME; i++)
        if(!Mark[i])
            Prime.push_back(i);

    scanf("%d %d", &N, &M);

    for(int i = 0; i < N; i++)
        scanf("%d", &A[i]);
    for(int i = 0; i < M; i++)
        scanf("%d", &B[i]);

    for(int i = 0; i < N; i++)
        factorize(A[i], Num);
    for(int i = 0; i < M; i++)
        factorize(B[i], Den);

    for(pair<int,int> p : Num)
    {
        map<int,int>::iterator it = Den.find(p.first);
        if(it == Den.end())
        {
            Den[p.first] = 0;
            it = Den.find(p.first);
        }

        int com = min(it->second, p.second);
        Num[p.first] -= com, Den[p.first] -= com;
    }

    printf("%d %d\n", N, M);

    for(int i = 0; i < N; i++)
        output(A[i], Num);
    printf("\n");
    for(int i = 0; i < M; i++)
        output(B[i], Den);
}


#include <cstdio>
#include <map>
#include <vector>

#define MAX_PRIME 3162

bool Mark[3162];
int N, M, A[100005], B[100005];
using namespace std;
map<int,int> Num, Den;
vector<int> Prime;

void factorize(int n, map<int,int> &m)
{
    for(int p : Prime)
        while(!(n % p))
            n /= p, m[p]++;

    if(n > 1) m[n]++;
}

void output(int n, map<int,int> &m)
{
    int out = 1;

    for(int p : Prime)
        while(!(n % p) && m[p] > 0)
        {
            n /= p;
            m[p]--;
            out *= p;
        }

    if(n > 1 && m[n] > 0)
    {
        m[n]--;
        out *= n;
    }

    printf("%d ", out);
}

int main()
{
    for(int i = 3; i < MAX_PRIME; i++)
        if(!Mark[i])
            for(int k = 2*i; k < MAX_PRIME; k += i)
                Mark[k] = true;

    Prime.push_back(2);

    for(int i = 3; i < MAX_PRIME; i++)
        if(!Mark[i])
            Prime.push_back(i);

    scanf("%d %d", &N, &M);

    for(int i = 0; i < N; i++)
        scanf("%d", &A[i]);
    for(int i = 0; i < M; i++)
        scanf("%d", &B[i]);

    for(int i = 0; i < N; i++)
        factorize(A[i], Num);
    for(int i = 0; i < M; i++)
        factorize(B[i], Den);

    for(pair<int,int> p : Num)
    {
        map<int,int>::iterator it = Den.find(p.first);
        if(it == Den.end())
        {
            Den[p.first] = 0;
            it = Den.find(p.first);
        }

        int com = min(it->second, p.second);
        Num[p.first] -= com, Den[p.first] -= com;
    }

    printf("%d %d\n", N, M);

    for(int i = 0; i < N; i++)
        output(A[i], Num);
    printf("\n");
    for(int i = 0; i < M; i++)
        output(B[i], Den);
}


#include <cstdio>
#include <map>
#include <vector>
#define f(i,x) for(int i=0;i<x;i++)
int N,M,A[100005],B[100005];
using namespace std;
map<int,int> X,Y;
void g(int n, map<int,int> &m)
{
    for(int p=2;p*p<=n;p++)while(!(n%p))n/=p,m[p]++;
    if(n>1)m[n]++;
}
void o(int n, map<int,int> &m)
{
    int out=1;
    for(int p=2;p*p<=n;p++)while(!(n%p))
    {
        n/=p;
        if(m[p]>0)m[p]--,out*=p;
    }
    if(n>1&&m[n]>0)m[n]--,out*=n;
    printf("%d ",out);
}
int main()
{
    scanf("%d%d",&N,&M);
    f(i,N)scanf("%d",&A[i]);
    f(i,M)scanf("%d",&B[i]);
    f(i,N)g(A[i],X);
    f(i,M)g(B[i],Y);
    for(pair<int,int>p:X)
    {
        if(!Y[p.first]>0)Y[p.first]=0;
        int com = min(Y[p.first], p.second);
        X[p.first]-=com,Y[p.first]-=com;
    }
    printf("%d %d\n",N,M);
    f(i,N)o(A[i],X);
    printf("\n");
    f(i,M)o(B[i],Y);
}


//#define PROB "test"    //{{{ Preamble
#ifndef LOCAL_PC
#  define NDEBUG
#  define LOG(...) static_cast<void>(0)
#else
#  include <logging.hpp>
#endif
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
//#include <iostream>
#include <cstdio>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
#  include <forward_list>
#  include <unordered_map>
#  include <unordered_set>
#endif
#include <ext/numeric>
using namespace std; typedef long long ll; typedef unsigned long long ull;
const double pi=3.14159265358979323846; const double E=2.7182818284590452354;
//static struct _{ ios_base::Init i; _(){cin.sync_with_stdio(0);cin.tie(0);} } _;
//inline ostream& endl(ostream& os) {return os << '\n';}  using __gnu_cxx::power;
template<class T> inline T sq(const T& x) { return x*x; }
//template<class I> inline void in(I d, int n) { do cin >> *d++; while (--n); }
#if defined(PROB) && !defined(LOCAL_PC)
static struct __{ __(){cin.rdbuf(a.open(PROB".in",ios::in));cout.rdbuf(b.open(
PROB".out",ios::out));} ~__(){cin.rdbuf(0);cout.rdbuf(0);} filebuf a,b; } __;
#endif
#define pb(...)      push_back(__VA_ARGS__)
#define eb(...)      emplace_back(__VA_ARGS__)
#define sz(v)        static_cast<int>((v).size())
#define X            first
#define Y            second
#define FOREACH(i,c) for(__typeof__((c).begin())i=(c).begin();i!=(c).end();++i)
#define mp(x, y)     make_pair((x), (y))
//}}}

inline void write_int(int x)
{
    char buf[10], *p = buf;
    do
    {
        *p++ = '0' + x % 10;
        x /= 10;
    }
    while (x);
    do
    {
        putchar(*--p);
    }
    while (p > buf);
}

inline int read_int()
{
    char c;

    while (c = getchar(), c <= ' ');

    int res = c - '0';
    while (c = getchar(), c >= '0' && c <= '9')
    {
        res = res * 10 + (c - '0');
    }

    return res;
}

const int lim = 10000001;

struct {
    char p;
    short a[8];
} __attribute__((__packed__)) sieve[lim];

int num[100000], denom[100000];

unordered_map<int, stack<int *>> um;

template<bool insert>
inline void add(int x, int *p)
{
    int y = x;
    for (int i = 0; i < sieve[x].p; i++)
    {
        int q = sieve[x].a[i];
        do
        {
            if (insert)
            {
                um[q].push(p);
            }
            else if (!um[q].empty())
            {
                *um[q].top() /= q; um[q].pop();
                *p /= q;
            }
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
    {
        if (insert)
        {
            um[y].push(p);
        }
        else if (!um[y].empty())
        {
            *um[y].top() /= y; um[y].pop();
            *p /= y;
        }
    }
}

int main()
{
    for (int i = 2; i*i < lim; i++)
    {
        if (sieve[i].p == 0)
        {
            for (int j = i*2; j < lim; j += i)
            {
                sieve[j].a[sieve[j].p++] = i;
            }
        }
    }

    um.reserve(2100000);

    int n = read_int(), m = read_int();
    for (int i = 0; i < n; i++)
    {
        num[i] = read_int();
        add<true>(num[i], num+i);
    }
    for (int i = 0; i < m; i++)
    {
        denom[i] = read_int();
        add<false>(denom[i], denom+i);
    }

    write_int(n); putchar(' '); write_int(m); putchar('\n');
    write_int(num[0]);
    for (int i = 1; i < n; i++)
        putchar(' '), write_int(num[i]);
    putchar('\n');
    write_int(denom[0]);
    for (int i = 1; i < m; i++)
        putchar(' '), write_int(denom[i]);
    putchar('\n');
}


//#define PROB "test"    //{{{ Preamble
#ifndef LOCAL_PC
#  define NDEBUG
#  define LOG(...) static_cast<void>(0)
#else
#  include <logging.hpp>
#endif
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
#  include <forward_list>
#  include <unordered_map>
#  include <unordered_set>
#endif
#include <ext/numeric>
using namespace std; typedef long long ll; typedef unsigned long long ull;
const double pi=3.14159265358979323846; const double E=2.7182818284590452354;
static struct _{ ios_base::Init i; _(){cin.sync_with_stdio(0);cin.tie(0);} } _;
inline ostream& endl(ostream& os) {return os << '\n';}  using __gnu_cxx::power;
template<class T> inline T sq(const T& x) { return x*x; }
template<class I> inline void in(I d, int n) { do cin >> *d++; while (--n); }
#if defined(PROB) && !defined(LOCAL_PC)
static struct __{ __(){cin.rdbuf(a.open(PROB".in",ios::in));cout.rdbuf(b.open(
PROB".out",ios::out));} ~__(){cin.rdbuf(0);cout.rdbuf(0);} filebuf a,b; } __;
#endif
#define pb(...)      push_back(__VA_ARGS__)
#define eb(...)      emplace_back(__VA_ARGS__)
#define sz(v)        static_cast<int>((v).size())
#define X            first
#define Y            second
#define FOREACH(i,c) for(__typeof__((c).begin())i=(c).begin();i!=(c).end();++i)
#define mp(x, y)     make_pair((x), (y))
//}}}

const int lim = 10000001;

struct {
    short p;
    short a[8];
} sieve[lim];

int arr[lim];

unordered_set<int> touched;

template<typename P>
inline void add(int x)
{
    int y = x;
    static P p;
    for (int i = 0; i < sieve[x].p; i++)
    {
        int q = sieve[x].a[i];
        do
        {
            touched.insert(q);
            arr[q] = p(arr[q], 1);
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
    {
        touched.insert(y);
        arr[y] = p(arr[y], 1);
    }
}

#if 0
template<typename G, typename S>
inline vector<int> compress()
{
    vector<int> res;

    res.reserve(100000);

    int l = 2, r = lim-1;
    static G g; static S s;
    while (l <= r)
    {
        if (!g(arr[r], 0))
        {
            r--;
            continue;
        }

        ll a = r; arr[r] = s(arr[r], 1);
        while (g(arr[r], 0) && a * r <= 10000000)
        {
            a *= r;
            arr[r] = s(arr[r], 1);
        }
        while (g(arr[l], 0) && a * l <= 10000000)
        {
            a *= l;
            arr[l] = s(arr[l], 1);
        }
        res.pb(a);

        if (!g(arr[l], 0))
            l++;
    }
    if (res.empty())
        res.pb(1);

    return res;
}
#endif

#if 1
inline void compress(const vector<int>& data, vector<int>& res)
{
    int i = 0, j = sz(data)-1;

    res.reserve(100000);

    while (i <= j)
    {
        ll a = data[j]; j--;
        while (i < j && a*data[j] <= 10000000)
        {
            a *= data[j--];
        }
        while (i <= j && a*data[i] <= 10000000)
        {
            a *= data[i++];
        }
        res.pb(a);
    }
}
#endif

int main()
{
    for (int i = 2; i*i < lim; i++)
    {
        if (sieve[i].p == 0)
        {
            for (int j = i*2; j < lim; j += i)
            {
                sieve[j].a[sieve[j].p++] = i;
            }
        }
    }

    int n, m; cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        int a; cin >> a;
        add<plus<int>>(a);
    }
    for (int i = 0; i < m; i++)
    {
        int b; cin >> b;
        add<minus<int>>(b);
    }

#if 0
    vector<int> num(compress<greater<int>, minus<int>>());
    vector<int> denom(compress<less<int>, plus<int>>());
#endif

#if 1
    vector<int> num_all, denom_all;
    num_all.reserve(2500000); denom_all.reserve(2500000);

    for (int i : touched)
    {
        while (arr[i] > 0)
        {
            num_all.pb(i);
            arr[i]--;
        }
        while (arr[i] < 0)
        {
            denom_all.pb(i);
            arr[i]++;
        }
    }
    if (num_all.empty())
        num_all.pb(1);
    if (denom_all.empty())
        denom_all.pb(1);

    vector<int> num, denom;
    compress(num_all, num);
    compress(denom_all, denom);
#endif

#if 0
    vector<int> num, denom;
    ll a = 1, b = 1;
    for (int i = 2; i < lim; !arr[i] ? i++ : 0)
    {
        if (arr[i] > 0)
        {
            if (a * i > 10000000)
            {
                num.pb(a);
                a = i;
            }
            else
            {
                a *= i;
            }
            arr[i]--;
        }
        else if (arr[i] < 0)
        {
            if (b * i > 10000000)
            {
                denom.pb(b);
                b = i;
            }
            else
            {
                b *= i;
            }
            arr[i]++;
        }
    }
    num.pb(a);
    denom.pb(b);
#endif

    cout << sz(num) << ' ' << sz(denom) << endl;
    cout << num[0];
    for (int i = 1; i < sz(num); i++)
        cout << ' '  << num[i];
    cout << endl;
    cout << denom[0];
    for (int i = 1; i < sz(denom); i++)
        cout << ' ' << denom[i];
    cout << endl;
}


//#define PROB "test"    //{{{ Preamble
#ifndef LOCAL_PC
#  define NDEBUG
#  define LOG(...) static_cast<void>(0)
#else
#  include <logging.hpp>
#endif
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
#  include <forward_list>
#  include <unordered_map>
#  include <unordered_set>
#endif
#include <ext/numeric>
using namespace std; typedef long long ll; typedef unsigned long long ull;
const double pi=3.14159265358979323846; const double E=2.7182818284590452354;
static struct _{ ios_base::Init i; _(){cin.sync_with_stdio(0);cin.tie(0);} } _;
inline ostream& endl(ostream& os) {return os << '\n';}  using __gnu_cxx::power;
template<class T> inline T sq(const T& x) { return x*x; }
template<class I> inline void in(I d, int n) { do cin >> *d++; while (--n); }
#if defined(PROB) && !defined(LOCAL_PC)
static struct __{ __(){cin.rdbuf(a.open(PROB".in",ios::in));cout.rdbuf(b.open(
PROB".out",ios::out));} ~__(){cin.rdbuf(0);cout.rdbuf(0);} filebuf a,b; } __;
#endif
#define pb(...)      push_back(__VA_ARGS__)
#define eb(...)      emplace_back(__VA_ARGS__)
#define sz(v)        static_cast<int>((v).size())
#define X            first
#define Y            second
#define FOREACH(i,c) for(__typeof__((c).begin())i=(c).begin();i!=(c).end();++i)
#define mp(x, y)     make_pair((x), (y))
//}}}

const int lim = 10000001;

struct {
    short p;
    short a[8];
} sieve[lim];

int num[100000], denom[100000];

vector<pair<int, int *>> nv, dv;

inline void add(vector<pair<int, int *>>& v, int x, int *p)
{
    int y = x;
    for (int i = 0; i < sieve[x].p; i++)
    {
        int q = sieve[x].a[i];
        do
        {
            v.eb(q, p);
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
        v.eb(y, p);
}

int main()
{
    for (int i = 2; i*i < lim; i++)
    {
        if (sieve[i].p == 0)
        {
            for (int j = i*2; j < lim; j += i)
            {
                sieve[j].a[sieve[j].p++] = i;
            }
        }
    }

    nv.reserve(2300000); dv.reserve(2300000);

    int n, m; cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> num[i];
        add(nv, num[i], num+i);
    }
    for (int i = 0; i < m; i++)
    {
        cin >> denom[i];
        add(dv, denom[i], denom+i);
    }

    sort(nv.begin(), nv.end());
    sort(dv.begin(), dv.end());

    int i = 0, j = 0;
    while (i < sz(nv) && j < sz(dv))
    {
        if (nv[i].X == dv[j].X)
        {
            int q = nv[i].X;
            *nv[i].Y /= q;
            *dv[j].Y /= q;
            i++;
            j++;
        }
        else
        {
            if (nv[i].X < dv[j].X)
                i++;
            else
                j++;
        }
    }

    cout << n << ' ' << m << endl;
    cout << num[0];
    for (int i = 1; i < n; i++)
        cout << ' '  << num[i];
    cout << endl;
    cout << denom[0];
    for (int i = 1; i < m; i++)
        cout << ' ' << denom[i];
    cout << endl;
}


//#define PROB "test"    //{{{ Preamble
#ifndef LOCAL_PC
#  define NDEBUG
#  define LOG(...) static_cast<void>(0)
#else
#  include <logging.hpp>
#endif
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
//#include <iostream>
#include <cstdio>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
#  include <forward_list>
#  include <unordered_map>
#  include <unordered_set>
#endif
#include <ext/numeric>
using namespace std; typedef long long ll; typedef unsigned long long ull;
const double pi=3.14159265358979323846; const double E=2.7182818284590452354;
//static struct _{ ios_base::Init i; _(){cin.sync_with_stdio(0);cin.tie(0);} } _;
//inline ostream& endl(ostream& os) {return os << '\n';}  using __gnu_cxx::power;
template<class T> inline T sq(const T& x) { return x*x; }
//template<class I> inline void in(I d, int n) { do cin >> *d++; while (--n); }
#if defined(PROB) && !defined(LOCAL_PC)
static struct __{ __(){cin.rdbuf(a.open(PROB".in",ios::in));cout.rdbuf(b.open(
PROB".out",ios::out));} ~__(){cin.rdbuf(0);cout.rdbuf(0);} filebuf a,b; } __;
#endif
#define pb(...)      push_back(__VA_ARGS__)
#define eb(...)      emplace_back(__VA_ARGS__)
#define sz(v)        static_cast<int>((v).size())
#define X            first
#define Y            second
#define FOREACH(i,c) for(__typeof__((c).begin())i=(c).begin();i!=(c).end();++i)
#define mp(x, y)     make_pair((x), (y))
//}}}

inline void write_int(int x)
{
    char buf[10], *p = buf;
    do
    {
        *p++ = '0' + x % 10;
        x /= 10;
    }
    while (x);
    do
    {
        putchar(*--p);
    }
    while (p > buf);
}

inline int read_int()
{
    char c;

    while (c = getchar(), c <= ' ');

    int res = c - '0';
    while (c = getchar(), c >= '0' && c <= '9')
    {
        res = res * 10 + (c - '0');
    }

    return res;
}

const int lim = 10000001;

struct {
    char p;
    short a[8];
} __attribute__((__packed__)) sieve[lim];

int num[100000], denom[100000];

struct mystruct {
    stack<int *> n, d;
};

unordered_map<int, mystruct> um;

template<stack<int *> mystruct::* A, stack<int *> mystruct::* B>
inline void add(int x, int *p)
{
    int y = x;
    for (int i = 0; i < sieve[x].p; i++)
    {
        int q = sieve[x].a[i];
        do
        {
            if (!(um[q].*B).empty())
            {
                *((um[q].*B).top()) /= q; (um[q].*B).pop();
                *p /= q;
            }
            else
            {
                (um[q].*A).push(p);
            }
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
    {
        if (!(um[y].*B).empty())
        {
            *((um[y].*B).top()) /= y; (um[y].*B).pop();
            *p /= y;
        }
        else
        {
            (um[y].*A).push(p);
        }
    }
}

int main()
{
    for (int i = 2; i*i < lim; i++)
    {
        if (sieve[i].p == 0)
        {
            for (int j = i*2; j < lim; j += i)
            {
                sieve[j].a[sieve[j].p++] = i;
            }
        }
    }

    um.reserve(4000000);

    int n = read_int(), m = read_int();
    for (int i = 0; i < n; i++)
    {
        num[i] = read_int();
        add<&mystruct::n, &mystruct::d>(num[i], num+i);
    }
    for (int i = 0; i < m; i++)
    {
        denom[i] = read_int();
        add<&mystruct::d, &mystruct::n>(denom[i], denom+i);
    }

    write_int(n); putchar(' '); write_int(m); putchar('\n');
    write_int(num[0]);
    for (int i = 1; i < n; i++)
        putchar(' '), write_int(num[i]);
    putchar('\n');
    write_int(denom[0]);
    for (int i = 1; i < m; i++)
        putchar(' '), write_int(denom[i]);
    putchar('\n');
}