Problem: One-Way Conveyors

You are working at a factory manufacturing many different products. Products have to be processed on a number of different machine tools. Machine shops with these machines are connected with conveyor lines to exchange unfinished products. Each unfinished product is transferred from a machine shop to another through one or more of these conveyors.

As the orders of the processes required are not the same for different types of products, the conveyor lines are currently operated in two-way. This may induce inefficiency as conveyors have to be completely emptied before switching their directions. Kaizen (efficiency improvements) may be found here!

Adding more conveyors is too costly. If all the required transfers are possible with currently installed conveyors operating in fixed directions, no additional costs are required. All the required transfers, from which machine shop to which, are listed at hand. You want to know whether all the required transfers can be enabled with all the conveyors operated in one-way, and if yes, directions of the conveyor lines enabling it.

Input

The input consists of a single test case of the following format.


$n$ $m$
$x_1$ $y_1$
.
.
.
$x_m$ $y_m$
$k$
$a_1$ $b_1$
.
.
.
$a_k$ $b_k$


The first line contains two integers $n$ ($2 \leq n \leq 10 000$) and $m$ ($1 \leq m \leq 100 000$), the number of machine shops and the number of conveyors, respectively. Machine shops are numbered $1$ through $n$. Each of the following $m$ lines contains two integers $x_i$ and $y_i$ ($1 \leq x_i < y_i \leq n$), meaning that the $i$-th conveyor connects machine shops $x_i$ and $y_i$. At most one conveyor is installed between any two machine shops. It is guaranteed that any two machine shops are connected through one or more conveyors. The next line contains an integer $k$ ($1 \leq k \leq 100 000$), which indicates the number of required transfers from a machine shop to another. Each of the following $k$ lines contains two integers $a_i$ and $b_i$ ($1 \leq a_i \leq n$, $1 \leq b_i \leq n$, $a_i \ne b_i$), meaning that transfer from the machine shop $a_i$ to the machine shop $b_i$ is required. Either $a_i \ne a_j$ or $b_i \ne b_j$ holds for $i \ne j$.

Output

Output “No” if it is impossible to enable all the required transfers when all the conveyors are operated in one-way. Otherwise, output “Yes” in a line first, followed by $m$ lines each of which describes the directions of the conveyors. All the required transfers should be possible with the conveyor lines operated in these directions. Each direction should be described as a pair of the machine shop numbers separated by a space, with the start shop number on the left and the end shop number on the right. The order of these $m$ lines do not matter as far as all the conveyors are specified without duplicates or omissions. If there are multiple feasible direction assignments, whichever is fine.

Sample Input 1


3 2
1 2
2 3
3
1 2
1 3
2 3


Sample Output 1


Yes
1 2
2 3


Sample Input 2


3 2
1 2
2 3
3
1 2
1 3
3 2


Sample Output 2


No


Sample Input 3


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


Sample Output 3


Yes
1 2
2 3
3 1
1 4






Example

Input

3 2
1 2
2 3
3
1 2
1 3
2 3


Output

Yes
1 2
2 3

Solution (in CPP):

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

#define int long long
#define endl "\n"
#define MOD 1000000007
#define INF 1000000010
#define EPS 1e-9
#define fst first
#define scd second

#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define lp(i,n) rep(i,n)
#define repn(i,n) for(int i=n;i>=0;i--);

void dfs1(vector<vector<pair<int,int>>> &graph, vector<int> &num, vector<int> &used, int node, int &con){

  if(used[node] ) return ;

  used[node] = true;

  for(pair<int,int> next : graph[node]){
    dfs1(graph, num, used, next.first, con);
  }

  num[con++] = node;
}

void dfs2(vector<vector<pair<int,int>>> &graph, vector<int>& scc, vector<int> &used, int node, int same){

  if(used[node] ) return ;

  used[node] = true;


 for(pair<int,int> next : graph[node]){
    dfs2(graph, scc, used, next.first, same);
  }

 scc[node] = same;

}


void SCC(vector<vector<pair<int,int>>> &graph, vector<int> &scc){
  vector<int> used(graph.size()), num(graph.size());
  vector<vector<pair<int,int>>> graph2(graph.size());
  int con = 0;

  scc.resize(graph.size());

  for(int i = 0; i < (int)graph.size(); i++){
    dfs1(graph, num, used, i, con);
  }

  for(int i = 0; i < (int)graph.size(); i++){
    for(int j = 0; j < (int)graph[i].size(); j++){
      graph2[graph[i][j].first].push_back(make_pair(i, graph[i][j].second));
    }
  }

  used.clear();
  used.resize(graph.size());

  for(int i = (int)graph.size()-1, same = 0; i >= 0; i--){
    if(!used[num[i]]){
      dfs2(graph2, scc, used, num[i], same);
      same++;
    }
  }
}

class union_find{
  vector<int> par, nume;
 public:

  union_find(){
  }

  void init(){
    for(int i = 0; i < (int)par.size(); i++){
      par[i] = i;
      nume[i] = 1;
    }
  }

  void resize(int x){
    par.resize(x);
    nume.resize(x);
    init();
  }

  int find(int x){
    return par[x] == x ? x : par[x] = find(par[x]);
  }

  void unite(int x, int y){
    x = find(x);
    y = find(y);

    if(x == y) return ;

    if(nume[x] > nume[y]) swap(x, y);

    par[x] = y;
    nume[y] += nume[x];
  }

  bool same(int x, int y){
    return find(x) == find(y);
  }

};


//map<pair<int,int>, int> used;
vector<vector<bool>> used;
union_find uf;

void dfs(vector<vector<pair<int,int>>>& G, vector<int> & scc, int num, int now,  int par){

  for(pair<int,int> next : G[now]){
    if(scc[next.first] != num) continue;
    if(used[now][next.first]) continue;
    if(used[next.first][now]) continue;
    used[now][next.first] = true;

    uf.unite(now, next.first);
    dfs(G, scc, num, next.first, now);
  }
}

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  vector<int> scc, scc2;
  vector<pair<int,int>> in, in2;
  vector<vector<pair<int,int>>> G, G2, G3;

  int n, m, k;

  cin>>n>>m;

  uf.resize(n);
  G.resize(n);
  G2.resize(n);
  G3.resize(n);

  in2.resize(m);

  used.resize(n, vector<bool>(n));

  for(int i = 0; i < m; i++){
    int x, y;

    cin>>x>>y;

    x--, y--;

    in2[i] = {x, y};

    G2[x].push_back({y,0});
    G2[y].push_back({x,0});
  }

  // cout<<"<2>"<<endl;

  cin>>k;

  in.resize(k);
  for(int i = 0; i < k; i++){
    int a, b;

    cin>>a>>b;

    a--, b--;
    in[i] = {a,b};

    G[a].push_back({b,0});
  }


  //  cout<<"SCC b"<<endl;

  SCC(G, scc);

  //cout<<"SCC e"<<endl;

  for(int i = 0; i < n; i++){
    dfs(G2, scc, scc[i], i, -1);
  }

  //cout<<"dfs e"<<endl;

  for(int i = 0; i < n; i++){ //cout<<"i = "<<i<<" G2[i].size()"<<G2[i].size()<<endl;
    for(int j = 0; j < (int)G2[i].size(); j++){
      // cout<<"j = "<<j<<endl;
      if(used[i][G2[i][j].first]) continue;
      if(used[G2[i][j].first][i]) continue;

      uf.unite(i,G2[i][j].first );

      if(scc[i] <= scc[G2[i][j].first]){
        used[i][G2[i][j].first] = true;
      } else {
        used[G2[i][j].first][i] = true;
      }
    }
  }

  //cout<<"Xx"<<endl;

  // for(pair<pair<int,int>,int> edge : used){
  // if(edge.second) G3[edge.first.first].push_back({edge.first.second, 0});
  // }

  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      if(used[i][j]) G3[i].push_back({j,0});
    }
  }


  SCC(G3, scc2);

  bool flag = true;

  for(int i = 0; i < k; i++){
    if(uf.same(in[i].first, in[i].second)){
      if(scc2[in[i].first] <= scc2[in[i].second]){
      } else {
        flag = false;
      }
    } else {
      flag = false;
      break;
    }
  }

  if(flag == false) cout<<"No"<<endl;
  else {
    cout<<"Yes"<<endl;


    for(int i = 0; i < m; i++){
      if(used[in2[i].first][in2[i].second]){
        cout<<in2[i].first+1<<" "<<in2[i].second+1<<endl;
      } else {
        cout<<in2[i].second+1<<" "<<in2[i].first+1<<endl;
      }
    }



  }
  return 0;
}