#include <bits/stdint-intn.h>

#include <algorithm>
#include <cstddef>
#include <ctime>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

const long long mod = 1e9 + 7;

int a[1000], n;

int64_t fac[1000], inv_fac[1000];

int64_t pw(int64_t x, int64_t y) {
  int64_t res = 1;
  while (y) {
    if (y & 1) {
      res = (res * x) % mod;
    }
    y >>= 1;
    x = (x * x) % mod;
  }
  return res;
}

void dfs(std::vector<bool>& is_valid, std::vector<std::pair<uint64_t, int>>& v,
         int k, int m, int mask, std::vector<uint64_t>& sets) {
  if (k == m) {
    is_valid[mask] = sets.size() < 2;
    return;
  }
  dfs(is_valid, v, k + 1, m, mask, sets);
  std::vector<uint64_t> new_sets;
  uint64_t new_mask = v[k].first;
  for (auto x : sets) {
    if (new_mask & x) {
      new_mask |= x;
    } else {
      new_sets.push_back(x);
    }
  }
  new_sets.push_back(new_mask);
  dfs(is_valid, v, k + 1, m, (mask | (1 << k)), new_sets);
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];

  fac[0] = inv_fac[0] = 1;
  for (int i = 1; i < 1000; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
    inv_fac[i] = (inv_fac[i - 1] * pw(i, mod - 2)) % mod;
  }

  vector<int> is_root(n, 0);
  vector<int> component_root(n, -1);
  vector<vector<int>> children(n), parents(n), components(n);
  for (int i = 0; i < n; i++) {
    is_root[i] = true;
    for (int j = 0; j < n; j++)
      if (j != i) {
        if (a[i] % a[j] == 0) {
          is_root[i] = false;
          break;
        }
      }
    int child_cnt = 0;
    for (int j = 0; j < n; j++) {
      if (j != i && a[j] % a[i] == 0) {
        child_cnt++;
      }
    }
    if (child_cnt < 2) is_root[i] = false;

    // if (is_root[i]) cerr << i << ' ' << a[i] << ' ' << child_cnt << endl;
  }

  for (int i = 0; i < n; i++) {
    if (is_root[i] && component_root[i] == -1) {
      component_root[i] = i;
      queue<int> q;
      // cerr << i << ' ';
      q.push(i);
      while (!q.empty()) {
        int x = q.front();
        q.pop();
        for (int j = 0; j < n; j++) {
          if (is_root[j] && component_root[j] == -1) {
            for (int k = 0; k < n; k++) {
              if (!is_root[k] && a[k] % a[x] == 0 && a[k] % a[j] == 0) {
                // if (a[k] == 30) {
                // cerr << "xxx " << a[i] << ' ' << a[j] << ' ' << endl;
                //}
                component_root[j] = i;
                q.push(j);
                // cerr << j << ' ';
                break;
              }
            }
          }
        }
      }
    }
    // cerr << endl;
  }

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      if (j != i && a[i] % a[j] == 0) {
        if (is_root[j]) {
          children[j].push_back(i);
          parents[i].push_back(j);
        }
      }
    }

  int64_t res = 1;
  int tot_cnt = 0;
  for (int i = 0; i < n; i++) {
    if (is_root[i] && component_root[i] == i) {
      std::vector<int> i_children, i_roots;
      for (int j = 0; j < n; j++) {
        if ((is_root[j] && component_root[j] == i)) {
          i_roots.push_back(j);
        } else if (!is_root[j] && parents[j].size() > 0 &&
                   component_root[parents[j][0]] == i) {
          i_children.push_back(j);
        }
      }

      std::cerr << a[i] << endl;
      for (auto x : i_roots) {
        std::cerr << a[x] << ' ';
      }
      cerr << endl;
      for (auto x : i_children) {
        cerr << a[x] << ' ';
      }
      std::cerr << endl;
      std::cerr << i_children.size() << " ---" << endl;

      int children_cnt = i_children.size();
      map<uint64_t, int> child_cnts_map;
      for (int x : i_children) {
        uint64_t key = 0;
        for (auto k : parents[x]) {
          key |= (1ll << k);
        }
        child_cnts_map[key]++;
      }
      cerr << child_cnts_map.size() << endl;

      int m = child_cnts_map.size();
      std::vector<std::pair<uint64_t, int>> child_cnts(child_cnts_map.begin(),
                                                       child_cnts_map.end());
      sort(child_cnts.begin(), child_cnts.end(),
           [](const auto& a, const auto& b) {
             return __builtin_popcountll(a.first) >
                    __builtin_popcountll(b.first);
           });
      int max_m = 0;
      for (; max_m < m; max_m++)
        if (__builtin_popcountll(child_cnts[max_m].first) == 1) break;

      /* auto is_status_valid = [&child_cnts, &m](uint64_t bitmask) -> bool {
        vector<uint64_t> regions;
        vector<uint64_t> new_regions;
        for (int i = 0; i < m; i++)
          if (1 & (bitmask >> i)) {
            new_regions.clear();
            uint64_t new_mask = child_cnts[i].first;
            for (auto r : regions) {
              if ((r & child_cnts[i].first)) {
                new_mask |= r;
              } else
                new_regions.push_back(r);
            }
            new_regions.push_back(new_mask);
            regions.swap(new_regions);
          }
        return regions.size() <= 1;
      };*/
      // vector<vector<int64_t>> is_valid(1 << m);
      // int max_connection = 0;
      // for (int i = 0; i < m; i++) {
      //}

      vector<bool> is_valid(1 << m);
      vector<uint64_t> _v;
      dfs(is_valid, child_cnts, 0, m, 0, _v);

      /*cout << child_cnts[0].first << "xxxxxxxxx"*/
      // for (int i = 0; i < (1 << m); i++) {
      // if (is_valid[i]) cerr << i << ' ';
      //}

      cerr << time(NULL) << endl;

      vector<int64_t> p(1 << m);
      vector<int> sum(1 << m);

      p[(1 << m) - 1] = 1;
      sum[(1 << m) - 1] = 0;
      for (int bitmask = (1 << m) - 1; bitmask > 0; bitmask--) {
        if (p[bitmask]) {
          for (int k = 0; k < m; k++)
            if ((1 & (bitmask >> k))) {
              int new_sum = sum[bitmask ^ (1 << k)] =
                  sum[bitmask] + child_cnts[k].second;

              int new_bitmask = bitmask ^ (1 << k);
              if (is_valid[new_bitmask]) {
                int add = child_cnts[k].second;
                p[new_bitmask] +=
                    p[bitmask] *
                    (((fac[new_sum - 1] * inv_fac[sum[bitmask]]) % mod * add) %
                     mod) %
                    mod;
                p[new_bitmask] %= mod;
              }
            }
        }
      }
      cerr << time(NULL) << endl;

      res *= p[0];
      res %= mod;
      res *= fac[children_cnt - 1 + tot_cnt];
      res %= mod;
      res *= inv_fac[children_cnt - 1];
      res %= mod;
      res *= inv_fac[tot_cnt];
      res %= mod;
      tot_cnt += children_cnt - 1;
    }
  }

  cout << res << endl;
  return 0;
}


///1292f
#include <bits/stdc++.h>
#pragma GCC target ("sse4.2")
#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize ("unroll-loops")
using namespace std;

typedef long long ll;
typedef long double ld;
#define sp(x) fixed<<setprecision(x)
#define pb push_back
#define eb emplace_back
#define die(x) return cout << x << endl, 0
#define fastio ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
#define file ifstream cin("input.txt"); ofstream cout("output.txt")
//#define endl '\n'
#define pii pair<int, int>
#define F first
#define S second
#define int long long
const int MAXN = 120 + 7, MOD = 1000 * 1000 * 1000 + 7;
const ld EPS = ld(1e-10);
int n, a[MAXN], sum, c[MAXN][MAXN], indeg[MAXN];
vector<int>adj[MAXN], t, s;
int ans = 1, cur= 0;
bool mark[MAXN];
int calc_cnt(){

	int ss = s.size(), ts = t.size();
	vector<int>num(ts, 0), cnt(1 << ss, 0);
	vector<vector<int>>dp(ts + 1, vector<int>(1 << ss, 0);
	for(int i = 0; i < ss; i ++)
		for(int j = 0; j < ts; j ++)
			if(t[j] % s[i] == 0)
				num[j] |= (1 << i);
	for(int i = 0; i < (1 << ss); i ++)
		for(int j = 0; j < ts; j ++)
			if((num[j] & i) == num[j])
				cnt[i] ++;
	for(int i = 0; i < ts; i ++)
		dp[1][num[i]] ++;
	for(int k = 1; k < ts; k ++){
		for(int mask = 0; mask < (1 << ss); mask ++){
			dp[k][mask] %= MOD;
			for(int i = 0; i < ts; i ++){
				int ands = (num[i] & mask);
				if(ands != num[i] && ands != 0){
					dp[k + 1][mask | num[i]] %= MOD;
					dp[k + 1][mask | num[i]] += dp[k][mask];
					dp[k + 1][mask | num[i]] %= MOD;
				}
			}
			dp[k + 1][mask] = ((dp[k + 1][mask]% MOD) + (1ll * (dp[k][mask] % MOD * ((cnt[mask] - k) % MOD)) % MOD) % MOD);
		}
	}
	return dp[ts][(1 << ss) - 1];
}

void dfs(int v){
    mark[v] = 1;
    if(indeg[v] == 0){
        s.pb(a[v]);
    }else{
    	t.pb(a[v]);
    }
    for(auto u : adj[v]){
        if(!mark[u]){
            dfs(u);
        }
    }
    return;
}
void dfs_all(){
    for(int i = 0; i < n; i ++){
        if(!mark[i]){
        	s.clear(), t.clear();
            dfs(i);
            if(!t.empty()){
				int cnt = calc_cnt(), tt = t.size() - 1;
				ans = 1ll * (ans%MOD) * (cnt % MOD) % MOD;
				ans = 1ll * (ans % MOD) * (c[cur + tt][tt] % MOD) % MOD;
				cur += tt;
			}
        }
    }
    return;
}
int32_t main(){
    fastio;
    for(int i = 0; i < MAXN; i ++){
    	c[i][0]  = 1;
    	for(int j = 1; j <= i; j ++){
    		c[i][j] = (c[i - 1][j - 1] + c[i - 1][j])%MOD;
    	}
    }
    cin >> n;
    for(int i = 0; i < n; i ++){
        cin >> a[i];
    }
    sort(a, a + n);
    for(int i = 0; i < n; i ++){
        for(int j = 0; j < n; j ++)if(i != j){
            if(a[j] % a[i] == 0){
                adj[j].pb(i);
                adj[i].pb(j);
                indeg[j] ++;
            }
        }
    }

    dfs_all();
	cout << ans % MOD << endl;
    return 0;

}


#include <cstddef>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <string>
#include <vector>

using namespace std;

const long long mod = 1e9 + 7;

int a[1000], n;

int64_t fac[1000], inv_fac[1000];

int64_t pw(int64_t x, int64_t y) {
  int64_t res = 1;
  while (y) {
    if (y & 1) {
      res = (res * x) % mod;
    }
    y >>= 1;
    x = (x * x) % mod;
  }
  return res;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];

  fac[0] = inv_fac[0] = 1;
  for (int i = 1; i < 1000; i++) {
    fac[i] = (fac[i - 1] * i) % mod;
    inv_fac[i] = (inv_fac[i - 1] * pw(i, mod - 2)) % mod;
  }

  vector<int> is_root(n, 0);
  vector<int> component_root(n, -1);
  vector<vector<int>> children(n), parents(n), components(n);
  for (int i = 0; i < n; i++) {
    is_root[i] = true;
    for (int j = 0; j < n; j++)
      if (j != i) {
        if (a[i] % a[j] == 0) {
          is_root[i] = false;
          break;
        }
      }
    int child_cnt = 0;
    for (int j = 0; j < n; j++) {
      if (j != i && a[j] % a[i] == 0) {
        child_cnt++;
      }
    }
    if (child_cnt < 2) is_root[i] = false;

    // if (is_root[i]) cerr << i << ' ' << a[i] << ' ' << child_cnt << endl;
  }

  for (int i = 0; i < n; i++) {
    if (is_root[i] && component_root[i] == -1) {
      component_root[i] = i;
      queue<int> q;
      // cerr << i << ' ';
      q.push(i);
      while (!q.empty()) {
        int x = q.front();
        q.pop();
        for (int j = 0; j < n; j++) {
          if (is_root[j] && component_root[j] == -1) {
            for (int k = 0; k < n; k++) {
              if (!is_root[k] && a[k] % a[x] == 0 && a[k] % a[j] == 0) {
                // if (a[k] == 30) {
                // cerr << "xxx " << a[i] << ' ' << a[j] << ' ' << endl;
                //}
                component_root[j] = i;
                q.push(j);
                // cerr << j << ' ';
                break;
              }
            }
          }
        }
      }
    }
    // cerr << endl;
  }

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      if (j != i && a[i] % a[j] == 0) {
        if (is_root[j]) {
          children[j].push_back(i);
          parents[i].push_back(j);
        }
      }
    }

  int64_t res = 1;
  int tot_cnt = 0;
  for (int i = 0; i < n; i++) {
    if (is_root[i] && component_root[i] == i) {
      std::vector<int> i_children, i_roots;
      for (int j = 0; j < n; j++) {
        if ((is_root[j] && component_root[j] == i)) {
          i_roots.push_back(j);
        } else if (!is_root[j] && parents[j].size() > 0 &&
                   component_root[parents[j][0]] == i) {
          i_children.push_back(j);
        }
      }

      std::cerr << a[i] << endl;
      for (auto x : i_roots) {
        std::cerr << a[x] << ' ';
      }
      cerr << endl;
      for (auto x : i_children) {
        cerr << a[x] << ' ';
      }
      std::cerr << endl;
      std::cerr << i_children.size() << " ---" << endl;

      int children_cnt = i_children.size();
      map<uint64_t, int> child_cnts_map;
      for (int x : i_children) {
        uint64_t key = 0;
        for (auto k : parents[x]) {
          key |= (1ll << k);
        }
        child_cnts_map[key]++;
      }
      cerr << child_cnts_map.size() << endl;

      int m = child_cnts_map.size();
      std::vector<std::pair<uint64_t, int>> child_cnts(child_cnts_map.begin(),
                                                       child_cnts_map.end());

      cerr << time(NULL) << endl;
      /* auto is_status_valid = [&child_cnts, &m](uint64_t bitmask) -> bool {
        vector<uint64_t> regions;
        vector<uint64_t> new_regions;
        for (int i = 0; i < m; i++)
          if (1 & (bitmask >> i)) {
            new_regions.clear();
            uint64_t new_mask = child_cnts[i].first;
            for (auto r : regions) {
              if ((r & child_cnts[i].first)) {
                new_mask |= r;
              } else
                new_regions.push_back(r);
            }
            new_regions.push_back(new_mask);
            regions.swap(new_regions);
          }
        return regions.size() <= 1;
      };*/
      vector<vector<int64_t>> is_valid(1 << m);
      vector<vector<int64_t>> p(children_cnt + 1);
      for (int i = 0; i < m; i++) {
        is_valid[1 << i].push_back(child_cnts[i].first);
        for (int j = 1; j < (1 << i); j++) {
          uint64_t new_mask = child_cnts[i].first;
          for (auto x : is_valid[j]) {
            if (new_mask & x) {
              new_mask |= x;
            } else {
              is_valid[(1 << i) | j].push_back(x);
            }
          }
          is_valid[(1 << i) | j].push_back(new_mask);
        }
      }
      cerr << time(NULL) << endl;

      for (int k = 0; k <= children_cnt; k++) p[k].resize(1 << m);
      p[0][(1 << m) - 1] = 1;
      for (int cnt = 0; cnt < children_cnt; cnt++) {
        for (int bitmask = 0; bitmask < (1 << m); bitmask++)
          if (p[cnt][bitmask]) {
            for (int k = 0; k < m; k++)
              if ((1 & (bitmask >> k))) {
                int new_bitmask = bitmask ^ (1 << k);
                if (is_valid[new_bitmask].size() < 2) {
                  int add = child_cnts[k].second;
                  if (add + cnt <= children_cnt) {
                    p[cnt + add][new_bitmask] +=
                        p[cnt][bitmask] *
                        (((fac[cnt + add - 1] * inv_fac[cnt]) % mod * add) %
                         mod) %
                        mod;
                    p[cnt + add][new_bitmask] %= mod;
                  }
                }
              }
          }
      }
      cerr << time(NULL) << endl;

      res *= p[children_cnt][0];
      res %= mod;
      res *= fac[children_cnt - 1 + tot_cnt];
      res %= mod;
      res *= inv_fac[children_cnt - 1];
      res %= mod;
      res *= inv_fac[tot_cnt];
      res %= mod;
      tot_cnt += children_cnt - 1;
    }
  }

  cout << res << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0)
int in(){int32_t x ; scanf("%d",&x) ;  return x ;}
inline long long lin() {long long x ; scanf("%lld",&x) ; return x ;}
const int maxn = 2e5 + 10 ;
int a[maxn] ,cnt[21] , ans;
vector<pair<int,int> > op;
int t;
inline void solve(int n , int from , int to , bool rev = 0)
{
    if (n == 0) return;
    int other = from ^ to ^ 3;
    if (rev || a[n ] == 1)
    {
        solve(n-1,from,other,true);
        for (int i = 0 ; i < a[n] ; i ++)
            op.push_back({from,to});
        solve(n-1,other,to,true);
        return ;
    }
    if (n == 1)
    {
        for (int i = 0 ; i < a[n] - 1 ; i ++)
            op.push_back({from,other});
        op.push_back({from,to});
        for (int i = 0 ; i < a[n] - 1 ; i ++)
            op.push_back({other,to});
        return ;
    }
    solve(n-1,from,to,1);
    for (int i = 0 ; i < a[n] ; i ++)
        op.push_back({from,other});
    solve(n-1,to,from,1);
    for (int i = 0 ; i < a[n] ; i ++)
        op.push_back({other,to});
    solve(n-1,from,to,0);
}
int32_t main()
{
    int n = in();
    for (int i = 0 ; i < n ; i++) a[i] = in() , cnt[a[i]] ++;
    for (int i = 1 ; i <= 20 ; i ++)
        if (cnt[i]){
            a[++t] = cnt[i];
        }
    solve(t,0,2);
    cout << op.size() << "\n";
    for (auto x : op)
        cout << x.first + 1 << " " << x.second + 1 << "\n";
}


#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iterator>
#include <cassert>
#include <stack>
#include <queue>
#include <ctime>
#include <vector>
#include <map>
#include <set>
#include <utility>

using namespace std;

#define FORALL(i,a,b) for (int i = (a); i <= (b); i++)
#define FOR(i,n) for (int i=0;i<n;i++)
#define FORB(i,a,b) for (int i = (a); i >= (b); i--)
#define FORIN(it,type,b) for(type::iterator it = (b).begin(); it != (b).end(); it++)
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;

#define in cin
#define out cout
#define dout if(true) cout

#define MAXN 25
#define MAXS 1148576
int A[MAXN];
int G[MAXN];

typedef vector < pair <int,int> > vpii;
//vpii res;

int smemo[MAXN][2];

stack<int> fin[4];

int S(int p, int type, int me, int go, int other, vpii& tres){
	int & ans = smemo[p][type];
	//assert( ans <= 0);	//remove this assertion, its fine
	//if (ans > 0) return ans;

	int t;

	if (p==1){
		if (type==0){
			FOR(i,G[p]-1) tres.pb( mp(me,other));
			tres.pb(mp(me,go));
			FOR(i,G[p]-1) tres.pb(mp(other,go));
			return ans = 2*G[p]-1;
		}else{
			FOR(i,G[p]) tres.pb(mp(me,go));
			return ans = G[p];
		}
	}

	//r1

	//int flip = S(
	//int norm = smemo[p-1][0];

	vpii r1,r2;
	int f = S(p-1,1,me,other,go, r1);
	int r = S(p-1,0,me,other,go, r2);

	assert((f <= r && r <= 2*f) || (r <= f && f <= 2*r));

	if (type == 1){	//get flipped
		if (f<=r){
			int flip1 = S(p-1,1,me,other,go, tres);
			FOR(i,G[p]) tres.pb(mp(me,go));
			int flip2 = S(p-1,1,other,go,me, tres);
			assert(flip1 == flip2);
			ans = flip1 + flip2 + G[p];
			return ans;
		}else{
			int norm1 = S(p-1,0,me,other,go, tres);
			FOR(i,G[p]) tres.pb(mp(me,go));
			int norm2 = S(p-1,0,other,go,me, tres);
			assert(norm1 == norm2);
			ans = norm1 + norm2 + G[p];
			return ans;
		}
	}else if (G[p] == 1){ //go normal
		if (f <= r){
			int flip1 = S(p-1,1,me,other,go, tres);
			tres.pb(mp(me,go));
			int flip2 = S(p-1,1,other,go,me, tres);
			assert(flip1 == flip2);
			//int norm = S(p-1,0,other,go,me,r3);
			ans = G[p] + flip1 + flip2;
			return ans;
		}else{
			int norm1 = S(p-1,0,me,other,go,tres);
			tres.pb(mp(me,go));
			int norm2 = S(p-1,0,other,go,me, tres);
			assert(norm1 == norm2);
			ans = G[p] + norm1 + norm2;
			return ans;
		}
	}else{
		if (f <= r){
			int flip1 = S(p-1,1,me,go,other, tres);
			FOR(i,G[p]) tres.pb(mp(me,other));
			int flip2 = S(p-1,1,go,me,other, tres);
			assert(flip1 == flip2);
			FOR(i,G[p]) tres.pb(mp(other,go));
			int norm = S(p-1,0,me,go,other,tres);
			ans = flip1 + flip2 + norm + 2*G[p];
			return ans;
		}else{
			int norm1 = S(p-1,0,me,go,other, tres);
			FOR(i,G[p]) tres.pb(mp(me,other));
			int norm2 = S(p-1,0,go,me,other, tres);
			assert(norm1 == norm2);
			FOR(i,G[p]) tres.pb(mp(other,go));
			int norm3 = S(p-1,0,me,go,other,tres);
			assert(norm1 == norm3);
			ans = norm1 + norm2 + norm3 + 2*G[p];
			return ans;
		}
	}

	throw "bad!";
}

int main()
{
	int N;
	in >> N;
	int P = 0;
	memset(G,0,sizeof(G));
	FOR(i,N) {
		in >> A[i];
		fin[1].push(A[i]);
		if (!(G[P] == 0 || A[i] == A[i-1])){ P++;}
		G[P]++;
	}
	P++;
	//cout << P << endl;
	G[P] = 0; reverse(G,G+P+1);
	A[N] = 0; reverse(A,A+N+1);

	vpii res;
	int ans = S(P,0,1,3,2,res);

	assert(ans == res.size());
	cout << ans << endl;
	FOR(i,ans){
		cout << res[i].first << " " << res[i].second << endl;
	}
	return 0;
}


#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iterator>
#include <cassert>
#include <stack>
#include <queue>
#include <ctime>
#include <vector>
#include <map>
#include <set>
#include <utility>

using namespace std;

#define FORALL(i,a,b) for (int i = (a); i <= (b); i++)
#define FOR(i,n) for (int i=0;i<n;i++)
#define FORB(i,a,b) for (int i = (a); i >= (b); i--)
#define FORIN(it,type,b) for(type::iterator it = (b).begin(); it != (b).end(); it++)
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;

#define in cin
#define out cout
#define dout if(true) cout

#define MAXN 25
#define MAXS 1148576
int A[MAXN];
int G[MAXN];

typedef vector < pair <int,int> > vpii;
//vpii res;

int smemo[MAXN][2];

stack<int> fin[4];

int S(int p, int type, int me, int go, int other, vpii& tres){
	int & ans = smemo[p][type];
	//assert( ans <= 0);	//remove this assertion, its fine
	//if (ans > 0) return ans;

	int t;

	if (p==1){
		if (type==0){
			FOR(i,G[p]-1) tres.pb( mp(me,other));
			tres.pb(mp(me,go));
			FOR(i,G[p]-1) tres.pb(mp(other,go));
			return ans = 2*G[p]-1;
		}else{
			FOR(i,G[p]) tres.pb(mp(me,go));
			return ans = G[p];
		}
	}

	//r1

	//int flip = S(
	//int norm = smemo[p-1][0];

	vpii r1,r2;
	int f = S(p-1,1,me,other,go, r1);
	int r = S(p-1,0,me,other,go, r2);

	assert((f <= r && r <= 2*f) || (r <= f && f <= 2*r));

	if (type == 1){	//get flipped
		if (f<=r){
			int flip1 = S(p-1,1,me,other,go, tres);
			FOR(i,G[p]) tres.pb(mp(me,go));
			int flip2 = S(p-1,1,other,go,me, tres);
			assert(flip1 == flip2);
			ans = flip1 + flip2 + G[p];
			return ans;
		}else{
			int norm1 = S(p-1,0,me,other,go, tres);
			FOR(i,G[p]) tres.pb(mp(me,go));
			int norm2 = S(p-1,0,other,go,me, tres);
			assert(norm1 == norm2);
			ans = norm1 + norm2 + G[p];
			return ans;
		}
	}else if (G[p] == 1){ //go normal
		if (f <= r){
			int flip1 = S(p-1,1,me,other,go, tres);
			tres.pb(mp(me,go));
			int flip2 = S(p-1,1,other,go,me, tres);
			assert(flip1 == flip2);
			//int norm = S(p-1,0,other,go,me,r3);
			ans = G[p] + flip1 + flip2;
			return ans;
		}else{
			int norm1 = S(p-1,0,me,other,go,tres);
			tres.pb(mp(me,go));
			int norm2 = S(p-1,0,other,go,me, tres);
			assert(norm1 == norm2);
			ans = G[p] + norm1 + norm2;
			return ans;
		}
	}else{
		if (f <= r){
			int flip1 = S(p-1,1,me,go,other, tres);
			FOR(i,G[p]) tres.pb(mp(me,other));
			int flip2 = S(p-1,1,go,me,other, tres);
			assert(flip1 == flip2);
			FOR(i,G[p]) tres.pb(mp(other,go));
			int norm = S(p-1,0,me,go,other,tres);
			ans = flip1 + flip2 + norm + 2*G[p];
			return ans;
		}else{
			int norm1 = S(p-1,0,me,go,other, tres);
			FOR(i,G[p]) tres.pb(mp(me,other));
			int norm2 = S(p-1,0,go,me,other, tres);
			assert(norm1 == norm2);
			FOR(i,G[p]) tres.pb(mp(other,go));
			int norm3 = S(p-1,0,me,go,other,tres);
			assert(norm1 == norm3);
			ans = norm1 + norm2 + norm3 + 2*G[p];
			return ans;
		}
	}

	throw "bad!";
}

int main()
{
	int N;
	in >> N;
	int P = 0;
	memset(G,0,sizeof(G));
	FOR(i,N) {
		in >> A[i];
		fin[1].push(A[i]);
		if (!(G[P] == 0 || A[i] == A[i-1])){ P++;}
		G[P]++;
	}
	P++;
	//cout << P << endl;
	G[P] = 0; reverse(G,G+P+1);
	A[N] = 0; reverse(A,A+N+1);

	vpii res;
	int ans = S(P,0,1,3,2,res);

	assert(ans == res.size());
	cout << ans << endl;
	FOR(i,ans){
		cout << res[i].first << " " << res[i].second << endl;
	}
	/*
	FOR(i,3){
		while (!fin[i].empty()){
			int x = fin[i].top(); fin[i].pop();
			cout << x << " ";
		}cout << endl;
	}*/
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
int cnt[21],s;
int n;
int a[21];
int rx[2010],ry[2010],ans;
void work(int a,int b,int c)
{
	for(int i=1;i<=c;i++)
	{
		ans++;
		rx[ans]=a;
		ry[ans]=b;
	}
}
void solve(int x,int a,int b,int c,int t)
{
	if(x==s)
	{
		if(cnt[x]==1 || t)work(a,c,cnt[x]);
		else
		{
			work(a,b,cnt[x]-1);
			work(a,c,1);
			work(b,c,cnt[x]-1);
		}
	}
	else if(cnt[x]==1 || t)
	{
		solve(x+1,a,c,b,1);
		work(a,c,cnt[x]);
		solve(x+1,b,a,c,1);
	}
	else
	{
		solve(x+1,a,b,c,1);
		work(a,b,cnt[x]);
		solve(x+1,c,b,a,1);
		work(b,c,cnt[x]);
		solve(x+1,a,b,c,0);
	}
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",a+i);
	for(int i=1;i<=n;i++)
	{
		if(a[i]!=a[i-1])s++;
		cnt[s]++;
	}
	solve(1,1,2,3,0);
	printf("%d\n",ans);
	for(int i=1;i<=ans;i++)printf("%d %d\n",rx[i],ry[i]);
	return 0;
}


#include "bits/stdc++.h"
#include <assert.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define PB push_back
#define MP make_pair
const int MOD=1000000007;
#define endl "\n"
#define fst first
#define snd second
const int UNDEF = -1;
const ll INF=1e18;
template<typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; }
template<typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; }
typedef pair<ll,ll> pll;typedef vector<ll> vll;typedef pair<int,int> pii;typedef vector<int> vi;typedef vector<vi> vvi;
#define DEBUG_CAT
#ifdef DEBUG_CAT
#define dbg(...)   printf( __VA_ARGS__ )
#else
#define dbg(...)   /****nothing****/
#endif
int rint();char rch();long long rlong();
const int mn=52;
int mod_pow(int a, int n, int mod) { int ret = 1; int p = a % mod; while (n) { if (n & 1) ret = (ret *(ll) p) % mod; p = (p *(ll) p) % mod; n >>= 1; } return ret; }
const int MAXFACT=52;
int fact[MAXFACT+1],invfact[MAXFACT+1];
inline int mul(int x,int y) {return (x*(ll)y)%MOD;}
void init() {
  int got=1;
  for (int x=0;x<=MAXFACT;x++) {
    fact[x]=got;
    got=mul(got,x+1);
  }
  got=mod_pow(got,MOD-2,MOD);
  for (int x=MAXFACT;x>=0;x--) {
    got=mul(got,x+1);
    invfact[x]=got;
  }
}
int dpg[mn][mn];
int h[mn][mn];
const int mc=51;
int g(int n, int f) {
  if (dpg[n][f]!=-1) {
    //printf("n:%d f:%d ans:%d\n",n,f,dpg[n][f]);
    return dpg[n][f];
  }
  int ans=0;
  for (int na=0;na<=n;na++) {
    int nb=n-na;
    for (int fa=f;fa<=mc;fa++) {
      int fb=f;
      ans=(ans + (h[na][fa]*(ll)(h[nb][fb])))%MOD;
    }
    for (int fb=f+1;fb<=mc;fb++) {
      int fa=f;
      ans=(ans + (h[na][fa]*(ll)(h[nb][fb])))%MOD;
    }
  }
  //printf("G n:%d f:%d ans:%d\n",n,f,ans);
  return dpg[n][f]=ans;
}
int binom(int n, int k) {
  if (k==0) return 1;
  if (n>k) return 0;
  int numer=n;
  for (int i=1;i<k;i++) {
    numer=mul(numer,n-i);
  }
  return mul(numer,invfact[k]);
}
int solve(int gn, int igf) {
  int gf=mc;
  memset(h,0,sizeof h);
  memset(dpg,-1,sizeof dpg);
  for (int f=0;f<mn;f++) {
    dpg[0][f]=0;
  }
  dpg[0][1]=1;
  h[0][1]=1;
  for (int nstep=1;nstep<=gn;nstep++) {
    for (int fstep=gf;fstep>=1;fstep--) {
      int klim=min((gn)/nstep, gf/fstep);
      int possible=g(nstep-1,fstep);
      //printf("nstep:%d fstep:%d possible:%d klim:%d\n",nstep,fstep,possible,klim);
      int ncost=nstep,fcost=fstep;
      int numer=possible;
      for (int k=1;k<=klim;k++) {
        int bin=mul(numer,invfact[k]);
        for (int n=gn;n>=ncost;n--) {
          for (int f=gf;f>=fcost;f--) {
            int prev=h[n-ncost][f-fcost];
            ll toadd=prev*(ll)bin;
            //printf("n:%d f:%d prev:%d bin:%d gf:%d\n",n,f,prev,bin,gf);
            h[n][f]=(h[n][f]+toadd)%MOD;
          }
        }
        ncost+=nstep;
        fcost+=fstep;
        numer=mul(numer,possible+k);
      }
    }
  }
  return h[gn][igf];
}

int main()
{
  ios_base::sync_with_stdio(false); cin.tie(0);
  init();
  //for (int flim=0;flim<mn;flim++) {
  //  for (int f=0;f<mn;f++) {
  //    dph[0][f][flim]=0;
  //  }
  //  dph[0][1][flim]=1;
  //}
  assert(solve(0,1)==1);
  assert(solve(1,1)==0);
  assert(solve(1,2)==1);
  assert(solve(2,0)==0);
  assert(solve(2,1)==0);
  assert(solve(2,2)==2);
  assert(solve(2,3)==1);
  //printf("g(2,2):%d\n",g(2,2)); return 0;
  //return 0;
  int n=rint(),m=rint();
  int ans=solve(n,m);
  printf("%d\n",ans);
}

static char stdinBuffer[1024];
static char* stdinDataEnd = stdinBuffer + sizeof (stdinBuffer);
static const char* stdinPos = stdinDataEnd;

void readAhead(size_t amount)
{
    size_t remaining = stdinDataEnd - stdinPos;
    if (remaining < amount) {
       memmove(stdinBuffer, stdinPos, remaining);
       size_t sz = fread(stdinBuffer + remaining, 1, sizeof (stdinBuffer) - remaining, stdin);
       stdinPos = stdinBuffer;
       stdinDataEnd = stdinBuffer + remaining + sz;
       if (stdinDataEnd != stdinBuffer + sizeof (stdinBuffer))
         *stdinDataEnd = 0;
    }
}

int rint()
{
    readAhead(16);

    int x = 0;
    bool neg = false;
    while(*stdinPos==' '||*stdinPos=='\n') ++stdinPos;
    if (*stdinPos == '-') {
       ++stdinPos;
       neg = true;
    }

    while (*stdinPos >= '0' && *stdinPos <= '9') {
       x *= 10;
       x += *stdinPos - '0';
       ++stdinPos;
    }

    return neg ? -x : x;
}
char rch()
{
    readAhead(16);
    while(*stdinPos==' '||*stdinPos=='\n') ++stdinPos;
    char ans=*stdinPos;
    ++stdinPos;
    return ans;
}
long long rlong()
{
    readAhead(32);

    long long x = 0;
    bool neg = false;
    while(*stdinPos==' '||*stdinPos=='\n') ++stdinPos;
    if (*stdinPos == '-') {
       ++stdinPos;
       neg = true;
    }

    while (*stdinPos >= '0' && *stdinPos <= '9') {
       x *= 10;
       x += *stdinPos - '0';
       ++stdinPos;
    }

    return neg ? -x : x;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <numeric>
#include <iterator>
#include <cstring>
using namespace std;

const int MOD = 1'000'000'007;

int n, k;
int sol[55][55];
int dp[2][55][55];
int bk[105][105];
int dabl[55][55];
int dabl_ex[55][55][55];

inline void dod(int& x, int y) {
	x += y;
	if (x >= MOD) {
		x -= MOD;
	}
}

/*
	sol[i][j] = Imas u pocetku jednu granu. Dodaj i novih
	cvorova tako da flow bude tacno j << broj nacina.

	dp[a][b][i][j] = U pocetku nemas granu. Broj nacina
	da dodas i cvorova, tako da je flow tacno j i tako da
	koristis samo dabl-komponente koje su formata *lex*<= (a, b)

	dabl[i][j] = Imas jedan cvor na sredini a sve ostalo sto si
	dodao si nakacio na jednu od ove dve grane gore. U zbiru
	imas i cvorova i protok je j
*/

int mpow(int a, int b) {
	if (b == 0) {
		return 1;
	}
	int t = mpow(a, b/2);
	if (b % 2) {
		return t * 1ll * t % MOD * a % MOD;
	} else {
		return t * 1ll * t % MOD;
	}
}

int fact[105], finv[105];
int inv[105];

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

	cin >> n >> k;

	sol[0][1] = 1;

	fact[0] = finv[0] = 1;
	for (int i=1; i<=104; i++) {
		fact[i] = fact[i-1] * 1ll * i % MOD;
		finv[i] = mpow(fact[i], MOD - 2);
		inv[i] = mpow(i, MOD-2);
	}

	for (int i=1; i<=n+1; i++) {
		for (int j=1; j<=n+1; j++) {
			dabl_ex[i][j][0] = 1;
		}
	}

	for (int i=1; i<=n; i++) {
		for (int j=1; j<=min(k, i)+1; j++) {

			if (i - j > n - k + 1) {
				continue;
			}

			// izracunaj dabl
			for (int li=0; li<i; li++) {
				int ri = i - li - 1;

				// lj = rj = j
				dod(dabl[i][j], sol[li][j] * 1ll * sol[ri][j] % MOD);

				// lj = j, rj > j
				for (int rj=j+1; rj<=ri+1; rj++) {
					dod(dabl[i][j], sol[li][j] * 1ll * sol[ri][rj] % MOD);
				}

				// lj > j, rj = j
				for (int lj=j+1; lj<=li+1; lj++) {
					dod(dabl[i][j], sol[li][lj] * 1ll * sol[ri][j] % MOD);
				}
			}

			// extenduj dabl

			{
				int run_and_gun = 1;
				for (int rep=0; rep<=n+1; rep++) {
					dabl_ex[i][j][rep] = run_and_gun;
					run_and_gun = run_and_gun * 1ll * (dabl[i][j] + rep) % MOD;
					run_and_gun = run_and_gun * 1ll * inv[rep+1] % MOD;
				}
			}

			// cerr << "dabl " << i << ' ' << j << ": " << dabl[i][j] << '\n';

			// dodaj jedno po jedno
			auto prev = dp[0];
			auto curr = dp[1];

			memset(dp, 0, sizeof(dp));

			prev[0][0] = 1;

			for (int a=1; a<=i; a++) {
				for (int b=1; b<=min(j, (a+1)/2); b++) {

					memset(curr, 0, sizeof(dp[0]));
					curr[0][0] = 1;

					for (int rep=0; rep<=i; rep++) {

						int dex = dabl_ex[a][b][rep];

						for (int c=max(1, a*rep); c<=i; c++) {
							for (int d=max(1, b*rep); d<=min(c, j); d++) {

								dod(curr[c][d], prev[c - a*rep][d - b*rep] *
									1ll * dex % MOD);
							}
						}
					}

					swap(prev, curr);
				}
			}

			// cerr << "chk  " << dp[3][1][3][1] << '\n';

			sol[i][j] = prev[i][j-1];

			// cerr << "sol  " << i << ' ' << j << ": " << sol[i][j] << '\n';
		}
	}

	cout << sol[n][k] << '\n';

}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 52, mod = 1000000007;
typedef long long LL;
int N, M, F[maxn][maxn], G[maxn][maxn], inv[maxn];

void init()
{
    scanf("%d%d", &N, &M);
    inv[1] = 1;
    for(int i = 2;i < maxn;++i)
        inv[i] = LL(mod-mod/i) * inv[mod%i] % mod;
}
void work()
{
    F[0][1] = 1;
    for(int i = 1;i <= N;++i) for(int j = 1;j < maxn;++j)
    {
        for(int a = 0;a < i;++a)
        {
            G[i][j] = (G[i][j] + (LL) F[a][j] * F[i-1-a][j]) % mod;
            for(int b = j+1;b <= i+1 && b < maxn;++b)
            {
                G[i][j] = (G[i][j] + (LL) F[a][b] * F[i-1-a][j]) % mod;
                G[i][j] = (G[i][j] + (LL) F[a][j] * F[i-1-a][b]) % mod;
            }
        }
        //get G[i][j]
        for(int x = N-1;x >= 0;--x) for(int y = 1;y < maxn;++y) if(F[x][y])
        {
            int C = 1;
            for(int t = 1;x+t*i <= N && y+t*j < maxn;++t)
            {
                C = (LL) C * (G[i][j]-1+t) % mod * inv[t] % mod;
                F[x+t*i][y+t*j] = (F[x+t*i][y+t*j] + (LL) F[x][y] * C) % mod;
            }
        }
    }
    printf("%d\n", F[N][M]);
}
int main()
{
    init();
    work();
    return 0;
}

动态规划


By Asm.Def, contest: Codeforces Round #431 (Div. 1), problem: (D) Shake It!, Accepted, #

#include <bits/stdc++.h>
using namespace std;
const int maxn = 52, mod = 1000000007;
typedef long long LL;
int N, M, F[maxn][maxn], G[maxn][maxn], inv[maxn];

void init()
{
    scanf("%d%d", &N, &M);
    inv[1] = 1;
    for(int i = 2;i < maxn;++i)
        inv[i] = LL(mod-mod/i) * inv[mod%i] % mod;
}
void work()
{
    F[0][1] = 1;
    for(int i = 1;i <= N;++i) for(int j = 1;j < maxn;++j)
    {
        for(int a = 0;a < i;++a)
        {
            G[i][j] = (G[i][j] + (LL) F[a][j] * F[i-1-a][j]) % mod;
            for(int b = j+1;b <= i+1 && b < maxn;++b)
            {
                G[i][j] = (G[i][j] + (LL) F[a][b] * F[i-1-a][j]) % mod;
                G[i][j] = (G[i][j] + (LL) F[a][j] * F[i-1-a][b]) % mod;
            }
        }
        //get G[i][j]
        for(int x = N-1;x >= 0;--x) for(int y = 1;y < maxn;++y) if(F[x][y])
        {
            int C = 1;
            for(int t = 1;x+t*i <= N && y+t*j < maxn;++t)
            {
                C = (LL) C * (G[i][j]-1+t) % mod * inv[t] % mod;
                F[x+t*i][y+t*j] = (F[x+t*i][y+t*j] + (LL) F[x][y] * C) % mod;
            }
        }
    }
    printf("%d\n", F[N][M]);
}
int main()
{
    init();
    work();
    return 0;
}

动态规划


/** ` Micro Mezzo Macro Flation -- Overheated Economy ., **/

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>

using namespace std;

#define REP(i, n) for (int i=0;i<int(n);++i)
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
#define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)
#define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)
#define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)
#define REP_N(i, n) for (i=0;i<int(n);++i)
#define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
#define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
#define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)
#define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)
#define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)
#define REP_1_N(i, n) for (i=1;i<=int(n);++i)
#define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
#define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
#define REP_C_N(i, n) for (n____=int(n),i=0;i<n____;++i)
#define FOR_C_N(i, a, b) for (b____=int(b),i=a;i<b____;++i)
#define DWN_C_N(i, b, a) for (a____=int(a),i=b-1;i>=a____;--i)
#define REP_1_C_N(i, n) for (n____=int(n),i=1;i<=n____;++i)
#define FOR_1_C_N(i, a, b) for (b____=int(b),i=a;i<=b____;++i)
#define DWN_1_C_N(i, b, a) for (a____=int(a),i=b;i>=a____;--i)

#define DO(n) while(n--)
#define DO_C(n) int n____ = n; while(n____--)
#define TO(i, a, b) int s_=a<b?1:-1,b_=b+s_;for(int i=a;i!=b_;i+=s_)
#define TO_1(i, a, b) int s_=a<b?1:-1,b_=b;for(int i=a;i!=b_;i+=s_)
#define SQZ(i, j, a, b) for (int i=int(a),j=int(b)-1;i<j;++i,--j)
#define SQZ_1(i, j, a, b) for (int i=int(a),j=int(b);i<=j;++i,--j)
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)

#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define BSC(A, X) find(ALL(A), X) // != A.end()
#define CTN(T, x) T.find(x) != T.end()
#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)

#define Rush int T____; RD(T____); DO(T____)
#pragma comment(linker, "/STACK:36777216")
#pragma GCC optimize ("O2")
#define Ruby system("ruby main.rb")
#define Haskell system("runghc main.hs")
#define Pascal system("fpc main.pas")

typedef long long LL;
typedef double DB;
typedef unsigned UINT;
typedef unsigned long long ULL;

typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VD;
typedef set<int> SI;
typedef set<string> SS;
typedef set<LL> SL;
typedef set<DB> SD;
typedef map<int, int> MII;
typedef map<string, int> MSI;
typedef map<LL, int> MLI;
typedef map<DB, int> MDI;
typedef map<int, bool> MIB;
typedef map<string, bool> MSB;
typedef map<LL, bool> MLB;
typedef map<DB, bool> MDB;
typedef pair<int, int> PII;
typedef pair<int, bool> PIB;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;
typedef set<PII> SII;
typedef map<PII, int> MPIII;
typedef map<PII, bool> MPIIB;

/** I/O Accelerator **/

/* ... :" We are I/O Accelerator ... Use us at your own risk ;) ... " .. */

template<class T> inline void RD(T &);
template<class T> inline void OT(const T &);

template<class T> inline T& _RD(T &x){ RD(x); return x;}
inline int RD(){ int x; RD(x); return x;}
inline LL RD_LL(){ LL x; RD(x); return x;}
inline DB RD_DB(){ DB x; RD(x); return x;}

template<class T0, class T1> inline void RD(T0 &x0, T1 &x1){RD(x0), RD(x1);}
template<class T0, class T1, class T2> inline void RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2);}
template<class T0, class T1, class T2, class T3> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3){RD(x0), RD(x1), RD(x2), RD(x3);}
template<class T0, class T1, class T2, class T3, class T4> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);}
template<class T0, class T1> inline void OT(T0 &x0, T1 &x1){OT(x0), OT(x1);}
template<class T0, class T1, class T2> inline void OT(T0 &x0, T1 &x1, T2 &x2){OT(x0), OT(x1), OT(x2);}
template<class T0, class T1, class T2, class T3> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3){OT(x0), OT(x1), OT(x2), OT(x3);}
template<class T0, class T1, class T2, class T3, class T4> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}

template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}
template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}
template<class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3){RST(A0), RST(A1), RST(A2), RST(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}
template<class T> inline void CLR(T &A){A.clear();}
template<class T0, class T1> inline void CLR(T0 &A0, T1 &A1){CLR(A0), CLR(A1);}
template<class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2){CLR(A0), CLR(A1), CLR(A2);}
template<class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3){CLR(A0), CLR(A1), CLR(A2), CLR(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);}
template<class T> inline void CLR(T &A, int n){REP(i, n) CLR(A[i]);}
template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}
template<class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x){FLC(A0, x), FLC(A1, x);}
template<class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2){FLC(A0), FLC(A1), FLC(A2);}
template<class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3){FLC(A0), FLC(A1), FLC(A2), FLC(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5), FLC(A6);}

template<class T> inline void SRT(T &A){sort(ALL(A));}
template<class T, class C> inline void SRT(T &A, C B){sort(ALL(A), B);}

/** Add - On **/

const int MOD = 1000000007;
const int INF = 0x7fffffff;
const DB PI = acos(-1.0);
const DB EPS = 1e-6;
const DB OO = 1e15;

// <<= ` 0. Daily Use .,

template<class T> inline void checkMin(T &a,const T b){if (b<a) a=b;}
template<class T> inline void checkMax(T &a,const T b){if (b>a) a=b;}
template <class T, class C> inline void checkMin(T& a, const T b, C c){if (c(b,a)) a = b;}
template <class T, class C> inline void checkMax(T& a, const T b, C c){if (c(a,b)) a = b;}
template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T cub(T a){return a*a*a;}
int Ceil(int x, int y){return (x - 1) / y + 1;}

// <<= ` 1. Bitwise Operation .,

inline bool _1(int x, int i){return x & 1<<i;}
inline int _1(int i){return 1<<i;}
inline int _U(int i){return _1(i) - 1;};

inline int count_bits(int x){
    x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
    x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
    x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);
    x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8);
    x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16);
    return x;
}

template<class T> inline T low_bit(T x) {
    return x & -x;
}

template<class T> inline T high_bit(T x) {
    T p = low_bit(x);
    while (p != x) x -= p, p = low_bit(x);
    return p;
}

// <<= ` 2. Modular Arithmetic Basic .,

inline void INC(int &a, int b){a += b; if (a >= MOD) a -= MOD;}
inline int sum(int a, int b){a += b; if (a >= MOD) a -= MOD; return a;}
inline void DEC(int &a, int b){a -= b; if (a < 0) a += MOD;}
inline int dff(int a, int b){a -= b; if (a < 0) a  += MOD; return a;}
inline void MUL(int &a, int b){a = (LL)a * b % MOD;}
inline int pdt(int a, int b){return (LL)a * b % MOD;}

// <<= ' 9. Computational Geometry .,

// <<= ' 0. I/O Accelerator interface .,

template<class T> inline void RD(T &x){
    cin >> x;
    //scanf("%d", &x);
    //char c; for (c = getchar(); c < '0'; c = getchar()); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';
    //char c; c = getchar(); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';

}

int ____Case;
template<class T> inline void OT(const T &x){
    cout << x << endl;
    //printf("%d\n", x);
    //printf("%.9lf\n", x);
    //printf("Case %d: %d\n", ++____Case, x);
}

#define For_each(it, A) for (SII::iterator it = A.begin(); it != A.end(); ++it)
#define Foreach(it, A ) for(__typeof(A.begin()) it =A.begin(); it!=A.end(); ++it)

/* .................................................................................................................................. */

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int,double> PIF;

int sgn(DB x){
    return x < -EPS ? -1 : x > EPS;
}

struct Po{
    DB x, y;

    Po(){}
    Po(DB _x, DB _y):x(_x),y(_y){}

    bool operator <(const Po& rhs) const {
        return sgn(x - rhs.x) < 0 || !sgn(x - rhs.x) && sgn(y - rhs.y) < 0;
    }

    bool operator ==(const Po& rhs) const {
        return !sgn(x - rhs.x) && !sgn(y - rhs.y);
    }

    bool operator !=(const Po& rhs) const {
        return !(*this == rhs);
    }
    void input(){
        scanf("%lf%lf", &x, &y);
    }
};

struct line{
    Po x, y;

    line(){}
    line(Po _x,Po _y):x(_x),y(_y){}

    void input(){x.input(), y.input();}
};

struct node{

    string c;vector<int> a;double sz;
    node(string _c,double _sz=0):c(_c),sz(_sz){}
    void add(int x){a.pb(x);}
};

    Po med(Po a,Po b,Po c)
    {

        return __median(a, b, c);

     //       if(a<b)if(b<c)return b;else if(a<c)return c;else return a;
       //     else if(a<c)return a;else if(b<c)return c;else return b;
    }
    int getcrs(line a,line b,Po&c)
    {
            double A=a.y.y-a.x.y,B=a.x.x-a.y.x,C=a.x.x*A+a.x.y*B;
            double D=b.y.y-b.x.y,E=b.x.x-b.y.x,F=b.x.x*D+b.x.y*E;
            if(fabs(A*E-B*D)<EPS)return -1;
            c=Po((C*E-F*B)/(A*E-D*B),(C*D-F*A)/(B*D-E*A));
            if(med(a.x,a.y,c)!=c||med(b.x,b.y,c)!=c)return -1;
            return 0;
    }
    bool online(Po p,line a)
    {
            if(med(a.x,a.y,p)!=p)return 0;
            double A=a.y.y-a.x.y,B=a.x.x-a.y.x,C=a.x.x*A+a.x.y*B;
            return abs(A*p.x+B*p.y-C)<EPS;
    }
    double ang(Po a,Po b)
    {
            double S=atan2(b.y-a.y,b.x-a.x)*180/M_PI;
            if(S<EPS)S+=360;
            return S;
    }
    bool cmpid(const PIF&a,const PIF&b){return a.fi<b.fi;}
    bool equid(const PIF&a,const PIF&b){return a.fi==b.fi;}
    bool cmpang(const PIF&a,const PIF&b){return a.se<b.se;}
    double getsz(vector<Po> p)
    {
            double S=0;
            for(int i=0;i<(int)p.size();i++)
            {
                    int j=(i+1)%(int)p.size();
                    S+=p[i].x*p[j].y-p[j].x*p[i].y;
            }
            return -S/2;
    }
    double crs(Po a,Po b,Po c)
    {
            return (b.y-a.y)*(c.x-a.x)-(b.x-a.x)*(c.y-a.y);
    }
    int inpoly(Po p,vector<Po> a)
    {
            int n=(int)a.size();a.pb(a[0]);
            for(int i=0;i<n;i++)
                    if(online(p,line(a[i],a[i+1])))return 0;
            int w=0;
            for(int i=0;i<n;i++)
                    if(a[i].y<=p.y&&a[i+1].y>p.y&&crs(a[i],a[i+1],p)<0)++w;else
                    if(a[i].y>p.y&&a[i+1].y<=p.y&&crs(a[i],a[i+1],p)>0)--w;
            return w;
    }
    int w,h,n,m,p;

    vector<Po> po;
    vector<vector<PIF> > pa;
    vector<node> gr;
    vector<int> bl;
    vector<Po> fl;
    vector<vector<Po> > pl;

    void bfs(int x,string c)
    {
            string d=gr[x].c;
            queue<int> q;vector<bool> v((int)gr.size(),0);
            v[x]=1,q.push(x);
            while(!q.empty())
            {
                    int x=q.front();q.pop();
                    gr[x].c=c;
                    for(int i=0;i<(int)gr[x].a.size();i++)
                            if(!v[gr[x].a[i]]&&gr[gr[x].a[i]].c==d)
                                    q.push(gr[x].a[i]),v[gr[x].a[i]]=1;
            }
    }

int main(){
            //scanf("%d%d%d",&w,&h,&n);

            RD(w, h, n);

            vector<line> l0(n);

            REP(i, n) l0[i].input();

            //for(int i=0;i<n;i++)l0[i].get();
            for(int i=0;i<n;i++)po.pb(l0[i].x),po.pb(l0[i].y);
            for(int i=0;i<n;i++)
                    for(int j=i+1;j<n;j++)
                    {
                            Po p;
                            if(getcrs(l0[i],l0[j],p)!=-1)po.pb(p);
                    }
            sort(po.begin(),po.end()),
            po.erase(unique(po.begin(),po.end()),po.end());
            m=(int)po.size();
            pa.resize(m);

            for(int i=0;i<n;i++)
            {
                    vector<int> vp;
                    for(int j=0;j<m;j++)
                            if(online(po[j],l0[i]))vp.pb(j);
                    for(int j=1;j<(int)vp.size();j++)
                            pa[vp[j-1]].pb(mp(vp[j],ang(po[vp[j-1]],po[vp[j]]))),
                            pa[vp[j]].pb(mp(vp[j-1],ang(po[vp[j]],po[vp[j-1]])));
            }

            for(int i=0;i<m;i++)
                    sort(pa[i].begin(),pa[i].end(),cmpid),
                    pa[i].erase(unique(pa[i].begin(),pa[i].end(),equid),pa[i].end()),
                    sort(pa[i].begin(),pa[i].end(),cmpang);
            gr.pb(node("white",w*h));
            fl.pb(Po(0.0,0.0));
            bl.resize(m,-1);
            for(int i=0;i<m;i++)if(bl[i]==-1)
            {
                    queue<int> q;
                    q.push(i);
                    gr.pb(node("black",0.0));
                    bl[i]=++p,fl.pb(po[i]);
                    while(!q.empty())
                    {
                            int x=q.front();q.pop();
                            for(int j=0;j<(int)pa[x].size();j++)
                                    if(bl[pa[x][j].fi]==-1)
                                            q.push(pa[x][j].fi),
                                            bl[pa[x][j].fi]=p;
                    }
            }
            pl.resize(p+1);
            vector<vector<bool> > chk(m);
            for(int i=0;i<m;i++)chk[i].resize((int)pa[i].size(),0);
            for(int i=0;i<m;i++)
                    for(int j=0;j<(int)pa[i].size();j++)
                    {
                            if(chk[i][j])continue;
                            vector<Po> vp;
                            int i0=i,j0=j;
                            while(!chk[i0][j0])
                            {
                                    vp.pb(po[i0]);
                                    chk[i0][j0]=1;
                                    int i1=pa[i0][j0].fi,j1=-1;
                                    for(int j=0;j<(int)pa[i1].size();j++)
                                            if(pa[i1][j].fi==i0){j1=(j+1)%(int)pa[i1].size();break;}
                                    i0=i1,j0=j1;
                            }
                            double sz=getsz(vp);
                            if(sz>0)
                            {
                                    pl.pb(vp);
                                    gr.pb(node("white",sz));
                                    int r=bl[i];
                                    gr[r].add((int)gr.size()-1),
                                    gr[(int)gr.size()-1].add(r);
                                    gr[r].sz-=sz;
                            }
                    }
            for(int i=1;i<=p;i++)
            {
                    int fa=0;
                    for(int j=(int)pl.size()-1;j>p;j--)
                            if(inpoly(fl[i],pl[j])){fa=j;break;}
                    gr[fa].add(i),gr[i].add(fa),gr[fa].sz+=gr[i].sz,gr[i].sz=0;
            }

        int _;scanf("%d",&_);
        while(_--){

            Po x; x.input();string c;char s[100];scanf("%s",s),c=s;

            for(int i=0;i<m;i++)
                for(int j=0;j<(int)pa[i].size();j++)

            if(online(x,line(po[i],po[pa[i][j].fi]))){bfs(bl[i],c);goto end;}
            for(int i=(int)pl.size()-1;i>p;i--)
                if(inpoly(x,pl[i])){bfs(i,c);goto end;}
            bfs(0,c);
            end:;
        }

        map<string,double> S;
        for(int i=0;i<(int)gr.size();i++)
            S[gr[i].c]+=gr[i].sz;
        //for(map<string,double>::iterator i=S.begin();i!=S.end();i++)

    Foreach(it, S){
        printf("%s %.9lf\n", it->first.c_str(), it->second);
    }
    return 0;
}


/** Micro Mezzo Macro Flation -- Overheated Economy ., Last Update: Dec. 24th 2012 **/ //{

/** Header .. **/ //{
#define LOCAL

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>

using namespace std;

#define REP(i, n) for (int i=0;i<int(n);++i)
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
#define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)
#define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)
#define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)
#define REP_N(i, n) for (i=0;i<int(n);++i)
#define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
#define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
#define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)
#define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)
#define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)
#define REP_1_N(i, n) for (i=1;i<=int(n);++i)
#define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
#define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
#define REP_C_N(i, n) for (int n____=(i=0,int(n));i<n____;++i)
#define FOR_C_N(i, a, b) for (int b____=(i=0,int(b);i<b____;++i)
#define DWN_C_N(i, b, a) for (int a____=(i=b-1,int(a));i>=a____;--i)
#define REP_1_C_N(i, n) for (int n____=(i=1,int(n));i<=n____;++i)
#define FOR_1_C_N(i, a, b) for (int b____=(i=1,int(b);i<=b____;++i)
#define DWN_1_C_N(i, b, a) for (int a____=(i=b,int(a));i>=a____;--i)

#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)
#define REP_S(i, str) for (char*i=str;*i;++i)
#define REP_L(i, hd, nxt) for (int i=hd;i;i=nxt[i])
#define REP_G(i, u) REP_L(i,hd[u],suc)
#define DO(n) for ( int ____n ## __line__ = n; ____n ## __line__ -- ; )
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)

#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define BSC(A, x) (lower_bound(ALL(A), x) - A.begin())
#define CTN(T, x) (T.find(x) != T.end())
#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define PTT pair<T, T>
#define fi first
#define se second

#define Rush for(int ____T=RD(); ____T--;)

#define Display(A, n, m) {                      \
	REP(i, n){		                            \
        REP(j, m) cout << A[i][j] << " ";       \
        cout << endl;				            \
	}						                    \
}

#define Display_1(A, n, m) {				    \
	REP_1(i, n){		                        \
        REP_1(j, m) cout << A[i][j] << " ";     \
		cout << endl;		            		\
	}						                    \
}

#pragma comment(linker, "/STACK:36777216")
//#pragma GCC optimize ("O2")
#define Ruby system("ruby main.rb")
#define Haskell system("runghc main.hs")
#define Python system("python main.py")
#define Pascal system("fpc main.pas")

typedef long long LL;
//typedef long double DB;
typedef double DB;
typedef unsigned UINT;
typedef unsigned long long ULL;

typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VF;
typedef set<int> SI;
typedef set<string> SS;
typedef map<int, int> MII;
typedef map<string, int> MSI;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;

template<class T> inline T& RD(T &);
template<class T> inline void OT(const T &);
inline LL RD(){LL x; return RD(x);}
inline DB& RF(DB &);
inline DB RF(){DB x; return RF(x);}
inline char* RS(char *s);
inline char& RC(char &c);
inline char RC();
inline char& RC(char &c){scanf(" %c", &c); return c;}
inline char RC(){char c; return RC(c);}
//inline char& RC(char &c){c = getchar(); return c;}
//inline char RC(){return getchar();}

template<class T> inline T& RDD(T &x){
    char c; for (c = getchar(); c < '-'; c = getchar());
    if (c == '-'){x = '0' - getchar(); for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + '0' - c;}
    else {x = c - '0'; for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';}
    return x;
}

inline LL RDD(){LL x; return RDD(x);}

template<class T0, class T1> inline T0& RD(T0 &x0, T1 &x1){RD(x0), RD(x1); return x0;}
template<class T0, class T1, class T2> inline T0& RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2); return x0;}
template<class T0, class T1, class T2, class T3> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3){RD(x0), RD(x1), RD(x2), RD(x3); return x0;}
template<class T0, class T1, class T2, class T3, class T4> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); return x0;}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); return x0;}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); return x0;}
template<class T0, class T1> inline void OT(const T0 &x0, const T1 &x1){OT(x0), OT(x1);}
template<class T0, class T1, class T2> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2){OT(x0), OT(x1), OT(x2);}
template<class T0, class T1, class T2, class T3> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3){OT(x0), OT(x1), OT(x2), OT(x3);}
template<class T0, class T1, class T2, class T3, class T4> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5, const T6 &x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}
inline char& RC(char &a, char &b){RC(a), RC(b); return a;}
inline char& RC(char &a, char &b, char &c){RC(a), RC(b), RC(c); return a;}
inline char& RC(char &a, char &b, char &c, char &d){RC(a), RC(b), RC(c), RC(d); return a;}
inline char& RC(char &a, char &b, char &c, char &d, char &e){RC(a), RC(b), RC(c), RC(d), RC(e); return a;}
inline char& RC(char &a, char &b, char &c, char &d, char &e, char &f){RC(a), RC(b), RC(c), RC(d), RC(e), RC(f); return a;}
inline char& RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g){RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g); return a;}
inline DB& RF(DB &a, DB &b){RF(a), RF(b); return a;}
inline DB& RF(DB &a, DB &b, DB &c){RF(a), RF(b), RF(c); return a;}
inline DB& RF(DB &a, DB &b, DB &c, DB &d){RF(a), RF(b), RF(c), RF(d); return a;}
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e){RF(a), RF(b), RF(c), RF(d), RF(e); return a;}
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f){RF(a), RF(b), RF(c), RF(d), RF(e), RF(f); return a;}
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f, DB &g){RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g); return a;}
inline void RS(char *s1, char *s2){RS(s1), RS(s2);}
inline void RS(char *s1, char *s2, char *s3){RS(s1), RS(s2), RS(s3);}
template<class T0,class T1>inline void RDD(const T0&a, const T1&b){RDD(a),RDD(b);}
template<class T0,class T1,class T2>inline void RDD(const T0&a, const T1&b, const T2&c){RDD(a),RDD(b),RDD(c);}

template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}
template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}
template<class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3){RST(A0), RST(A1), RST(A2), RST(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}
template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}
template<class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x){FLC(A0, x), FLC(A1, x);}
template<class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x);}
template<class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);}
template<class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x), FLC(A6, x);}
template<class T> inline void CLR(priority_queue<T, vector<T>, less<T> > &Q){while (!Q.empty()) Q.pop();}
template<class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q){while (!Q.empty()) Q.pop();}
template<class T> inline void CLR(T &A){A.clear();}
template<class T0, class T1> inline void CLR(T0 &A0, T1 &A1){CLR(A0), CLR(A1);}
template<class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2){CLR(A0), CLR(A1), CLR(A2);}
template<class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3){CLR(A0), CLR(A1), CLR(A2), CLR(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);}
template<class T> inline void CLR(T &A, int n){REP(i, n) CLR(A[i]);}

template<class T> inline T& SRT(T &A){sort(ALL(A)); return A;}
template<class T, class C> inline T& SRT(T &A, C B){sort(ALL(A), B); return A;}
template<class T> inline T& UNQ(T &A){A.resize(unique(ALL(SRT(A)))-A.begin());return A;}

//}

/** Constant List .. **/ //{

const int MOD = 1000000007;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const LL INFF = 1LL << 60;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

//}

/** Add On .. **/ //{
// <<= '0. Nichi Joo ., //{
template<class T> inline void checkMin(T &a,const T b){if (b<a) a=b;}
template<class T> inline void checkMax(T &a,const T b){if (a<b) a=b;}
template<class T> inline void checkMin(T &a, T &b, const T x){checkMin(a, x), checkMin(b, x);}
template<class T> inline void checkMax(T &a, T &b, const T x){checkMax(a, x), checkMax(b, x);}
template <class T, class C> inline void checkMin(T& a, const T b, C c){if (c(b,a)) a = b;}
template <class T, class C> inline void checkMax(T& a, const T b, C c){if (c(a,b)) a = b;}
template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}
template<class T> inline T max(T a, T b, T c, T d){return max(max(a, b), max(c, d));}
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T cub(T a){return a*a*a;}
inline int ceil(int x, int y){return (x - 1) / y + 1;}
inline int sgn(DB x){return x < -EPS ? -1 : x > EPS;}
inline int sgn(DB x, DB y){return sgn(x - y);}
//}
// <<= '1. Bitwise Operation ., //{
namespace BO{

inline bool _1(int x, int i){return bool(x&1<<i);}
inline bool _1(LL x, int i){return bool(x&1LL<<i);}
inline LL _1(int i){return 1LL<<i;}
inline LL _U(int i){return _1(i) - 1;};

inline int reverse_bits(int x){
    x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);
    x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
    x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);
    x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
    x = ((x >>16) & 0x0000ffff) | ((x <<16) & 0xffff0000);
    return x;
}

inline LL reverse_bits(LL x){
    x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL);
    x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL);
    x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL);
    x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL);
    x = ((x >>16) & 0x0000ffff0000ffffLL) | ((x <<16) & 0xffff0000ffff0000LL);
    x = ((x >>32) & 0x00000000ffffffffLL) | ((x <<32) & 0xffffffff00000000LL);
    return x;
}

template<class T> inline bool odd(T x){return x&1;}
template<class T> inline bool even(T x){return !odd(x);}
template<class T> inline T low_bit(T x) {return x & -x;}
template<class T> inline T high_bit(T x) {T p = low_bit(x);while (p != x) x -= p, p = low_bit(x);return p;}
template<class T> inline T cover_bit(T x){T p = 1; while (p < x) p <<= 1;return p;}

inline int low_idx(int x){return __builtin_ffs(x);}
inline int low_idx(LL x){return __builtin_ffsll(x);}
inline int high_idx(int x){return low_idx(reverse_bits(x));}
inline int high_idx(LL x){return low_idx(reverse_bits(x));}
inline int clz(int x){return __builtin_clz(x);}
inline int clz(LL x){return __builtin_clzll(x);}
inline int ctz(int x){return __builtin_ctz(x);}
inline int ctz(LL x){return __builtin_ctzll(x);}
inline int parity(int x){return __builtin_parity(x);}
inline int parity(LL x){return __builtin_parityll(x);}
inline int lg2(int a){return 31 - clz(a);}
inline int lg2(LL a){return 63 - clz(a);}
inline int count_bits(int x){return __builtin_popcount(x);}
inline int count_bits(LL x){return __builtin_popcountll(x);}

} using namespace BO;//}
// <<= '2. Number Theory .,//{
namespace NT{
inline LL __lcm(LL a, LL b){return a*b/__gcd(a,b);}
inline void INC(int &a, int b){a += b; if (a >= MOD) a -= MOD;}
inline int sum(int a, int b){a += b; if (a >= MOD) a -= MOD; return a;}
inline void DEC(int &a, int b){a -= b; if (a < 0) a += MOD;}
inline int dff(int a, int b){a -= b; if (a < 0) a  += MOD; return a;}
inline void MUL(int &a, int b){a = (LL)a * b % MOD;}
inline int pdt(int a, int b){return (LL)a * b % MOD;}

inline int sum(int a, int b, int c){return sum(sum(a, b), c);}
inline int sum(int a, int b, int c, int d){return sum(sum(a, b), sum(c, d));}
inline int pdt(int a, int b, int c){return pdt(pdt(a, b), c);}
inline int pdt(int a, int b, int c, int d){return pdt(pdt(pdt(a, b), c), d);}

inline int pow(int a, int b){
    int c(1); while (b){
        if (b&1) MUL(c, a);
        MUL(a, a), b >>= 1;
    }
    return c;
}

inline int pow(int a, LL b){
    int c(1); while (b){
        if (b&1) MUL(c, a);
        MUL(a, a), b >>= 1;
    }
    return c;
}

template<class T> inline T pow(T a, LL b){
    T c(1); while (b){
        if (b&1) c *= a;
        a *= a, b >>= 1;
    }
    return c;
}

inline int _I(int b){
    int a = MOD, x1 = 0, x2 = 1, q;
    while (true){
        q = a / b, a %= b;
        if (!a) return (x2 + MOD) % MOD;
        DEC(x1, pdt(q, x2));

        q = b / a, b %= a;
        if (!b) return (x1 + MOD) % MOD;
        DEC(x2, pdt(q, x1));
    }
}

inline void DIV(int &a, int b){MUL(a, _I(b));}
inline int qtt(int a, int b){return pdt(a, _I(b));}

inline int phi(int n){
    int res = n; for (int i=2;sqr(i)<=n;++i) if (!(n%i)){
        DEC(res, qtt(res, i));
        do{n /= i;} while(!(n%i));
    }
    if (n != 1)
        DEC(res, qtt(res, n));
    return res;
}

} using namespace NT;//}

namespace SL{
    namespace KMP{

        void calc_pi(const char *P, int n, int *pi){
            for (int i = 1, j = pi[0] = -1; i < n; ++i){
                while (j >= 0 && P[i] != P[j+1]) j = pi[j];
                if (P[i] == P[j+1]) ++j;
                pi[i] = j;
            }
            //REP(i, n) cout << pi[i] << " "; cout << endl;
        }

        bool run(const char *T, int n, const char *P, int m, const int *pi){
            for (int i = 0, j = -1; i < n; ++i){
                while (j >= 0 && T[i] != P[j+1]) j = pi[j];
                if (T[i] == P[j+1]) ++j;
                if (j == m - 1) return true;
            }
            return false;
        }

    } //using namespace KMP;

    namespace Z{
        void calc_z(const char *P, int n, int *z){

            z[0] = n;

            for (int i = 1, l = 0, r = 0; i < n; ++i){
                if (i > r){
                    for(l = r = i; r < n && P[r] == P[r - l];) ++r;
                    z[i] = r - l, --r;
                }
                else {
                    if (z[i - l] < r - i + 1) z[i] = z[i - l];
                    else {
                        for (l = i;r < n && P[r] == P[r - l];) ++r;
                        z[i] = r - l, --r;
                    }
                }
            }

            //REP(i, n) cout << z[i] << " "; cout << endl;
        }

        int run(const char *T, int n, const char *P, int m, const int *z){

            int ex; REP_C_N(ex, min(n, m)) if (T[ex] != P[ex]) break;

            int res = ex == m;

            for (int i = 1, l = 0, r = 0; i < n; ++i){
                if (i > r){
                    for (l = r = i; r < n && T[r] == P[r - l];) ++r;
                    ex = r - l, --r;
                }
                else {
                    if (z[i - l] < r - i + 1) ex = z[i - l];
                    else {
                        for (l = i; r < n && T[r] == P[r - l];) ++r;
                        ex = r - l, --r;
                    }
                }
                if (ex == m) ++res;
            }

            return res;
        }
    } //using namespace Z;

    void Manacher(char s[], int n, int p[]){
        const int NN = 0;
        static char ss[NN*2+2]; int nn = 2*n+2;
        ss[0] = '$', ss[nn-1] = '#', ss[nn] = 0;
        REP(i, n) ss[i*2+1] ='#', ss[i*2+2] = s[i];
        int mx = 0, id = 0; FOR(i, 1, nn){
            p[i] = mx > i ? min(p[2*id-i], mx - i) : 1;
            while (ss[i+p[i]] == ss[i-p[i]]) ++p[i];
            if (i + p[i] > mx) mx = i + p[i], id = i;
        }
    }

} //using namespace SL;//}
// <<= '9. Comutational Geometry .,//{
namespace CG{

struct Po; struct Line; struct Seg;

struct Po{
    DB x, y; Po(DB _x=0, DB _y=0):x(_x), y(_y){}
    friend istream& operator >>(istream& in, Po &p){return in >> p.x >> p.y;}
    friend ostream& operator <<(ostream& out, Po p){return out << "(" << p.x << ", " << p.y << ")";}
    bool operator ==(const Po& r)const{return !sgn(x-r.x) && !sgn(y-r.y);};
    bool operator !=(const Po& r)const{return sgn(x-r.x) || sgn(y-r.y);}
    Po operator +(const Po& r)const{return Po(x+r.x, y+r.y);}
    Po operator -(const Po& r)const{return Po(x-r.x, y-r.y);}
    Po operator *(DB k)const{return Po(x*k,y*k);}
    Po operator /(DB k)const{return Po(x/k,y/k);}
    DB operator *(const Po&) const;
    DB operator ^(const Po&) const;

    bool operator <(const Po &r) const{return sgn(x,r.x)<0||!sgn(x,r.x)&&sgn(y,r.y)<0;}
    Po operator -()const{return Po(-x,-y);}
    Po& operator +=(const Po &r){x+=r.x,y+=r.y;return *this;}
    Po& operator -=(const Po &r){x-=r.x,y-=r.y;return *this;}
    Po& operator *=(DB k){x*=k,y*=k;return*this;}
    Po& operator /=(DB k){x/=k,y/=k;return*this;}

    DB length_sqr()const{return sqr(x)+sqr(y);}
    DB length()const{return sqrt(length_sqr());}
    Po unit()const{return *this/length();}
    bool dgt()const{return !sgn(x)&&!sgn(y);}
    DB atan()const{return atan2(y,x);}
    void rotate(DB alpha, const Po& o = Po()){
        x -= o.x, y -= o.y;
        (*this) = Po(x * cos(alpha) - y * sin(alpha), y * cos(alpha) + x * sin(alpha)) + o;
    }

    void input(){RF(x,y);}
};

Po operator *(DB k, Po a){return a * k;}

#define innerProduct dot
#define scalarProduct dot
#define outerProduct det
#define crossProduct det

inline DB dot(const DB &x1, const DB &y1, const DB &x2, const DB &y2){return x1 * x2 + y1 * y2;}
inline DB dot(const Po &a, const Po &b){return dot(a.x, a.y, b.x, b.y);}
inline DB dot(const Po &p0, const Po &p1, const Po &p2){return dot(p1 - p0, p2 - p0);}

inline DB det(const DB &x1, const DB &y1, const DB &x2, const DB &y2){return x1 * y2 - x2 * y1;}
inline DB det(const Po &a, const Po &b){return det(a.x, a.y, b.x, b.y);}
inline DB det(const Po &p0, const Po &p1, const Po &p2){return det(p1 - p0, p2 - p0);}

DB Po::operator *(const Po &r)const{return dot(*this, r);}
DB Po::operator ^(const Po &r)const{return det(*this, r);}

struct Line{
    Po a, b;
    Line(DB x0=0, DB y0=0, DB x1=0, DB y1=0):a(Po(x0, y0)), b(Po(x1, y1)){}
    Line(const Po &a, const Po &b):a(a), b(b){}
    Line(const Line &l):a(l.a), b(l.b){}

    friend ostream& operator <<(ostream& out, Line p){return out << p.a << "-" << p.b;}
    Line operator +(Po x)const{return Line(a + x, b + x);}
    DB length()const{return (b-a).length();}
    bool dgt()const{return (b-a).dgt();}
    void input(){a.input(), b.input();}
    void getequation(DB, DB, DB) const;
};

struct Seg: Line{
};

inline DB dot(const Line &l1, const Line &l2){return dot(l1.b - l1.a, l2.b - l2.a);}
inline DB det(const Line &l1, const Line &l2){return det(l1.b - l1.a, l2.b - l2.a);}

void Line::getequation(DB A, DB B, DB C) const{
    A = a.y - b.y, B = b.x - a.x, C = det(a, b);
}

template<class T1, class T2> inline DB dist(const T1 &x, const T2 &y){return sqrt(dist_sqr(x, y));}
template<class T1, class T2, class T3> inline DB dist(const T1 &x, const T2 &y, const T3 &z){return sqrt(dist_sqr(x, y, z));}

template<class T1, class T2> inline int dett(const T1 &x, const T2 &y){return sgn(det(x, y));}
template<class T1, class T2> inline int dott(const T1 &x, const T2 &y){return sgn(dot(x, y));}
template<class T1, class T2, class T3> inline int dett(const T1 &x, const T2 &y, const T3 &z){return sgn(det(x, y, z));}
template<class T1, class T2, class T3> inline int dott(const T1 &x, const T2 &y, const T3 &z){return sgn(dot(x, y, z));}
template<class T1, class T2, class T3, class T4> inline int dett(const T1 &x, const T2 &y, const T3 &z, const T4 &w){return sgn(det(x, y, z, w));}
template<class T1, class T2, class T3, class T4> inline int dott(const T1 &x, const T2 &y, const T3 &z, const T4 &w){return sgn(dot(x, y, z, w));}

inline DB dist_sqr(const DB &x, const DB &y){return sqr(x) + sqr(y);}
inline DB dist_sqr(const DB &x, const DB &y, const DB &z){return sqr(x) + sqr(y) + sqr(z);}
inline DB dist_sqr(const Po &a, const Po &b){return sqr(a.x - b.x) + sqr(a.y - b.y);}
inline DB dist_sqr(const Po &p, const Line &l){Po v0 = l.b - l.a, v1 = p - l.a; return sqr(fabs(det(v0, v1))) / v0.length_sqr();}
inline DB dist_sqr(const Po &p, const Seg &l){
    Po v0 = l.b - l.a, v1 = p - l.a, v2 = p - l.b;
    if (sgn(dot(v0, v1)) * sgn(dot(v0, v2)) <= 0) return dist_sqr(p, Line(l));
    else return min(v1.length_sqr(), v2.length_sqr());
}
inline DB dist_sqr(Line l, Po p){return dist_sqr(p, l);}
inline DB dist_sqr(Seg l, Po p){return dist_sqr(p, l);}
inline DB dist_sqr(Line l1, Line l2){
    if (sgn(det(l1, l2)) != 0) return 0;
    return dist_sqr(l1.a, l2);
}
inline DB dist_sqr(Line l1, Seg l2){
    Po v0 = l1.b - l1.a, v1 = l2.a - l1.a, v2 = l2.b - l1.a; DB c1 = det(v0, v1), c2 = det(v0, v2);
    return sgn(c1) != sgn(c2) ? 0 : sqr(min(fabs(c1), fabs(c2))) / v0.length_sqr();
}

bool isIntersect(Seg l1, Seg l2){

    if (l1.a == l2.a || l1.a == l2.b || l1.b == l2.a || l1.b == l2.b) return true;

    return
        min(l1.a.x, l1.b.x) <= max(l2.a.x, l2.b.x) &&
        min(l2.a.x, l2.b.x) <= max(l1.a.x, l1.b.x) &&
        min(l1.a.y, l1.b.y) <= max(l2.a.y, l2.b.y) &&
        min(l2.a.y, l2.b.y) <= max(l1.a.y, l1.b.y) &&
    sgn( det(l1.a, l2.a, l2.b) ) * sgn( det(l1.b, l2.a, l2.b) ) <= 0 &&
    sgn( det(l2.a, l1.a, l1.b) ) * sgn( det(l2.b, l1.a, l1.b) ) <= 0;

}

inline DB dist_sqr(Seg l1, Seg l2){
    if (isIntersect(l1, l2)) return 0;
    else return min(dist_sqr(l1.a, l2), dist_sqr(l1.b, l2), dist_sqr(l2.a, l1), dist_sqr(l2.b, l1));
}

inline bool isOnSide(const Po &p, const Seg &l){
    return p == l.a || p == l.b;
}

inline bool isOnSeg(const Po &p, const Seg &l){
    return sgn(det(p, l.a, l.b)) == 0 &&
        sgn(l.a.x, p.x) * sgn(l.b.x, p.x) <= 0 && sgn(l.a.y, p.y) * sgn(l.b.y, p.y) <= 0;
}

inline bool isOnSegg(const Po &p, const Seg &l){
    return sgn(det(p, l.a, l.b)) == 0 &&
        sgn(l.a.x, p.x) * sgn(l.b.x, p.x) < 0 && sgn(l.a.y, p.y) * sgn(l.b.y, p.y) < 0;
}

inline Po intersect(const Line &l1, const Line &l2){
    return l1.a + (l1.b - l1.a) * (det(l2.a, l1.a, l2.b) / det(l2, l1));
}

// perpendicular foot
inline Po intersect(const Po & p, const Line &l){
    return intersect(Line(p, p + Po(l.a.y - l.b.y, l.b.x - l.a.x)), l);
}

inline Po rotate(Po p, DB alpha, const Po &o = Po()){
    p.rotate(alpha, o);
    return p;
}

} using namespace CG;//}
//}

/** I/O Accelerator Interface .. **/ //{
template<class T> inline T& RD(T &x){
    //cin >> x;
    scanf("%d", &x);
    //char c; for (c = getchar(); c < '0'; c = getchar()); x = c - '0'; for (c = getchar(); '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
    //char c; c = getchar(); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';
    return x;
}

inline DB& RF(DB &x){
    scanf("%lf", &x);
    /*char t; while ((t=getchar())==' '||t=='\n'); x = t - '0';
    while ((t=getchar())!=' '&&t!='\n'&&t!='.')x*=10,x+=t-'0';
    if (t=='.'){DB l=1; while ((t=getchar())!=' '&&t!='\n')l*=0.1,x += (t-'0')*l;}*/
    return x;
}

inline char* RS(char *s){
    //gets(s);
    scanf("%s", s);
    return s;
}

int Case; template<class T> inline void OT(const T &x){
    //printf("Case %d: %d\n", ++Case, x);
    //printf("%.2lf\n", x);
    printf("%d\n", x);
    //cout << x << endl;
}
//}

//}/* .................................................................................................................................. */

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int,double> PIF;

struct line{
    Po x, y;

    line(){}
    line(Po _x,Po _y):x(_x),y(_y){}

    void input(){x.input(), y.input();}
};

struct node{
    string c;vector<int> a;double sz;
    node(string _c,double _sz=0):c(_c),sz(_sz){}
    void add(int x){a.pb(x);}
};

    Po med(Po a,Po b,Po c)
    {
            if(a<b)if(b<c)return b;else if(a<c)return c;else return a;
            else if(a<c)return a;else if(b<c)return c;else return b;
    }
    int getcrs(line a,line b,Po&c)
    {
            double A=a.y.y-a.x.y,B=a.x.x-a.y.x,C=a.x.x*A+a.x.y*B;
            double D=b.y.y-b.x.y,E=b.x.x-b.y.x,F=b.x.x*D+b.x.y*E;
            if(fabs(A*E-B*D)<EPS)return -1;
            c=Po((C*E-F*B)/(A*E-D*B),(C*D-F*A)/(B*D-E*A));
            if(med(a.x,a.y,c)!=c||med(b.x,b.y,c)!=c)return -1;
            return 0;
    }
    bool online(Po p,line a)
    {
            if(med(a.x,a.y,p)!=p)return 0;
            double A=a.y.y-a.x.y,B=a.x.x-a.y.x,C=a.x.x*A+a.x.y*B;
            return abs(A*p.x+B*p.y-C)<EPS;
    }
    double ang(Po a,Po b)
    {
            double S=atan2(b.y-a.y,b.x-a.x)*180/M_PI;
            if(S<EPS)S+=360;
            return S;
    }
    bool cmpid(const PIF&a,const PIF&b){return a.fi<b.fi;}
    bool equid(const PIF&a,const PIF&b){return a.fi==b.fi;}
    bool cmpang(const PIF&a,const PIF&b){return a.se<b.se;}
    double getsz(vector<Po> p)
    {
            double S=0;
            for(int i=0;i<(int)p.size();i++)
            {
                    int j=(i+1)%(int)p.size();
                    S+=p[i].x*p[j].y-p[j].x*p[i].y;
            }
            return -S/2;
    }
    double crs(Po a,Po b,Po c)
    {
            return (b.y-a.y)*(c.x-a.x)-(b.x-a.x)*(c.y-a.y);
    }
    int inpoly(Po p,vector<Po> a)
    {
            int n=(int)a.size();a.pb(a[0]);
            for(int i=0;i<n;i++)
                    if(online(p,line(a[i],a[i+1])))return 0;
            int w=0;
            for(int i=0;i<n;i++)
                    if(a[i].y<=p.y&&a[i+1].y>p.y&&crs(a[i],a[i+1],p)<0)++w;else
                    if(a[i].y>p.y&&a[i+1].y<=p.y&&crs(a[i],a[i+1],p)>0)--w;
            return w;
    }
    int w,h,n,m,p;

    vector<Po> po;
    vector<vector<PIF> > pa;
    vector<node> gr;
    vector<int> bl;
    vector<Po> fl;
    vector<vector<Po> > pl;

    void bfs(int x,string c)
    {
            string d=gr[x].c;
            queue<int> q;vector<bool> v((int)gr.size(),0);
            v[x]=1,q.push(x);
            while(!q.empty())
            {
                    int x=q.front();q.pop();
                    gr[x].c=c;
                    for(int i=0;i<(int)gr[x].a.size();i++)
                            if(!v[gr[x].a[i]]&&gr[gr[x].a[i]].c==d)
                                    q.push(gr[x].a[i]),v[gr[x].a[i]]=1;
            }
    }
    int main()
    {

        #ifndef ONLINE_JUDGE
            freopen("in.txt", "r", stdin);
            //freopen("out.txt", "w", stdout);
        #endif

            RD(w, h, n);

            vector<line> l0(n);

            REP(i, n) l0[i].input();

            //for(int i=0;i<n;i++)l0[i].get();
            for(int i=0;i<n;i++)po.pb(l0[i].x),po.pb(l0[i].y);
            for(int i=0;i<n;i++)
                    for(int j=i+1;j<n;j++)
                    {
                            Po p;
                            if(getcrs(l0[i],l0[j],p)!=-1)po.pb(p);
                    }
            sort(po.begin(),po.end()),
            po.erase(unique(po.begin(),po.end()),po.end());
            m=(int)po.size();
            pa.resize(m);

            for(int i=0;i<n;i++)
            {
                    vector<int> vp;
                    for(int j=0;j<m;j++)
                            if(online(po[j],l0[i]))vp.pb(j);
                    for(int j=1;j<(int)vp.size();j++)
                            pa[vp[j-1]].pb(mp(vp[j],ang(po[vp[j-1]],po[vp[j]]))),
                            pa[vp[j]].pb(mp(vp[j-1],ang(po[vp[j]],po[vp[j-1]])));
            }

            for(int i=0;i<m;i++)
                    sort(pa[i].begin(),pa[i].end(),cmpid),
                    pa[i].erase(unique(pa[i].begin(),pa[i].end(),equid),pa[i].end()),
                    sort(pa[i].begin(),pa[i].end(),cmpang);
            gr.pb(node("white",w*h));
            fl.pb(Po(0.0,0.0));
            bl.resize(m,-1);
            for(int i=0;i<m;i++)if(bl[i]==-1)
            {
                    queue<int> q;
                    q.push(i);
                    gr.pb(node("black",0.0));
                    bl[i]=++p,fl.pb(po[i]);
                    while(!q.empty())
                    {
                            int x=q.front();q.pop();
                            for(int j=0;j<(int)pa[x].size();j++)
                                    if(bl[pa[x][j].fi]==-1)
                                            q.push(pa[x][j].fi),
                                            bl[pa[x][j].fi]=p;
                    }
            }
            pl.resize(p+1);
            vector<vector<bool> > chk(m);
            for(int i=0;i<m;i++)chk[i].resize((int)pa[i].size(),0);
            for(int i=0;i<m;i++)
                    for(int j=0;j<(int)pa[i].size();j++)
                    {
                            if(chk[i][j])continue;
                            vector<Po> vp;
                            int i0=i,j0=j;
                            while(!chk[i0][j0])
                            {
                                    vp.pb(po[i0]);
                                    chk[i0][j0]=1;
                                    int i1=pa[i0][j0].fi,j1=-1;
                                    for(int j=0;j<(int)pa[i1].size();j++)
                                            if(pa[i1][j].fi==i0){j1=(j+1)%(int)pa[i1].size();break;}
                                    i0=i1,j0=j1;
                            }
                            double sz=getsz(vp);
                            if(sz>0)
                            {
                                    pl.pb(vp);
                                    gr.pb(node("white",sz));
                                    int r=bl[i];
                                    gr[r].add((int)gr.size()-1),
                                    gr[(int)gr.size()-1].add(r);
                                    gr[r].sz-=sz;
                            }
                    }
            for(int i=1;i<=p;i++)
            {
                    int fa=0;
                    for(int j=(int)pl.size()-1;j>p;j--)
                            if(inpoly(fl[i],pl[j])){fa=j;break;}
                    gr[fa].add(i),gr[i].add(fa),gr[fa].sz+=gr[i].sz,gr[i].sz=0;
            }

        int _;scanf("%d",&_);
        while(_--){

            Po x; x.input();string c;char s[100];scanf("%s",s),c=s;

            for(int i=0;i<m;i++)
                for(int j=0;j<(int)pa[i].size();j++)

            if(online(x,line(po[i],po[pa[i][j].fi]))){bfs(bl[i],c);goto end;}
            for(int i=(int)pl.size()-1;i>p;i--)
                if(inpoly(x,pl[i])){bfs(i,c);goto end;}
            bfs(0,c);
            end:;
        }

        map<string,double> S;
        for(int i=0;i<(int)gr.size();i++)
            S[gr[i].c]+=gr[i].sz;
        //for(map<string,double>::iterator i=S.begin();i!=S.end();i++)

    ECH(it, S){
        printf("%s %.9lf\n", it->first.c_str(), it->second);
    }
    return 0;
}