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

const int di[] = {1, -1, 0, 0};
const int dj[] = {0, 0, 1, -1};

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

	int h, w;
	cin >> h >> w;

	vector <string> g(h);
	vector < vector < pair <int, int> > > go(26);

	int si, sj, gi, gj;
	for(int i = 0; i < h; i++){
		cin >> g[i];
		for(int j = 0; j < w; j++){
			if(g[i][j] == 'S')si = i, sj = j;
			if(g[i][j] == 'G')gi = i, gj = j;
			if('a' <= g[i][j] && g[i][j] <= 'z'){
				go[g[i][j] - 'a'].push_back({i, j});
			}
		}
	}

	vector <int> d(h * w + 26, -1);
	vector <bool> vis(h * w + 26, false);
	deque <int> q;

	d[si * w + sj] = 0;
	q.push_back(si * w + sj);

	while(!q.empty()){
		int u = q.front();
		q.pop_front();

		if(vis[u])continue;
		vis[u] = true;

		if(u >= h * w){
			int c = u - h * w;
			for(auto [ni, nj] : go[c]){
				if(d[ni * w + nj] == -1 || d[ni * w + nj] > d[u] + 1){
					d[ni * w + nj] = d[u] + 1;
					q.push_back(ni * w + nj);
				}
			}
		}else{
			int i = u / w, j = u % w;

			if('a' <= g[i][j] && g[i][j] <= 'z'){
				if(d[h * w + g[i][j] - 'a'] == -1 || d[h * w + g[i][j] - 'a'] > d[u]){
					d[h * w + g[i][j] - 'a'] = d[u];
					q.push_front(h * w + g[i][j] - 'a');
				}
			}

			for(int dir = 0; dir < 4; dir++){
				int ni = i + di[dir], nj = j + dj[dir];
				if(ni < 0 || ni >= h || nj < 0 || nj >= w)continue;
				if(g[ni][nj] == '#')continue;

				if(d[ni * w + nj] == -1 || d[ni * w + nj] > d[u] + 1){
					d[ni * w + nj] = d[u] + 1;
					q.push_back(ni * w + nj);
				}
			}
		}
	}

	printf("%d\n", d[gi * w + gj]);

	return 0;
}

#include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define rrep(i, n) for (int i = (int)n-1; i >= 0; --i)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9+7;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

__attribute__ ((constructor))
void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(10);
}

int main(){
    int N, M;
    cin >> N >> M;

    vector<vector<int>> to(N);

    rep(i,M){
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        to[a].push_back(b);
        to[b].push_back(a);
    }

    int k;
    cin >> k;
    vector<int> c(k);
    rep(i, k) cin >> c[i];
    rep(i, k) c[i]--;

    // k個の頂点間の最短距離をBFSして求める．
    auto bfs = [&](int now_c) {
        vector<int> dist(N,inf);
        dist[now_c] = 0;
        queue<int> que;
        que.push(now_c);
        while(!que.empty()){
            int c_front = que.front();
            que.pop();
            for(auto u : to[c_front]){
                if(dist[u] != inf)
                    continue;
                chmin(dist[u], dist[c_front] + 1);
                que.push(u);
            }
        }
        return dist;
    };

    vector<vector<int>> dist_k(k, vector<int>(k, inf));
    rep(i,k){
        auto dist = bfs(c[i]);
        rep(j, k){
            if(dist[c[j]] != inf){
                chmin(dist_k[i][j],dist[c[j]]);
            }
        }
    }

    // bit DP

    int k2 = 1 << k;

    vector<vector<int>> dp(k2, vector<int>(k, inf));

    // 初期化
    rep(i,k){
        dp[1<<i][i] = 1; //1個の石のみ
    }

    rep(i,k2){
        rep(j, k){
            if(!(i>>j)&1)
                continue;
            rep(l, k){
                if(i>>k&1)
                    continue;
                chmin(dp[i | 1 << l][l], dp[i][j] + dist_k[j][l]);
            }
        }
    }

    int ans = inf;

    rep(i,k){
        chmin(ans,dp[k2-1][i]);
    }

    if(ans != inf)
        cout << ans << endl;
    else
        cout << -1 << endl;

    return 0;
}


#include <bits/stdc++.h>

const long long INF = 1e9;
const long long MOD = 1e9 + 7;
//const long long MOD = 998244353;
const long long LINF = 1e18;
using namespace std;

#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE"  ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible"  ) << endl
#define dump(x)  cout << #x << " = " << (x) << endl
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define COUT(x) cout<<(x)<<endl
#define SCOUT(x) cout<<(x)<<" "
#define VECCOUT(x) for(auto&youso_: (x) )cout<<right<<setw(10)<<youso_<<" ";cout<<endl
#define ENDL cout<<endl
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define LCIN(...) long long __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)
#define VECCIN(x) for(auto&youso_: (x) )cin>>youso_
#define mp make_pair
#define PQ priority_queue<long long>
#define PQG priority_queue<long long,V,greater<long long>>

typedef long long ll;
typedef vector<long long> vl;
typedef vector<long long> vi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
typedef pair<long long, long long> pll;

#define COUT(x) cout<<(x)<<endl
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){
    cin>>head;
    CINT(move(tail)...);
}
template<class T>
void mod(T &x) {
    x %= MOD;
    x += MOD;
    x %= MOD;
}

ll GCD(ll a, ll b) {
    if(b == 0) return a;
    else return GCD(b, a%b);
}

struct COMB{
    vl fact, fact_inv, inv;
    void init_nCk(long long SIZE) {
        fact.resize(SIZE + 5);
        fact_inv.resize(SIZE + 5);
        inv.resize(SIZE + 5);
        fact.at(0) = fact.at(1) = fact_inv.at(0) = fact_inv.at(1) = inv.at(1) = 1;
        for(long long i = 2; i < SIZE + 5; i++) {
            fact.at(i) = fact.at(i - 1)*i%MOD;
            inv.at(i) = MOD - inv.at(MOD%i)*(MOD/i)%MOD;
            fact_inv.at(i) = fact_inv.at(i - 1)*inv.at(i)%MOD;
        }
    }
    long long nCk (long long n, long long k) {
        assert(!(n < k));
        assert(!(n < 0 || k < 0));
        return fact.at(n)*(fact_inv.at(k)*fact_inv.at(n - k)%MOD)%MOD;
    }
};
ll extGCD(ll a, ll b, ll &x, ll &y) {
    if(b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    ll d = extGCD(b, a%b, y, x);
    y -= a/b*x;
    return d;
}

void Main() {
    LCIN(T);
    for(int i = 0; i < T; i++) {
        LCIN(N);
        SCIN(S1, S2, S3);
        for(int i = 0; i < N; i++) {
            cout << 0;
        }
        for(int i = 0; i < N; i++) {
            cout << 1;
        }
        cout << 0 << endl;
    }
}

int main() {
    cout << fixed << setprecision(15);
    Main();
    return 0;
}

//Bismillahir Rahmanir Raheem
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update>pref_trie;
#define fi first
#define se second
#define PI 3.14159265
#define mod  998244353
#define ordered_set tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update>
///order_of_key find_by_order
const ll INFLL = 1e18;
const ll INF =1e9;
const ll EPS =1e-9;
void fast()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
ll gcd(ll a,ll b){if(b==0) return a; else return gcd(b,a%b);}
ll lcm(ll a,ll b){ return (a*b)/gcd(a,b);}
ll ncr(ll n,ll r){ ll ans=1;for(ll i=1;i<=r;i++) ans=(ans*(n-i+1))/i;return ans;}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        string s1,s2,s3;
        cin>>n>>s1>>s2>>s3;
        for(int i=0;i<n;i++) cout<<1;
        for(int i=0;i<n;i++) cout<<0;
        cout<<1<<endl;
    }
}


#include <iostream>
#include <vector>
#include <utility>
#include <numeric>
#include <limits>
#include <tuple>
#include <cmath>

using ull = unsigned long long int;
using ll = long long int;

int main()
{
	std::string s;
	std::cin >> s;
	bool flag = true;
	for(ull i = 0; i<s.size(); i++)
	{
		if(i%2 == 0 and int(s[i]) < 96)
		{
			std::cout<<"No\n";
			flag = false;
			break;
		}
		else if(i%2 == 1 and int(s[i]) > 96)
		{
			std::cout<<"No\n";
			flag = false;
			break;
		}
	}
	if(flag) std::cout<<"Yes\n";
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int main(){
  int i,n;
  cin >> n;
  int ans=0;
  for(i=1;i<=n;i++){
    int a=i,res=0;
    while(a!=0){
      if(a%10==7){
        ans++,res=1;
        break;
      }
      a/=10;
    }
    if(res==1){
      continue;
    }
    a=i,res=0;
    while(a!=0){
      if(a%8==7){
        ans++,res=1;
        break;
      }
      a/=8;
    }
    if(res==1){
      continue;
    }
  }
  cout << n-ans << endl;
}


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

#define int long long
#define ff first
#define ss second
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
int powmod(int a,int l, int md){a%=md; int res=1;while(l){if(l&1)res=res*a%md;l/=2;a=a*a%md;}return res;}
int binpow(int a,int l){int res=1;while(l){if(l&1)res=res*a;l/=2;a=a*a;}return res;}
int invmod(int a, int md){return powmod(a,md-2,md);}
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
typedef vector<int> vi; typedef pair<int, int> ii; typedef vector< ii > vii;
#define pb push_back
int __set(int b, int i) {return b|(1LL<<i);} //set ith bit
int __unset(int b, int i) {return b&(~(1UL<<i));}
int __check(int b, int i) {return b&(1LL<<i);} //returns 0 if ith bit is 0
int mulmod(int a, int b, int md) {return (((a%md)*(b%md))%md+md)%md;}
int addmod(int a, int b, int md) {return ((a%md+b%md)%md+md)%md;}
int submod(int a, int b, int md) {return (((a%md-b%md)%md)+md)%md;}
int divmod(int a, int b, int md) {return mulmod(a, powmod(b, md-2, md), md);} //if md is prime;
const ll inf = 0xFFFFFFFFFFFFFFFL; //very large number
priority_queue<int, vector<int>, greater<int> > pq; //for min priority_queue
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int MOD=998244353;
char mat[5010][5010];
int dp[5010][5010],sufr[5010][5010],sufc[5010][5010];
signed main(void)
{
	IOS;
	int n,m,k; cin>>n>>m>>k;

	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++)
			mat[i][j] = '0';
	}

	for(int i = 0; i < k; i++) {
		int r,c;
		char ch;
		cin>>r>>c>>ch;
		mat[r-1][c-1] = ch;
	}
	// int sufr[n][m], sufc[m][n];
	for(int i = 0; i < n; i++) {
		sufr[i][m-1] = 0;
		if(mat[i][m-1] == '0')
			sufr[i][m-1]++;
		for(int j = m-2; j >= 0; j--) {
			sufr[i][j] = sufr[i][j+1];
			if(mat[i][j] == '0')
				sufr[i][j]++;
		}
	}
	for(int i = 0; i < m; i++) {
		sufc[i][n-1] = 0;
		if(mat[n-1][i]=='0')
			sufc[i][n-1]++;
		for(int j = n-2; j >= 0; j--) {
			sufc[i][j] = sufc[i][j+1];
			if(mat[j][i] == '0')
				sufc[i][j]++;
		}
	}
	// int dp[n][m];
	// for(int i = 0; i < n; i++) {
	// 	for(int j = 0; j < m; j++)
	// 		cout<<sufr[i][j]<<" ";
	// 	cout<<endl;
	// }
	// cout<<endl<<endl;
	// for(int i = 0; i < m; i++) {
	// 	for(int j = 0; j < n; j++)
	// 		cout<<sufc[i][j]<<" ";
	// 	cout<<endl;
	// }
	// memset(dp,0,sizeof(dp));
	int po[5010];
	po[0]=1;
	for(int i = 1; i < 5010; i++) {
		po[i] = po[i-1]*3;
		if(po[i] >= MOD)
			po[i] %= MOD;
	}
	dp[0][0]=1;
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			if(i-1 >= 0) {
				if(mat[i-1][j] != 'R') {
					int tp=0;
					if(j+1<m)
						tp = sufr[i-1][j+1];
					int tmp = dp[i-1][j]*po[tp];
					if(tmp >= MOD)
						tmp %= MOD;
					if(mat[i][j] == '0') {
						tmp *= 2;
						if(tmp >= MOD)
							tmp %= MOD;
					}
					dp[i][j] += tmp;
					if(dp[i][j] >= MOD)
						dp[i][j] %= MOD;
				}
			}
			if(j-1 >= 0 && mat[i][j-1] != 'D') {
				int tp = 0;
				if(i+1 < n)
					tp = sufc[j-1][i+1];
				int tmp = dp[i][j-1]*po[tp];
				if(tmp >= MOD)
					tmp %= MOD;
				if(mat[i][j] == '0') {
					tmp *= 2;
					if(tmp >= MOD)
						tmp %= MOD;
				}
				dp[i][j] += tmp;
				if(dp[i][j] >= MOD)
					dp[i][j] %= MOD;

			}
		}
	}

	// for(int i = 0; i < n; i++) {
	// 	for(int j = 0; j < m; j++)
	// 		cout<<dp[i][j]<<" ";
	// 	cout<<endl;
	// }

	if(mat[n-1][m-1]=='0') {
		dp[n-1][m-1] *= 3;
		dp[n-1][m-1]%= MOD;
	}
	cout<<dp[n-1][m-1]<<endl;
	return 0;
}

#include <bits/stdc++.h>
#ifdef LOCAL
    #include "../../Library/DebugTool.cpp"
#endif
using namespace std;
#define ll long long
#define pii pair<int,int>
//const ll MOD = 1000000007;
const ll MOD = 998244353;
ll INF_int = 1e9+11;ll INF_ll = 1e18 + 13;
#define rep(i,n) for(int i = 0;i<n;i++)
#define rep1(i,n) for(int i = 1;i<n;i++)
#define per(i,n) for(int i = n-1;i>=0;i--)
#define mp make_pair
#define All(a) a.begin(),a.end()

void in(){}
template<typename Head,typename ...Tail>void in(Head&& head,Tail&& ...tail){cin >> head;in(std::forward<Tail>(tail)...);}
template<typename T>void vecin(vector<T> &vec){int n = vec.size();rep(i,n) cin >> vec[i];}
void out(){cout << "\n";}
template<typename Head,typename ...Tail>void out(Head&& head,Tail&& ...tail){cout << head << " ";out(std::forward<Tail>(tail)...);}
template<typename T>void vecout(vector<T> a){for(auto x:a)cout << x << " ";cout << endl;}
template<typename T>bool umax(T &x,const T &y){if(x<y){x=y;return true;}return false;}
template<typename T>bool umin(T &x,const T &y){if(x>y){x=y;return true;}return false;}

template<int MOD>
struct mint{
    ll x;

    mint(ll a=0) : x(((a%MOD)+MOD)%MOD) {}

    mint &operator += (const mint &a){
        x+=a.x;if(x>=MOD)x-=MOD;return *this;
    }
    mint &operator -= (const mint &a){
        x-=a.x;if(x<0)x+=MOD;return *this;
    }
    mint &operator *= (const mint &a){
        x*=a.x;x%=MOD;return *this;
    }
    mint &operator /= (const mint &a){
        x*=a.inv().x;x%=MOD;return *this;
    }

    mint operator + (const mint &a) const{
        mint res(*this);
        return res+=a;
    }
    mint operator - (const mint &a) const{
        mint res(*this);
        return res-=a;
    }
    mint operator * (const mint &a) const{
        mint res(*this);
        return res*=a;
    }
    mint operator / (const mint &a) const{
        mint res(*this);
        return res/=a;
    }

    mint pow(ll a) const{
        mint now = *this;mint ret = 1;
        while(a>0){if(a&1) ret*=now;now*=now;a/=2;}
        return ret;
    }

    mint inv() const{
        return pow(MOD-2);
    }

    mint combi(int k) const{
        k = min(x,x-k);
        mint ret = 1;
        for(int i = x;i>=x-k+1;i--) ret*=mint(i);
        for(int i = 1;i<=k;i++) ret/=mint(i);
        return ret;
    }

    friend istream& operator >> (istream& is,mint& m){
        ll a;
        is >> a;m = mint(a);
        return is;
    }

    friend ostream& operator << (ostream& os, const mint& m){
        os << m.x;
        return os;
    }
};

int main(){
    int h,w,k;in(h,w,k);
    char state[h][w];
    rep(i,h) rep(j,w) state[i][j] = 'x';
    rep(i,k){
        int a,b;char c;in(a,b,c);
        a--;b--;state[a][b]=c;
    }
    using mint = mint<MOD>;
    ll tate_acc[w][h+1],yoko_acc[h][w+1];
    rep(i,h)rep(j,w+1) yoko_acc[i][j]=0;
    rep(i,h+1)rep(j,w) tate_acc[j][i]=0;
    rep(i,h) rep(j,w) if(state[i][j]=='x') {yoko_acc[i][j+1]=yoko_acc[i][j]+1;}else{yoko_acc[i][j+1]=yoko_acc[i][j];}
    rep(j,w) rep(i,h) if(state[i][j]=='x') {tate_acc[j][i+1]=tate_acc[j][i]+1;}else{tate_acc[j][i+1]=tate_acc[j][i];}
    mint dp[h][w];
    rep(i,h) rep(j,w) dp[i][j] = mint(0);
    dp[0][0] = 1;
    vector<mint> th(5005);
    th[0]=mint(1);
    rep(i,5005) th[i+1]=th[i]*mint(3);
    rep(i,h){
        rep(j,w){
            if(i!=0){
                if(state[i-1][j] == 'R') dp[i][j] += mint(0);
                else if(state[i-1][j] == 'D' || state[i-1][j] == 'X') dp[i][j] += (dp[i-1][j])*th[yoko_acc[i][j]];
                else  dp[i][j] += (mint(2)*dp[i-1][j])*th[yoko_acc[i][j]];
            }
            if(j!=0){
                if(state[i][j-1] == 'D') dp[i][j] += mint(0);
                else if(state[i][j-1] == 'R' || state[i][j-1] == 'X') dp[i][j] += (dp[i][j-1])*(th[tate_acc[j][i]]);
                else  dp[i][j] += (mint(2)*dp[i][j-1])*(th[tate_acc[j][i]]);
            }
        }
    }
    if(state[h-1][w-1]=='x') dp[h-1][w-1]*=mint(3);
    out(dp[h-1][w-1]);

}


// give up
#include <bitset>
#include <cassert>
#include <iostream>
#include <iomanip>
#include <list>
#include <numeric>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <valarray>

// alias, using declaration
using llint = long long; using ldouble = long double;
template <class T = llint> using vector = std::vector<T>;
template <class T = llint> using deque = std::deque<T>;
template <class T = llint> using list = std::list<T>;
template <class T = llint, class U = llint> using umap = std::unordered_map<T, U>;
template <class T = llint, class U = llint> using ummap = std::unordered_multimap<T, U>;
template <class T = llint> using uset = std::unordered_set<T>;
template <class T = llint> using umset = std::unordered_multiset<T>;
template <class T = llint, class U = T> using pair = std::pair<T, U>;
template <class T = llint> using varray = std::valarray<T>;
template <class T = llint, class U = vector<T>, class V = std::less<class U::value_type>> using pqueue = std::priority_queue<T, U, V>;
using std::array; using std::bitset; using std::string; using std::tuple;

// constant
constexpr llint INF = 0x7FFFFFFFFFFFFFFF;
constexpr llint INVALID = 0x8000000000000000;

namespace io {
  // in
  template <class... T, size_t... S> auto in(std::index_sequence<S...>) {tuple<T...> t; (..., (std::cin >> std::get<S>(t))); return t;}
  template <class... T, size_t... S> auto in(std::index_sequence<S...>, size_t n) {tuple<vector<T>...> t{vector<T>(n)...}; for (size_t i = 0; i < n; i++) (..., (std::cin >> std::get<S>(t)[i])); return t;}
  template <size_t N, class T = llint, class... U> auto in() {static_assert(N >= 1); if constexpr (N > 1) return in<N - 1, T, T, U...>(); else return in<T, U...>(std::index_sequence_for<T, U...>());}
  template <size_t N, class T = llint, class... U> auto in(size_t n) {static_assert(N >= 1); if constexpr (N > 1) return in<N - 1, T, T, U...>(n); else return in<T, U...>(std::index_sequence_for<T, U...>(), n);}
  template <size_t N, class T = llint> auto in(size_t n, size_t m) {static_assert(N == 1); vector<vector<T>> v(n, vector<T>(m)); for (auto&& vi : v) for (auto&& vij : vi) std::cin >> vij; return std::make_tuple(v);}
  // out
  template <class T, class... U> void out(const T& a, const U&... b) {std::cout << a << (sizeof...(b) ? " " : "\n"); if constexpr (sizeof...(b)) out(b...);}
  template <class T, class U = const char*> void out(const vector<T>& a, U d = " ") {for (auto&& b : a) std::cout << b << (&b != &a.back() ? d : "\n");}
}

int main() {
  // input
  auto [N, M, Q] = io::in<3>();
  auto [T, X, Y] = io::in<3>(Q);

  // solve
  vector<> a(N + 1), b(M + 1);
  umap<> Z;
  {
    Z.emplace(0, 0);
    vector<> Y_ = Y;
    std::sort(Y_.begin(), Y_.end());
    for (llint i = 0; i < Q; i++) Z.emplace(Y_.at(i), i + 1);
  }
  constexpr llint ST_SIZE = 1 << 19;
  vector<pair<>> st_a(ST_SIZE), st_b(ST_SIZE);
  auto update = [&Z](vector<pair<>>& st, llint v, llint n) {
    for (llint i = (ST_SIZE >> 1) + Z.at(v); i > 0; i >>= 1) {
      st.at(i).first += n;
      st.at(i).second += v * n;
    }
  };
  auto culc_line = [&Z](const vector<pair<>>& st, llint v) {
    llint r = st.at((ST_SIZE >> 1) + Z.at(v)).second;
    for (llint i = (ST_SIZE >> 1) + Z.at(v); i > 1; i >>= 1) {
      r += i & 1 ? st.at(i ^ 1).first * v : st.at(i ^ 1).second;
    }
    return r;
  };
  update(st_a, 0, N);
  update(st_b, 0, M);
  vector<> result(Q);
  for (llint i = 0; i < Q; i++) {
    auto& t = (T.at(i) == 1 ? a : b).at(X.at(i));
    auto& st_x = T.at(i) == 1 ? st_a : st_b;
    auto& st_y = T.at(i) == 1 ? st_b : st_a;
    update(st_x, t, -1);
    result.at(i) = -culc_line(st_y, t);
    t = Y.at(i);
    update(st_x, t, 1);
    result.at(i) += culc_line(st_y, t);
  }
  for (llint i = 1; i < Q; i++) result.at(i) += result.at(i - 1);

  // output
  io::out(result, "\n");
}

#include<bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x){
	x=0;char ch=getchar();bool f=false;
	while(!isdigit(ch)){if(ch=='-'){f=true;}ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	x=f?-x:x;
	return ;
}
template <typename T>
inline void write(T x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10^48);
	return ;
}
#define ll long long
const int N=2e5+5,M=1e8;
int n,m,q;
ll a[N],b[N];
struct SGTree{
	int cur,root;
	ll sum[N*80];
	int num[N*80],ls[N*80],rs[N*80];
	void Modify(int &x,int l,int r,int pos,int v,int tp){
		if(!x) x=++cur;
		int mid=l+r>>1;sum[x]+=v*tp;num[x]+=tp;
		if(l==r) return ;
		if(pos<=mid) Modify(ls[x],l,mid,pos,v,tp);
		else Modify(rs[x],mid+1,r,pos,v,tp);
		return ;
	}
	ll QuerySum(int x,int l,int r,int ql,int qr){
		if(ql>qr||!x) return 0;
		if(ql<=l&&r<=qr) return sum[x];
		int mid=l+r>>1;ll res=0;
		if(ql<=mid) res+=QuerySum(ls[x],l,mid,ql,qr);
		if(qr>mid) res+=QuerySum(rs[x],mid+1,r,ql,qr);
		return res;
	}
	int QueryNum(int x,int l,int r,int ql,int qr){
		if(ql>qr||!x) return 0;
		if(ql<=l&&r<=qr) return num[x];
		int mid=l+r>>1,res=0;
		if(ql<=mid) res+=QueryNum(ls[x],l,mid,ql,qr);
		if(qr>mid) res+=QueryNum(rs[x],mid+1,r,ql,qr);
		return res;
	}
}t1,t2;
ll Ans;
int main(){
	read(n),read(m),read(q);
	for(int i=1;i<=n;i++) t1.Modify(t1.root,0,M,a[i],a[i],1);
	for(int i=1;i<=m;i++) t2.Modify(t2.root,0,M,b[i],b[i],1);
//	for(int i=1;i<=n;i++){
//		Ans+=1ll*t2.QueryNum(t2.root,0,M,1,a[i])*a[i];
//		Ans+=1ll*t2.QuerySum(t2.root,0,M,a[i]+1,M);
//	}
	for(int i=1;i<=q;i++){
		int op,pos;ll v;
		read(op),read(pos),read(v);
		if(op==1){
			Ans-=1ll*t2.QueryNum(t2.root,0,M,0,a[pos])*a[pos];
			Ans-=1ll*t2.QuerySum(t2.root,0,M,a[pos]+1,M);
			t1.Modify(t1.root,0,M,a[pos],a[pos],-1);

			a[pos]=v;
			Ans+=1ll*t2.QueryNum(t2.root,0,M,0,a[pos])*a[pos];
			Ans+=1ll*t2.QuerySum(t2.root,0,M,a[pos]+1,M);
			t1.Modify(t1.root,0,M,a[pos],a[pos],1);
		}
		else{
			Ans-=1ll*t1.QueryNum(t1.root,0,M,0,b[pos])*b[pos];
			Ans-=1ll*t1.QuerySum(t1.root,0,M,b[pos]+1,M);
			t2.Modify(t2.root,0,M,b[pos],b[pos],-1);
			b[pos]=v;
			Ans+=1ll*t1.QueryNum(t1.root,0,M,0,b[pos])*b[pos];
			Ans+=1ll*t1.QuerySum(t1.root,0,M,b[pos]+1,M);
			t2.Modify(t2.root,0,M,b[pos],b[pos],1);
		}
		write(Ans),putchar('\n');
	}
	return 0;
}


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

#define ll long long
#define ld long double
#define precision(x,d) cout<<fixed<<setprecision(d)<<x
#define all(x) x.begin(),x.end()
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);srand(time(NULL));
#define MOD 1000000007
#define INF LONG_LONG_MAX
#define NINF LONG_LONG_MIN
//#define endl "\n"
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

int main()
{
    IOS;
    ll t,k,n,m,d,u,v;
    t=1;
    //cin >> t;
    while(t--)
    {
        cin >> n;
        vector<ll> v(n);
        ll ans = 0, mx = 0, pref = 0, prefpref = 0;
        for(int i=0;i<n;i++)
        {
            cin >> v[i];
            pref += v[i];
            mx = max(mx,pref);
            ans = max(ans,prefpref+mx);
            prefpref += pref;
        }
        cout << ans << endl;
    }
    return 0;
}

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

int main() {
    long long int N;
    cin >> N;
    vector<long long int> A(N), cumsum(N, 0);
    for(int i = 0; i < N; ++i) cin >> A.at(i);
    if(N == 1) {
        if(A.at(0) > 0) cout << A.at(0) << endl;
        else cout << 0 << endl;
        return 0;
    }

    cumsum.at(0) = A.at(0);
    for(int i = 1; i < N; ++i) {
        cumsum.at(i) = cumsum.at(i-1) + A.at(i);
    }
    // どのloop回数で最大化を探す
    long long int sum = A.at(0), candi = 0, candsum = A.at(0);
    vector<long long int> offset(N);
    offset.at(0) = A.at(0);
    // if(sum > 0) candi = 0;
    for(int i = 1; i < N; ++i) {
        candsum += cumsum.at(i);
        offset.at(i) = candsum;
        if(candsum > sum) {
            sum = candsum; // update
            candi = i;
        }
        // cout << offset.at(i) << endl;
    }

    long long int ans = offset.at(candi);
    // candsum = sum;
    for(int i = candi; i >= 0; --i) {
        sum -= A.at(i);
        ans = max(sum, ans);
    }
    sum = offset.at(candi);
    if(candi < N-1) {
        for(int i = 0; i <= candi+1; ++i) {
            sum += A.at(i);
            ans = max(sum, ans);
        }
    }
    cout << ans << endl;

}


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

#define ll              long long
#define pb              push_back
#define all(v)          v.begin(),v.end()
#define sz(a)           (ll)a.size()
#define F               first
#define S               second
#define INF             2000000000000000000
#define popcount(x)     __builtin_popcountll(x)
#define pll             pair<ll,ll>
#define pii             pair<int,int>
#define ld              long double

const int M = 1000000007;
const int MM = 998244353;
const long double PI = acos(-1);

template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; }
template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; }
template<typename T, typename U> ostream& operator<<(ostream &os, const pair<T, U> &p)
{
    return os<<'('<<p.F<< ","<<p.S<<')';
}

const int N = 105;

vector<int> v[N];

set<pii> A;

bool vis[N];

int dep[N];
int a[N];

void dfs(int s,int p)
{
    vis[s] = true;
    for(auto j:v[s])
    {
        if(!vis[j])
        {
            dep[j] = dep[s] + 1;
            A.insert({s,j});
            dfs(j,s);
        }
        else if(j!=p && dep[j]<dep[s])
        {
            A.insert({s,j});
        }
    }
}

int _runtimeTerror_()
{
    int n,m;
    cin>>n>>m;
    vector<int> ans(m,-1);
    vector<pii> ed;
    for(int i=0;i<m;++i)
    {
        int x,y;
        cin>>x>>y;
        ed.pb({x,y});
    }
    for(int i=1;i<=n;++i)
        cin>>a[i];
    // 0 right,1 left;
    int cur = 0;
    for(auto &[j,k]:ed)
    {
        if(a[j] != a[k])
        {
            if(a[j]>a[k])
                ans[cur] = 0;
            else
                ans[cur] = 1;
        }
        else
        {
            v[j].pb(k);
            v[k].pb(j);
        }
        ++cur;
    }
    for(int i=1;i<=n;++i)
    {
        if(!vis[i])
        {
            dfs(i,0);
        }
    }
    cur = 0;
    for(auto &[j,k]:ed)
    {
        if(a[j]==a[k])
        {
            if(A.count({j,k}))
                ans[cur] = 0;
            else
                ans[cur] = 1,assert(A.count({k,j}));
        }
        ++cur;
    }
    for(int i=0;i<m;++i)
    {
        cout<<(ans[i]?"<-":"->")<<"\n";
    }
    return 0;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    #ifdef runSieve
        sieve();
    #endif
    #ifdef NCR
        initialize();
    #endif
    int TESTS=1;
    //cin>>TESTS;
    while(TESTS--)
        _runtimeTerror_();
    return 0;
}

#include<bits/stdc++.h>
#define int long long
#define ld long double

#define all(X) (X).begin(), (X).end()
#define reset(X) memset(X, 0, sizeof(X))
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define endl '\n'
#define ff first
#define ss second

using namespace std;
const int mod=1e9+7;
 // const int mod=998244353;
 // const int mod=1e9+9;
const int INF=4e18+10;
// const int INF=4e18+10;

typedef pair<int, int> pii;

int power(int a, int b, int in_mod)
{
    int ans=1;
    int prod=a;
    while(b)
    {
        if(b%2)
            ans=(ans*prod)%in_mod;
        prod=(prod*prod)%in_mod;
        b/=2;
    }
    return ans;
}
int mod_inv(int q, int in_mod)
{

    return power(q, in_mod-2,in_mod);
}

int gcd(int a,int b){
    if(b==0) return a;
    return gcd(b,a%b);
}

int fact(int n,int in_mod){
    int f=1;
    for(int i=1;i<=n;i++) f=(f*i)%in_mod;
    return f;
}
int ncr(int n,int r,int in_mod){
    return (((fact(n,in_mod)*mod_inv(fact(r,in_mod),in_mod))%in_mod)*mod_inv(fact(n-r,in_mod),in_mod))%in_mod;
}
const int N = 25;
vector<int> adj[N];
int vis[N]={};
int edge[N][N]={};
int par[N]={};
void dfs(int s,int p,vector<int>& v){
    v.pb(s);
    par[s]=p;
    vis[s]=1;
    for(auto u:adj[s]){
        if(!vis[u]) dfs(u,s,v);
    }
}
void fun(vector<int>& v,int idx,vector<int>& col,int& cnt){
    if(idx==v.size()){
        int flag=0;
        for(int i=0;i<idx;i++){
            for(int j=i+1;j<idx;j++){
                if(edge[v[i]][v[j]] && col[v[i]]==col[v[j]]){
                    flag=1;
                }
            }
        }
        if(!flag) cnt++;
        return ;
    }

    set<int> st={1,2,3};
    st.erase(col[par[v[idx]]]);
    for(auto f:st){
        col[v[idx]]=f;
        fun(v,idx+1,col,cnt);
        col[v[idx]]=0;
    }

}
void solve()
{
    //write code here
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int u,v;
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
        edge[u][v]=1;
        edge[v][u]=1;
    }
    int ans=1;
    for(int i=1;i<=n;i++){
        if(!vis[i]){
            vector<int> v;
            dfs(i,-1,v);
            int cnt=0;
            vector<int> col(N,0);
            col[i]=1;
            fun(v,1,col,cnt);
            col[i]=2;
            fun(v,1,col,cnt);
            col[i]=3;
            fun(v,1,col,cnt);

            ans*=cnt;
        }
    }
    cout<<ans<<endl;
}

signed main()

{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cout<<fixed<<setprecision(10);
    #ifndef ONLINE_JUDGE
    if(fopen("INPUT.txt","r"))
    {
        freopen ("INPUT.txt" , "r" , stdin);
        freopen ("OUTPUT.txt" , "w" , stdout);
    }
    #endif
    auto clk=clock();
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    // -------------------------------------Code starts here---------------------------------------------------------------------

    int t=1;
    //cin>>t;
    for(int test=1;test<=t;test++)
    {
        // cout<<"Case #"<<test<<": ";
        // cout<<endl;
        solve();
        // cout<<endl;

    }

    // -------------------------------------Code ends here------------------------------------------------------------------

    clk = clock() - clk;
    cerr << fixed << setprecision(6) << "Time: " << ((long double)clk)/CLOCKS_PER_SEC << "\n";
    return 0;
}
/*
1.do not use bitshift (1<<int) because it gives value in int and not in long long
2.check that for loops of i,j,k are properly declared
3.donot delete elements from map,set while looping in it
4.in a map's vector ,ie,map<int,vector<int>> mp,when you sort each vector in a loop it may not get sorted outside the scope of the loop so take care
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)
#define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x,t,f) ((x)?(t):(f))
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v  x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v  x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v))  //x is sorted
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd_(ll a,ll b){if(a%b==0)return b;return gcd_(b,a%b);}
ll lcm_(ll a,ll b){ll c=gcd_(a,b);return ((a/c)*b);}
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=ll(1e16)+ll(7);
const ll MOD=1000000007LL;
#define out(a) cout<<fixed<<setprecision((a))
//tie(a,b,c) = make_tuple(10,9,87);
#define pop_(a) __builtin_popcount((a))
ll keta(ll a){ll r=0;while(a){a/=10;r++;}return r;}
#define num_l(a,v) POSL(a,v) //v未満の要素数
#define num_eql(a,v) POSU(a,v) //v以下の要素数
#define num_h(a,v) (a.size() - POSU(a,v)) //vより大きい要素数
#define num_eqh(a,v) (a.size() - POSL(a,v)) //v以上の要素数

class dijkstra{
//0indexed
public:
	int v,startv;
	vector<int> d;
	vector<vector<pair<int,int> > >e;

	void initsize(int n0){
		d.resize(n0);
		for(int i=0;i<n0;i++)d[i]=INT_MAX;
		vector<pair<int,int> > ep;
		for(int i=0;i<n0;i++)e.push_back(ep);
		v=n0;
	}

	void initstart(int s){
		startv=s;
		for(int i=0;i<v;i++)d[i]=INT_MAX;
		d[s]=0;
	}

	void make_edge(int x,int y,int cost){
		e[x].push_back(make_pair(y,cost));
		e[y].push_back(make_pair(x,cost));
	}

	void make_edgedir(int x,int y,int cost){
		e[x].push_back(make_pair(y,cost));
	}

	void calcdistance(){
		// <-cost,x>
		//cost startv->x
		priority_queue<pair<int,int> > q;
		pair<int,int> p;
		vector<bool> did;
		for(int i=0;i<v;i++)did.push_back(false);
		did[startv]=true;
		for(int i=0;i<e[startv].size();i++){
			p.first=-e[startv][i].second;
			p.second=e[startv][i].first;
			q.push(p);
		}
		while(q.size()!=0){
			pair<int,int> p;
			p=q.top();
			q.pop();
			int x,costsx;
			x=p.second;
			costsx=-p.first;
			if(did[x]==true)continue;
			did[x]=true;
			d[x]=costsx;
			for(int i=0;i<e[x].size();i++){
				p.first=-(d[x]+e[x][i].second);
				p.second=e[x][i].first;
				if(did[p.second]==false)q.push(p);
			}
		}
	}
};

int main(){
	ll N,M;
	cin >> N >> M;
	vector< vector<pair<int,int>> > G(N);
	dijkstra di;
	di.initsize(N+1);//st ==> N

	FOR(i,0,M){
		int a,b,c;
		cin >> a >> b >> c;
		G[a-1].push_back({b-1,c});
		di.make_edgedir(a-1,b-1,c);
	}

	FOR(i,0,N){
		di.e[N] = G[i];
		di.initstart(N);
		di.calcdistance();
		int ans = di.d[i];
		if(ans == INT_MAX) ans = -1;
		cout << ans << endl;
	}

}

#include<bits/stdc++.h>
#define M(x,y) make_pair(x,y)
#define ll long long
using namespace std;
struct node{
	int u,to,next,val;
};node e[4005];
int head[2005],dist[2005],vis[2005],dis[2005][2005],to[2005][2005],sum[2005],b[2005][2005];
int n,m,cnt=0,inf=999999999;
void add(int u,int v,int val){
	++cnt;
	e[cnt].u=u;
	e[cnt].to=v;
	e[cnt].val=val;
	to[v][++sum[v]]=u;
	e[cnt].next=head[u];
	head[u]=cnt;
	b[u][v]=min(b[u][v],val);
}
struct nd{
	int id,dis;
//	nd() {}
	nd(int _id, int _dis) : id(_id), dis(_dis) {}
	bool friend operator < (nd a,nd b){
		return a.dis<b.dis;
	}
};
priority_queue< pair<int,int> > q;
void dijstra(int s){
	for(int i=1;i<=n;i++) dis[s][i]=inf;
	memset(vis,0,sizeof(vis));
	dis[s][s]=0;
	q.push(M(0,s));
	while(!q.empty()){
        int u=q.top().second;
        q.pop();
        if(vis[u]) continue;
        vis[u]=1;
        for(int i=head[u];i;i=e[i].next){
            int v=e[i].to;
            if(dis[s][v]>dis[s][u]+e[i].val){
                dis[s][v]=dis[s][u]+e[i].val;
                q.push(M(-dis[s][v],v));
            }
        }
    }
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			b[i][j]=inf;
		}
	}
	for(int i=1;i<=m;i++){
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		add(x,y,z);
	}
	for(int i=1;i<=n;i++){
		dijstra(i);
	}
	//cout<<"sss";
	/*for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cout<<dis[i][j]<<" ";
		}
		cout<<endl;
	}*/
	for(int i=1;i<=n;i++){
		int ans=inf;
		for(int j=1;j<=sum[i];j++){
			if(dis[i][to[i][j]]!=inf){
				ans=min(ans,dis[i][to[i][j]]+b[to[i][j]][i]);
			}
		}
		if(ans!=inf){
			cout<<ans<<endl;
		}
		else{
			cout<<"-1"<<endl;
		}
	}
}


#include<iostream>
#include<cmath>
using namespace std;

int main() {
	long long int n, k;
	cin >> n >> k;
	long long int ab = 2 * n;
	long long int res = 0;
	while (ab >= 2) {
		int cd = ab - k;
		if (cd >= 2 && cd <= 2 * n) {
			res += (n - abs(n + 1 - ab)) * (n - abs(n + 1 - cd));
		}
		ab--;
	}
	cout << res << endl;
	return 0;
}

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <utility>
#include <set>
using namespace std;
typedef pair<int, int> pairII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef long long LL;
#define shig_for(i, a, b) for(int i = (a); i < (b); ++i)
#define shig_rep(i, n) shig_for(i, 0, n)
#define shig_forB(bit,a,b) for(int bit = (a); bit < (1<<(b)); ++bit)
#define shig_repB(bit,n) shig_forB(bit,0,n)
#define tmp int temp = 0
#define str string s

int main(){

    LL N, K, ans = 0;
    const LL zero = 0;
    cin >> N >> K;

    for(LL i = 2; i <= 2 * N; i++){

        if(i - K >= 2){
            //ans += (i - 1);
            //ans += (i - K - 1);
            LL tempA = -1 * (((i - N) - 1) * 2);
            LL tempB = -1 * (((i - K - N) - 1) * 2);
            tempA = min(zero, tempA);
            tempB = min(zero, tempB);
            tempA = max(zero, ((i - 1) + tempA));
            tempB = max(zero, ((i - K - 1) + tempB));

            //ans += ((i - 1) + min(zero, tempA) ) * ((i - K - 1) + min(zero, tempB) );
            ans += tempA * tempB;

        }

    }

    cout << ans << endl;

    //cout << "Atcoder" << endl;

    return 0;
}


#include<bits/stdc++.h>
#define st first
#define nd second
#define mp make_pair
#ifndef LOCAL
#define cerr if(0)cerr
#endif
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using pii = pair<int, int>;

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n; cin>>n;
	vector<int> a(2*n); for(int& i: a) cin>>i;
	ll ans = 0;
	for(int i: a) ans += i;
	priority_queue<int> pq;
	for(int i=0; i<n; ++i) {
		pq.push(-(a[n-i-1]));
		pq.push(-(a[n+i]));

		ans += pq.top();
		pq.pop();
	}
	cout<<ans<<'\n';
}

/**
*    author:  shu8Cream
*    created: 19.12.2020 21:08:37
**/

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<(n); i++)
using ll = long long;
using P = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using Graph = vvi;

int main() {
    int n;
    cin >> n;
    vector<ll> a(n);
    rep(i,n) cin >> a[i];
    sort(a.begin(), a.end());
    ll ans = 0;
    int j=0;
    for(int i=n-1; i>0; i-=2){
        ans+=abs(a[j]-a[n-1-j])*i;
        j++;
    }
    cout << ans << endl;
}


/**
 *  created: 16/01/2021, 14:27:24
**/

#include <bits/stdc++.h>

using namespace std;

const int max_n = 1111, inf = 1000111222;

int n, cnt[max_n][max_n];
vector<string> ans;

int c2(int n) {
    return n * (n - 1) / 2;
}

bool get_bit(int mask, int pos) {
    return (mask >> pos) & 1;
}

void check() {
    for (string s : ans) {
        if (!(s.length() == (1 << n))) {
            cout << s.length() << endl;
            cout << (1 << n) << endl;
            exit(2);
        }
        for (int i = 0; i < s.length(); ++i) {
            for (int j = i + 1; j < s.length(); ++j) {
                cnt[i][j] += s[i] == s[j];
            }
        }
    }
    for (int i = 0; i < (1 << n); ++i) {
        for (int j = i + 1; j < (1 << n); ++j) {
            cout << cnt[i][j] << " ";
        }
        cout << endl;
    }
}

vector<string> solve(int n) {
    if (n == 1) {
        return {"AB"};
    }
    vector<string> v = solve(n - 1);
    vector<string> res;
    for (string s : v) {
        string t = s;
        for (int i = 0; i < t.size(); ++i) {
            t[i] ^= 'A' ^ 'B';
        }
        res.push_back(s + s);
        res.push_back(s + t);
    }
    string s;
    for (int i = 0; i < (1 << (n - 1)); ++i) {
        s += 'A';
    }
    for (int i = 0; i < (1 << (n - 1)); ++i) {
        s += 'B';
    }
    res.push_back(s);
    return res;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    cin >> n;
    /*int a = c2(1 << (n - 1)) * 2;
    int b = c2(1 << n);
    int lc = a / __gcd(a, b) * b;
    cout << a << " " << b << " " << lc << endl << lc / a << endl;*/
    /*
    ans.push_back("AABB");
    ans.push_back("ABAB");
    ans.push_back("ABBA");
    */

    ans = solve(n);

    /*ans.push_back("AAAABBBB");

    ans.push_back("AABBAABB");
    ans.push_back("ABABABAB");
    ans.push_back("ABBAABBA");

    ans.push_back("AABBBBAA");
    ans.push_back("ABABBABA");
    ans.push_back("ABBABAAB");*/

    cout << ans.size() << "\n";
    for (auto s : ans) {
        cout << s << "\n";
    }

    //check();

    return 0;
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
int N,M,a[1100][1100];
signed main(){
  cin>>N;M=(1<<N);
  cout<<M-1<<endl;
  for(int i=1;i<M;i++){
    string ans(M,'A');
    for(int k=0;k<M;k++)if(__builtin_popcount(i&k)&1)ans[k]='B';
    cout<<ans<<endl;
  }
}


#include <set>
#include <map>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

inline int rd() {
    int x = 0;
    bool f = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) f |= (c == '-');
    for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
    return f ? -x : x;
}

int main() {
    int n = rd();
    int m = rd();
    map<int, vector<int> > a;
    for (int i = 1; i <= m; ++i) a[rd()].push_back(rd());
    set<int> f{n};
    for (auto v : a) {
        vector<int> add, del;
        for (auto y : v.second)
            if (f.count(y - 1) || f.count(y + 1)) add.push_back(y);
            else del.push_back(y);
        for (auto y : add) f.insert(y);
        for (auto y : del) f.erase(y);
    }
    printf("%d\n", f.size());
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int, int>;

/* [💭] [💡] [🎈] */

const ll inf = 1e18;
using pll = pair<ll, ll>;

ll dist[100001];

struct edge {
    int to;
    ll c, d;

    edge() : to(0), c(0), d(0) {}

    edge(int to, ll c, ll d) : to(to), c(c), d(d) {}
};

ll f(ll t, ll c, ll d) {
    return t + c + d / (t + 1);
}

ll mnt(ll dt, ll c, ll d) {
    ll mn = inf;
    ld ta = max(dt, (ll) sqrt(d) - 1);
    for (ll x = ta; x <= ta + 10; x++) {
        mn = min(mn, f(x, c, d));
    }
    return mn;
}

int main() {
    cin.tie(nullptr), cout.tie(nullptr), ios::sync_with_stdio(false);

#ifdef _SHIFTPSH
    freopen("_run/in.txt", "r", stdin), freopen("_run/out.txt", "w", stdout);
#endif

    fill(dist, dist + 100001, inf);

    int n, m;
    cin >> n >> m;

    vector<vector<edge>> graph(n + 1);
    while (m--) {
        int u, v, c, d;
        cin >> u >> v >> c >> d;
        graph[u].emplace_back(v, c, d);
        graph[v].emplace_back(u, c, d);
    }

    dist[1] = 0;
    priority_queue<pll> pq;
    pq.emplace(0, 1);
    while (pq.size()) {
        ll c = -pq.top().first, u = pq.top().second;
        pq.pop();
        if (dist[u] < c) continue;

        for (const auto &e : graph[u]) {
            ll d = mnt(c, e.c, e.d);
            if (dist[e.to] <= d) continue;
            dist[e.to] = d;
            pq.emplace(-d, e.to);
        }
    }

    cout << (dist[n] == inf ? -1ll : dist[n]);

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;

inline int read()
{
    int X=0; bool flag=1; char ch=getchar();
    while(ch<'0'|ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}

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

int row,col,n,m;
map<PII,int> mp;
int b[1510][1510];
bool st[1510][1510];

int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>row>>col>>n>>m;
    for(int i=1;i<=n;++i){
        int x,y;
        cin>>x>>y;
        mp[make_pair(x,y)]++;
    }

    for(int i=1;i<=m;++i){
        int x,y;
        cin>>x>>y;
        b[x][y]++;
    }

    int ans=0;
    for(auto w:mp){
        int x=w.fi.fi;
        int y=w.fi.se;
        for(int i=y;i>=1;--i){
            if(st[x][i]) continue;
            if(b[x][i]) break;
            st[x][i]=true;
            ans++;
        }
        for(int i=y+1;i<=col;++i){
            if(st[x][i]) continue;
            if(b[x][i]) break;
            st[x][i]=true;
            ans++;
        }
        for(int i=x;i>=1;--i){
            if(st[i][y]) continue;
            if(b[i][y]) break;
            st[i][y]=true;
            ans++;
        }
        for(int i=x+1;i<=row;++i){
            if(st[i][y]) continue;
            if(b[i][y]) break;
            st[i][y]=true;
            ans++;
        }
    }

    cout<<ans;

    return 0;
}


// Disclaimer: Don't copy my template, it may lead to plagiarism.
/* Author: Soumy Jain
   Handle: soumy_jain || soumyjain
   "Beautiful flowers too, eventually wither and fall. That's the fate of all living beings."
   "I hate perfection. To be perfect is to be unable to improve any furthur." - Mayuri Kurotsuchi | Bleach
   "I smile to show the pressure of heroes and to trick the fear inside of me."
   "Gravel may be gravel, but me? I'm the gravel that shatters diamonds."
   "If you were to write a story with me in the lead role, it would certainly be...a TRAGEDY." - Kaneki Ken | Tokyo
   Ghoul
*/
/******************************************************************************/
// clang-format off
#include <bits/stdc++.h>
#define ll        long long
#define ull       unsigned long long
#define SPEEDHACK ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ff        first
#define ss        second
#define sz(v)     (ll)(v).size()
#define file      freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define MOD       1000000007      // 998244353
using namespace std;
/******************************************************************************/
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(ll x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(ull x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template<typename T, typename V>
void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; }
template<typename T>
void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; }
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); }
#ifndef ONLINE_JUDGE
#define dbg(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
// clang-format on

/***********************************MAIN***************************************/
// Are you ready to face the wrath of test cases? Good luck noob Soumy!

int minDifference(vector<ll> &arr, int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += arr[i];
    bool dp[n + 1][sum / 2 + 1];
    for (int i = 0; i <= n; i++)
    {
        dp[i][0] = true;
    }
    for (int i = 1; i <= sum / 2; i++)
    {
        dp[0][i] = false;
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= sum / 2; j++)
        {
            if (arr[i - 1] <= j)
                dp[i][j] = dp[i - 1][j - arr[i - 1]] || dp[i - 1][j];
            else
                dp[i][j] = dp[i - 1][j];
        }
    }
    int mn = INT_MAX;
    for (int i = 1; i <= sum / 2; i++)
    {
        if (dp[n][i])
            mn = min(mn, sum - 2 * i);
    }
    return mn == INT_MAX ? arr[0] : mn;
}

void work()
{
    ll n, sum = 0;
    cin >> n;
    vector<ll> v(n);
    for (auto &i : v)
        cin >> i, sum += i;
    ll x = minDifference(v, n);
    cout << (minDifference(v, n) + sum) / 2;
}

int main()
{
    SPEEDHACK
    // file
    ll t = 1;
    // cin >> t;
    while (t--)
    {
        work();
    }
    return 0;
}

#include<iostream>
#include<math.h>
#include<bits/stdc++.h>
#include<cmath>
#define ll long long int
#define hell 1000000007LL
using namespace std;
ll power(ll x, ll y,ll z){

    if(y==0)
     return 1;
    else
    {
        ll p=power(x,y/2,z);
        ll w=(p*p)%z;
        if(y%2==0)
         return w;
        else
        return (x*w)%z;
    }}
ll gcd(ll x,ll y)
{
    if(y==0)
     return x;
    else
    return gcd(y,x%y);}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(20);
   ll n;
   cin>>n;
   ll a[n],s=0;
   for(int i=0;i<n;i++)
   {
       cin>>a[i];
       s=s+a[i];
   }
   bool f[n+1][s+1];
   for (int i = 0; i <= n; i++)
        f[i][0] = true;
    for (int i = 1; i <= s; i++)
        f[0][i] = false;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= s; j++) {
            if (j < a[i - 1])
                f[i][j] = f[i - 1][j];
            if (j >= a[i - 1])
                f[i][j] = f[i - 1][j]
                               || f[i - 1][j - a[i - 1]];
        }
    }
    int b[100001];
    ll m=s;
    for(int i=1;i<s;i++)
    {
        int c=0;
        for(int j=0;j<n+1;j++)
        {
            if(f[j][i]==true){
            c++;
            break;}
        }
        if(c)
        b[i]=1;
    }
    for(ll i=1;i<s;i++)
    {
        if(b[i]==1)
        {
            ll x=i;
            if(x<s-i)
            x=s-i;
            m=min(m,x);
        }
    }
    cout<<m;}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

typedef pair<int, int> P;

class EAkari {
public:
    void solve(std::istream& cin, std::ostream& cout) {
        int H, W, N, M;
        cin >> H >> W >> N >> M;
        vector<int> A(N), B(N), C(M), D(M);
        vector<vector<int>> block(H, vector<int>(W));
        REP(i, N) {
            cin >> A[i] >> B[i];
            A[i]--, B[i]--;
        }
        REP(i, M) {
            cin >> C[i] >> D[i];
            C[i]--, D[i]--;
            block[C[i]][D[i]] = 1;
        }

        vector<set<pair<int, int>>> sh(W), sw(H);
        REP(i, N) {
            sh[B[i]].insert({A[i], 1});
            sw[A[i]].insert({B[i], 1});
        }
        REP(i, M) {
            sh[D[i]].insert({C[i], 2});
            sw[C[i]].insert({D[i], 2});
        }

        int res = 0;
        REP(i, H) {
            REP(j, W) {
                if (block[i][j]) continue;
                {
                    auto itr = sh[j].upper_bound({i, 0});
                    if (itr != sh[j].end()) {
                        if (itr->second == 1) {
                            res++;
                            continue;
                        }
                    }
                    if (itr != sh[j].begin()) {
                        itr--;
                        if (itr->second == 1) {
                            res++;
                            continue;
                        }
                    }
                }
                {
                    auto itr = sw[i].upper_bound({j, 0});
                    if (itr != sw[i].end()) {
                        if (itr->second == 1) {
                            res++;
                            continue;
                        }
                    }
                    if (itr != sw[i].begin()) {
                        itr--;
                        if (itr->second == 1) {
                            res++;
                            continue;
                        }
                    }
                }
            }
        }
        cout << res << endl;
    }
};

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    std::istream& in(std::cin);
    std::ostream& out(std::cout);

    EAkari solver;
    solver.solve(in, out);
    return 0;
}

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

void Main()
{
    int h, w, n, m;
    cin >> h >> w >> n >> m;
    vector<vector<int>> b(h, vector<int>(w));
    vector<vector<int>> l(h, vector<int>(w));
    vector<vector<int>> f(h, vector<int>(w));
    for (int i = 0; i < n; i++)
    {
        int y, x;
        cin >> y >> x;
        y--; x--;
        l[y][x] = 1;
    }
    for (int i = 0; i < m; i++)
    {
        int y, x;
        cin >> y >> x;
        y--; x--;
        b[y][x] = 1;
    }
    bool on = false;
    int ans = 0;
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            if (b[i][j] == 1) on = false;
            if (l[i][j] == 1) on = true;
            if (on && f[i][j] == 0) {
                f[i][j] = 1;
                ans++;
            }
        }
        on = false;
        for (int j = w-1; j >= 0; j--)
        {
            if (b[i][j] == 1) on = false;
            if (l[i][j] == 1) on = true;
            if (on && f[i][j] == 0) {
                f[i][j] = 1;
                ans++;
            }
        }
        on = false;
    }
    for (int j = 0; j < w; j++)
    {
        for (int i = 0; i < h; i++)
        {
            if (b[i][j] == 1) on = false;
            if (l[i][j] == 1) on = true;
            if (on && f[i][j] == 0) {
                f[i][j] = 1;
                ans++;
            }
        }
        on = false;
        for (int i = h-1; i >= 0; i--)
        {
            if (b[i][j] == 1) on = false;
            if (l[i][j] == 1) on = true;
            if (on && f[i][j] == 0) {
                f[i][j] = 1;
                ans++;
            }
        }
        on = false;
    }

    // ofstream ofs("output.txt");
    // for (int i = 0; i < h; i++)
    // {
    //     for (int j = 0; j < w; j++)
    //     {
    //         if (l[i][j]) ofs << "*";
    //         else if (b[i][j]) ofs << "@";
    //         else ofs << f[i][j];
    //     }
    //     ofs << "\n";
    // }
    // ofs << "\n";

    cout << ans << endl;
}

int main(int argc, char **argv)
{
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    Main();
    return 0;
}


#include <bits/stdc++.h>

#define rep(i,n) for(ll i=0;i<n;++i)
#define rrep(i, n) for(ll i=n-1;i>=0;--i)
#define rep1(i, n) for(ll i=1; i<=n; ++i)
#define repitr(itr,mp) for(auto itr=mp.begin();itr!=mp.end();itr++)
#define ALL(a) (a).begin(),(a).end()

template<class T> void chmax(T &a, const T &b){if(a < b){a = b;}}
template<class T> void chmin(T &a, const T &b){if(a > b){a = b;}}

using namespace std;
using ll  = long long;
using ull = unsigned long long;
using pll = pair<ll, ll>;

const ll MOD  = 1e9 + 7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;
const double PI = 3.1415926535897932384626433;

int main(){
    ll n;
    cin >> n;

    rep1(a, 39)rep1(b, 27){
        ull pa, pb;
        pa = pb = 1;

        rep(i, a)pa *= 3;
        rep(i, b)pb *= 5;

        if(pa + pb == n){
            cout << a << " " << b << endl;
            return 0;
        }
    }

    cout << -1 << endl;
}

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

#define REP(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for(int (i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for(int (i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
constexpr ll INFL = 1LL<<61;
constexpr ll mod = 1e9+7;

int main(){
  int N;
  cin >> N;
  vector<int> p(N);
  priority_queue<int, vector<int>, greater<int>> pq;
  REP(i, N){
    cin >> p[i];
  }
  int maxi = 200000;
  REP(i, maxi+1){
    pq.push(i);
  }
  set<int> st;
  REP(i, N){
    st.insert(p[i]);
    while(st.find(pq.top()) != st.end()){
      pq.pop();
    }
    cout << pq.top() << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int 					long long
//#define double 					long double
#define max(x,y) 				(((x)>(y))?(x):(y))
#define min(x,y) 				(((x)<(y))?(x):(y))
#define all(v) 					v.begin(),v.end()
#define vi 						vector<int>
#define MOD 					1000000007
#define inf 					1e18
#define ps(num,precision) 		fixed<<setprecision(precision)<<num
#define md(arr,n,type) 			type *arr=new type[n]

void inout(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif
}

int gcd(int a,int b){
    if(b==0)return a;
    return gcd(b,a%b);
}

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

void SieveOfEratosthenes(int n){
	bool prime[n+1];
	memset(prime,true,sizeof(prime));
	for(int p=2;p*p<=n;p++){
		if(prime[p]){
			for(int i=p*p;i<=n;i+=p){
				prime[i]=false;
			}
		}
	}

	for(int i=2;i<=n;i++){
		if(prime[i]){
		}
	}
}
int printDivisors(int n)
{
	int sum=0;
    for (int i=1; i<=sqrt(n); i++)
    {
        if (n%i == 0)
        {
            if (n/i == i)
                sum+=1;
            else{
                sum+=2;
            }
        }
    }
    return sum;
}

void solve(){
    int n;
	cin>>n;
	int a[n];
	int min;
	int ans=0;
	int con=0;
	vector<pair<int,int>> b;
	for(int i=0;i<n;i++){
		cin>>a[i];
		b.push_back(make_pair(a[i],i));
	}
	sort(all(b));
	for(int i=0;i<n;i++){
		min=a[i];
		if(ans<a[i]){
			ans=a[i];
		}
		for(int j=i+1;j<n;j++){
			if(min>a[j]){
				min=a[j];
			}
			if((j-i+1)*min>ans){
				ans=(j-i+1)*min;
			}
		}
	}
	cout<<ans;
}
int32_t main(){
	//inout();
	int t=1;
	//cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <random>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define repp(i,n,m) for (int i = m; i < (n); ++i)
#define repl(i,n) for (long long i = 0; i < (n); ++i)
#define reppl(i,n,m) for (long long i = m; i < (n); ++i)
//#define int long long
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PI = pair<pair<int,int>,int>;
using PL = pair<long long, long long>;
using PLL = pair<pair<long long, long long>, long long>;
using Pxy = pair<double, double>;
using Tiib = tuple<int, int, bool>;
const int INF = 1001001007;
const int modd = 1000000007;
const long long modl = 1000000007LL;
const int mod = 998244353;
const ll inf = 1e18;
template <typename AT>
void printvec(vector<AT> &ar){
    rep(i,ar.size()-1) cout << ar[i] << " ";
    cout << ar[ar.size()-1] << endl;
}
template <typename Q>
void printvvec(vector<vector<Q>> &ar){
    rep(i,ar.size()){
        rep(j,ar[0].size()-1) cout << ar[i][j] << " ";
        cout << ar[i][ar[0].size()-1] << endl;
    }
}
template <typename S>
bool range(S a, S b, S x){
    return (a <= x && x < b);
}

void yes(){
    cout << "Yes" << endl;
}

void no (){
    cout << "No" << endl;
}

ll cel (ll a, ll b){
    if (a % b == 0) return a / b;
    else return a / b + 1;
}

ll gcds(ll a, ll b){
    ll c = a % b;
    while (c != 0){
        a = b;
        b = c;
        c = a % b;
    }
    return b;
}

struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};

mint f(int n) {
  if (n == 0) return 1;
  mint x = f(n/2);
  x *= x;
  if (n%2 == 1) x *= 2;
  return x;
}

mint choose(int n, int a) {
  mint x = 1, y = 1;
  rep(i,a) {
    x *= n-i;
    y *= i+1;
  }
  return x / y;
}

int main(){
    int n, k; cin >> n >> k;
    vector<mint> ar(n,0LL);
    rep(i,n){
        ll a; cin >> a;
        mint b = a;
        ar[i] += b;
    }
    vector<mint> tab(k+1);
    vector<mint> gya(k+1,1LL);
    vector<mint> ars(n,1LL);
    rep(i,k){
        mint c = ll(i + 1);
        gya[i+1] = gya[i] / c;
    }
    rep(i,k+1){
        mint c = 0LL;
        rep(j,n){
            c += ars[j];
            ars[j] *= ar[j];
        }
        tab[i] = c * gya[i];
    }
    mint kai = 1LL;
    mint ni = 2LL;
    repp(i,k+1,1){
        mint x = ll(i);
        kai *= x;
        mint ans = 0LL;
        rep(j,i+1){
            ans += tab[j] * tab[i-j];
        }
        ans *= kai;
        ans -= f(i) * tab[i] * kai;
        ans /= ni;
        cout << ans.x << endl;
    }
}


#include <bits/stdc++.h>

const int INF  = 1e9;
const long long INFLL = 1e18;
const int NMAX = 100005;
const int MMAX = 100005;
const int KMAX = 1005;
const int MOD  = 1e9 + 7;
using namespace std;

// comment to disable debug functions
#define DEBUG

// frequently used macros

#define FOR(i, N) for(int (i) = 0; (i) < (N); ++(i))
#define FOR2(i, j, N, M) for(int (i) = 0; (i) < (N); ++(i)) \
  for(int (j) = 0; (j) < (M); ++(j))

#if __cplusplus >= 201103L
#define ALL(v) begin(v),end(v)
#define SORT(v) sort(begin(v), end(v))
#define FIND(v,x) find(begin(v), end(v), (x))
#else
#define ALL(v) (v).begin(),(v).end()
#define SORT(v) sort(v.begin(), v.end())
#define FIND(v,x) find(v.begin(), v.end(), (x))
#endif

#define fi first
#define se second

#define MEMNEXT(from, to) do{ memmove((to), (from), sizeof(from)); \
memset((from), 0, sizeof(from)); } while(0)
#ifdef DEBUG
#define DUMP(x) do{ std::cerr << (#x) << ": " << x << std::endl; }while(0)
#else
#define DUMP(x) do{}while(0)
#endif

// frequent used aliases
typedef long long ll;
typedef pair<int, int> p;
typedef pair<ll, int> lp;
typedef pair<ll, ll> llp;
typedef vector<int> vec;
typedef vector<ll> vecll;
typedef vector<vec> mat;
typedef vector<vecll> matll;

// frequently used constants
static const int di[] = {-1, 0, 1, -1, 1, -1, 0, 1};
static const int dj[] = {-1, -1, -1, 0, 0, 1, 1, 1};

// frequently used structs
struct edge{
  int to,cost;
};

// printf for debug
#ifndef DEBUG
void debug(const char* format, ...){}
#else
void debug(const char* format, ...){
  va_list arg;
  va_start(arg, format);
  vprintf(format, arg);
  va_end(arg);
}
#endif

// dump vector
#ifdef DEBUG
#define DUMPV(v, c) do{       \
  printf("%s: ", #v);         \
  for (int i = 0; i < (c); ++i) \
  {                           \
  cout << (v)[i] << " ";      \
  }                           \
  cout << endl;               \
} while(0)
#else
#define DUMPV(v,c)
#endif

// std::fill of multi dimensions
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
  std::fill( reinterpret_cast<T*>(array),
             reinterpret_cast<T*>(array+N),
             val );
}

// binary search
ll BSearch(ll ok, ll ng, bool (*f)(ll)){
  ll mid;
  while(abs(ok - ng) > 1LL) {
    mid =  (ok + ng) / 2LL;
    if(f(mid)) {
      debug("BSearch: f(%d) == true\n", mid);
      ok = mid;
    }
    else
    {
      debug("BSearch: f(%d) == false\n", mid);
      ng = mid;
    }
  }
  return ok;
}

ll NUM_TEST_CASE = 1;

ll N,M,K,A,B,C,D,E;
int dp[NMAX] = {};

string S;
vec v;

ll ans = {};

void solve(){
  // main algorithm

}
void debug(){
  // output debug information

}
void answer(){
  // output answer
  cout << N - M + K << endl;
}
void init(){
  // initialize for each test case
  // Fill(dp, -1);
}
int main(int argc, char const *argv[])
{
  // operate inputs

  // cin >> NUM_TEST_CASE;

  for (int test_case = 0; test_case < NUM_TEST_CASE; ++test_case)
  {
    init();

    cin >> N >> M >> K;
    solve();
    #ifdef DEBUG
    debug();
    #endif
    answer();
  }

  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main(){
    int n,a,b;
    cin >>n >>a >>b;
    cout <<n-a+b;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pint pair<int, int>
#define pll pair<ll, ll>
const ll INF = 1LL << 60;
const int MOD = 1000000007;
// const int MOD = 998244353;

int main()
{
    ll n, C;
    cin >> n >> C;

    // time, cost
    map<ll, ll> mp;
    for (ll i = 0; i < n; i++)
    {
        ll a, b, c;
        cin >> a >> b >> c;
        mp[a] += c;
        mp[b + 1] -= c;
    }
    mp[0] = 0;

    ll nowCost = 0;
    ll prevTime = 0;
    ll ans = 0;
    for (auto m : mp)
    {
        ll time = m.first;
        ll cost = m.second;
        ans += min(C, nowCost) * (time - prevTime);
        nowCost += cost;
        prevTime = time;
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
using ll  = long long;
using vi  = vector<int>;
using ull = unsigned long long;
#define pb push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=int(a);i<=(int)(b);i++)
#define per(i,a,b) for(int i=int(a);i>=(int)(b);i--)
const int mod  = 1e9+7;
const int inf  = 0x3f3f3f3f;
const int maxn = 1e6+10;
int main() {
#ifndef WA_DEBUG
    ios::sync_with_stdio(false);cin.tie(nullptr);
#endif
    int n;
    ll c;
    cin>>n>>c;
    vector<pair<int,int>> a(2*n+1);
    int cnt=0;
    rep(i,1,n) {
        int l,r,c;
        cin>>l>>r>>c;
        a[++cnt].fi=l;
        a[cnt].se=c;
        a[++cnt].fi=r+1;
        a[cnt].se=-c;
    }
    sort(a.begin()+1,a.end());
    ll sum=0,now=0;
    rep(i,1,cnt-1) {
        now+=a[i].se;
        if(now<c) sum+=now*(a[i+1].fi-a[i].fi);
        else sum+=c*(a[i+1].fi-a[i].fi);
    }
    cout<<sum<<'\n';
    return 0;
}


#include<bits/stdc++.h>
#include<tr1/unordered_map>
using namespace std;
#define int long long
int n;
int a[300000];
int dp[300000];
const int mod=998244353;
tr1::unordered_map<int,int>num,vis;
signed main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i],num[a[i]]++;
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)
    {
        dp[i]=dp[i-1]*2;
        dp[i]+=a[i];
        dp[i]%=mod;
    }int ans=0;
    for(int i=n;i>=1;i--)
    {
        ans+=a[i]*dp[i-1]%mod+a[i]*a[i]%mod;ans%=mod;
        //cout<<a[i]<<" "<<dp[i-1]<<endl;
    }
    cout<<ans<<endl;
}

#include<bits/stdc++.h>
#define L(i, j, k) for(int i = j, i##E = k; i <= i##E; i++)
#define R(i, j, k) for(int i = j, i##E = k; i >= i##E; i--)
#define ll long long
#define pii pair<int, int>
#define db double
#define x first
#define y second
#define ull unsigned long long
#define sz(a) ((int) (a).size())
using namespace std;
const int N = 1e6 + 7;
const int mod = 998244353, inv2 = (mod + 1) / 2;
int n, a[N], pw[N], ipw[N], sum[N], ns;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n;
	pw[0] = ipw[0] = 1;
	L(i, 1, n + 1) pw[i] = (ll) pw[i - 1] * 2 % mod, ipw[i] = (ll) ipw[i - 1] * inv2 % mod;
	L(i, 1, n) cin >> a[i], (ns += (ll) a[i] * a[i] % mod) %= mod;
	sort(a + 1, a + n + 1);
	R(i, n, 1) sum[i] = (sum[i + 1] + (ll) a[i] * pw[i] % mod) % mod;
	L(i, 1, n) (ns += (ll) a[i] * sum[i + 1] % mod * ipw[i + 1] % mod) %= mod;
	cout << ns << "\n";
	return 0;
}


#include <bits/stdc++.h>
#include <iostream>
using namespace std;
//#define int long long int
#define out2(x,y) cout<<x<<" "<<y<<endl;
#define out(x) cout<<x<<endl;
#define forin for(int i=0;i<n;i++)
#define forjm for(int j=0;j<m;j++)
#define in(x) int x;cin>>x;
#define in2(x,y) int x,y;cin>>x>>y;
#define in3(x,y,z) int x,y,z;cin>>x>>y>>z;
#define MOD 1000000007

int32_t main()
{
    in2(a,b);
    int can=2*a+100;
    int ans=-b+can;
    out(ans);

    return 0;
}

/*
START BY THE NAME OF ALMIGHTY ALLAH
THIS WONT BE ACCEPTED
STOP_GIVING_UP

██╗███████╗████████╗██╗ █████╗ ██╗  ██╗
██║██╔════╝╚══██╔══╝██║██╔══██╗██║ ██╔╝
██║███████╗   ██║   ██║███████║█████╔╝
██║╚════██║   ██║   ██║██╔══██║██╔═██╗
██║███████║   ██║   ██║██║  ██║██║  ██╗
╚═╝╚══════╝   ╚═╝   ╚═╝╚═╝  ╚═╝╚═╝  ╚═╝

*/
#include<bits/stdc++.h>
#define l(i,a,n)for(int i=a;i<n;i++)
#define pb push_back
#define in insert
#define mp make_pair
#define lw(v) sort(v.begin(),v.end());
#define hi(v) sort(v.begin(),v.end(),greater<long long>());
#define all(v) v.begin(),v.end()
#define filein freopen ("input.txt", "r", stdin)
#define fileout freopen ("output.txt", "w", stdout)
using namespace std;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    long long  t,r=1,r1=0,r2=0,k=0,a,b,c=0,m,d=0,n,e,f,x=0,g,p=0,q=0,y=0,z=0;
    vector<long long>v;
    vector<long long>u;
    vector<tuple<string,int,
           int>>mp;
    set<long long>s;
    std::vector<int>::iterator it;
    string  s1,s2,s3,s4;
    cin>>n>>m;
    cout<<((2*n)+100)-m<<endl;

}


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

int main() {
	int n,m,q;
	cin>>n>>m>>q;
	vector<pair<int,int>>v;
	vector<pair<int,int>>w;
	int a,b;
	for(int i=0;i<n;i++){
	    cin>>a>>b;
	    v.push_back(make_pair(b,a));
	    w.push_back(make_pair(b,a));
	}
	sort(v.rbegin(),v.rend());
	sort(w.rbegin(),w.rend());
	int arr[m];
	for(int i=0;i<m;i++){
	    cin>>arr[i];
	}
	//sort(arr,arr+m);
	int l,r;
	for(int i=1;i<=q;i++){
	    cin>>l>>r;
	    l--;
	    r--;
	    vector<int>vv;
	    for(int j=0;j<m;j++){
	        if(j>=l && j<=r){
	            continue;
	        }
	        vv.push_back(arr[j]);
	    }
	    sort(vv.begin(),vv.end());
	    for(int j=0;j<vv.size();j++){
	       // cout<<"#"<<vv[j]<<"\n";
	    }
	    vector<int>::iterator lower;
	    long long ans=0;
	    for(int j=0;j<v.size();j++){
	        if(v[j].first!=-1){
	            int r=v[j].second;
	       lower=lower_bound(vv.begin(),vv.end(),r);
	       if((lower - vv.begin() + 1)<=vv.size()){
	           ans=ans+v[j].first;
	           v[j].first=-1;
	           vv[lower-vv.begin()]=-1;
	           sort(vv.begin(),vv.end());
	       }
	        }
	    }
	    cout<<ans<<"\n";
	    for(int j=0;j<n;j++){
	        if(v[j].first==-1){
	            v[j].first=w[j].first;
	        }
	    }
	}
	return 0;
}

/*
  Problem Name:Graph Smoothing
  algorithm tag:矩阵快速幂
*/

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-4;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//#define x first
//#define y second
#define iosf ios::sync_with_stdio(false), cin.tie(0), cout << fixed

const int N = 110;

typedef long long ll;

int n;

struct Matrix
{
    int a[N][N];
    Matrix() { memset(a, 0, sizeof(a)); }
    int *operator[](int x)
    {
        return a[x];
    }
    Matrix operator*(Matrix b)
    {
        Matrix res;
        for (int k = 1; k <= n; k++)
            for (int i = 1; i <= n; i++)
                for (int j = 1; j <= n; j++)
                    res[i][j] = (res[i][j] + (ll)a[i][k] * b[k][j]) % mod;
        return res;
    }
} E;

Matrix Pow(Matrix x, int y)
{
    Matrix res = E;
    for (; y; y >>= 1, x = x * x)
        if (y & 1)
            res = res * x;
    return res;
}

int Pow(int x, int y)
{
    int res = 1;
    for (; y; y >>= 1, x = (ll)x * x % mod)
        if (y & 1)
            res = (ll)res * x % mod;
    return res;
}

int ind[N], A[N];

int main()
{
    int m, k;
    scanf("%d%d%d", &n, &m, &k);
    int inv = Pow(2 * m, mod - 2);
    for (int i = 1; i <= n; i++)
        scanf("%d", &A[i]);
    for (int i = 1; i <= n; i++)
        E[i][i] = 1;
    Matrix base;
    for (int i = 1; i <= m; i++) {
        int a, b;
        scanf("%d%d", &a, &b), ind[a]++, ind[b]++;
        base[a][a] = base[b][b] = base[a][b] = base[b][a] = inv;
    }
    for (int a = 1; a <= n; a++) {
        base[a][a] = (ll)(2 * m - ind[a]) * inv % mod;
    }
    base = Pow(base, k);
    for (int i = 1; i <= n; i++) {
        int res = 0;
        for (int j = 1; j <= n; j++)
            res = (res + (ll)A[j] * base[j][i]) % mod;
        printf("%d\n", res);
    }
}


#include <bits/stdc++.h>
#include <algorithm>
#define rep(i,n) for(int i=0;i<(n);i++)
#define per(i,n) for(int i=(n)-1;i>=0;i--)
#define prt(n) cout<<(n)<<endl
#define elif else if
#define str string
#define pb push_back
#define mp make_pair
typedef long long ll;
using namespace std;
const ll mod1=998244353;
const ll mod2=1000000007;
//ios_base::sync_with_stdio(false);�ӿ�cin
int main()
{
	string s;
	cin>>s;
	rep(i,s.size())
	{
		if((i%2==0)&&(isupper(s[i])))
		{
			cout<<"No"<<endl;
			return 0;
		}
		if((i%2==1)&&(islower(s[i])))
		{
			cout<<"No"<<endl;
			return 0;
		}
	}
	cout<<"Yes"<<endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
#define popcount __builtin_popcount

const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;

ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }

int main() {
  ll T; cin >> T;
  rep(_,T) {
    ll N; cin >> N;
    ll cnt = 0; while (N%2==0) { cnt++; N/=2; }
    string result;
    if (cnt==0) result = "Odd";
    else if (cnt==1) result = "Same";
    else result = "Even";
    cout << result << endl;
  }
  return 0;
}


///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
#define        u64             uint64_t
#define        ll              long long
#define        endl            "\n"
#define        PI              acos(-1)
#define        fi              first
#define        si              second
#define        mkp             make_pair
#define        pb              push_back
#define        set0(arr)       memset(arr, 0, sizeof(arr))
#define        setinf(arr)     memset(arr, 126, sizeof(arr))
#define        all(x)          (x).begin(),(x).end()
#define        sz(v)           ((int)(v).size())
#define        IOS             ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
using          pll        =      pair<ll, ll> ;
using          vl         =      vector<ll> ;
using          vpll       =      vector<pll> ;
using          mll        =      map<ll, ll> ;
using          mcl        =      map<char, ll> ;
using          msl        =      map<string,ll> ;
using          sl         =      set<ll> ;
using          sc         =      set<char> ;
using          dl         =      deque<ll> ;
const int N = 1e6+5 ;

ll      mod = 1e9+7 ;
vl      adj[N] ;
vpll    adjc[N] ;
ll      vis[N] ;
ll      arr[N] ;

int main()
{
    IOS;
    ll a, b, c, d, n, m, p, x, y, z, i, j, k, f = 0, tc, cnt = 0, sum = 0, mul = 1, mi = 1e18, ma = -1e18, cs;
    string   str ;
    char     ch ;
    double   db ;
    ll       l, r ;
    //code

   cin>>a>>b>>c>>d;
   if(a==b+c+d)
   {
           cout<<"Yes\n";
   }
   else if(b==a+c+d)
   {
           cout<<"Yes\n";
   }
   else if(c==a+b+d)
   {
           cout<<"Yes\n";
   }
   else if(d==a+b+c)
   {
           cout<<"Yes\n";
   }
   else if(a+b==c+d)
   {
           cout<<"Yes\n";
   }
   else if(a+c==b+d)
   {
           cout<<"Yes\n";
   }
   else if(a+d==b+c)
   {
          cout<<"Yes\n";
   }
   else cout<<"No\n";

    //code
    return 0;
}

#include<cstdio>
#include<queue>

int main(){
    int sum = 0;
    int a[15];
    for(int i = 1; i <= 4; i++){
        scanf("%d",&a[i]);
        sum += a[i];
    }
    int ok = 0;
    for(int i = 1; i < (1<<4); i++){
        int mask = i;
        int cur = 0;
        for(int j = 1; j <= 4; j++){
            if(mask%2) cur += a[j];
            mask /= 2;
        }
        if(cur*2==sum) ok = 1;
    }
    if(ok) printf("Yes\n");
    else printf("No\n");
    return 0;
}


#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <unordered_map>
#include <vector>

// #include <atcoder/all>
/*
cd $dir && g++ -std=c++17 -Wall -Wextra -O2 -DATCODERDEBUG
-I/mnt/d/MyProjects/atcoder/lib/ac-library $fileName && echo 'compilation ok!'
&&  ./a.out
*/
using namespace std;
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define ALL(x) x.begin(), x.end()
#define dame(a)        \
  {                    \
    cout << a << endl; \
    return 0;          \
  }
typedef long long ll;

class dstream : public ostream {};

template <typename T>
dstream &operator<<(dstream &os, T v) {
#ifdef ATCODERDEBUG
  cout << "\033[1;31m" << v << "\033[0m";
#endif
  return os;
}

ostream &endd(std::ostream &out) {
#ifdef ATCODERDEBUG
  out << std::endl;
#endif
  return out;
}
dstream dout;

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[ ";
  for (auto const &x : v) {
    os << x;
    if (&x != &v.back()) {
      os << " : ";
    }
  }
  os << " ]" << endl;
  return os;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
  os << "[" << endl;
  for (auto const &x : v) {
    os << " ";
    os << x;
  }
  os << "]" << endl;
  return os;
}

template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
  if (comp(xmax, x)) {
    xmax = x;
    return true;
  }
  return false;
}

template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
  if (comp(x, xmin)) {
    xmin = x;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  string s;
  cin >> s;
  ll cnt = 0;
  vector<ll> list;

  REP(i, s.size() - 1) {
    if (s[i + 1] == s[i])
      cnt++;
    else if (cnt > 0) {
      cnt = 0;
      list.emplace_back(i);
    }
  }
  dout << list << endd;
  ll ans = 0;

  vector<ll> tbl(26, 0);
  ll ct = s.size() - 1;
  for (ll i = list.size() - 1; i >= 0; i--) {
    ll idx = list[i];

    for (; ct > idx; ct--) {
      tbl[(s[ct] - 'a')]++;
    }
    ans += s.size() - idx - 1 - tbl[(s[idx] - 'a')];

    tbl = vector<ll>(26, 0);
    tbl[(s[idx] - 'a')] = s.size() - idx - 1;
  }
  cout << ans << endl;
  // cout << std::fixed << std::setprecision(15) << sqrt(ans) << endl;
}

#include<bits/stdc++.h>
using namespace std;
double n;
double ans;
int main()
{
    cin>>n;
    for(int i=1;i<n;i++){
        ans=ans+n/(n-i);
    }
    printf("%.10f\n",ans);
    system("pause");
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int getint()
{
	char ch;do{		ch=getchar();}while (ch!='-'&&(ch<'0'||ch>'9'));
	int ans=0,f=0;	if (ch=='-') f=1; else ans=ch-'0';
	while (isdigit(ch=getchar())) ans=ans*10+ch-'0';
	if (f) ans*=-1;
	return ans;
}
int a[2][211],b[2][21];
bool vis[211];
int main()
{
	int n,m,k,ans=0;
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	  cin>>a[0][i]>>a[1][i];
	cin>>k;
	for(int i=1;i<=k;i++)
	  cin>>b[0][i]>>b[1][i];
	for(int i=0;i<(1<<k);i++)
	{
		for(int j=0;j<k;j++)
		{
			if(i&(1<<j))
			  vis[b[0][j+1]]=1;
			else
			  vis[b[1][j+1]]=1;
		}
		int mid=0;
		for(int j=1;j<=m;j++)
		  if(vis[a[0][j]]&&vis[a[1][j]])
		    mid++;
		ans=ans>mid? ans:mid;
		for(int j=1;j<=n;j++)
		  vis[j]=0;
	}
	cout<<ans;
	return 0;
 }

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll ok[maxn];
typedef struct All
{
    ll A;
    ll B;
}all;

all a[maxn];
ll each[maxn][2];
ll k, n, m;
ll ma = 0;

void dfs(int pos)
{
    if(pos>k)
    {
        ll cnt = 0;
        rep(i,1,m) if(ok[a[i].A]&&ok[a[i].B]) cnt++;
        ma = max(cnt,ma);
        return;
    }
    ok[each[pos][0]] ++;
    dfs(pos+1);
    ok[each[pos][0]] --;

    ok[each[pos][1]] ++;
    dfs(pos+1);
    ok[each[pos][1]] --;
}

int main()
{
    n = read(), m = read();
    rep(i,1,m) a[i].A = read(), a[i].B = read();
    k = read();
    rep(i,1,k) each[i][0] = read(), each[i][1] = read();
    dfs(1);
    cout<<ma<<endl;
    return 0;
}


#include<bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define ll long long
#define rep(i,n) for(int i = 0; i < int(n); i++)
#define vi vector<int>
using namespace std;
const int INF = 1001001001;
const int MOD = 1e9+7;
template<class T> inline bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; }

int main(){
  cin.tie(0), ios::sync_with_stdio(false);
  int h,w,n,m; cin >> h >> w >> n >> m;
  set<pair<int,int>> block;
  vector<vi> l(h,vi(w)), r(h,vi(w)), u(h,vi(w)), d(h,vi(w));
  rep(i,n){
    int a,b; cin >> a >> b;
    --a; --b;
    l[a][b] = 1;
    r[a][b] = 1;
    u[a][b] = 1;
    d[a][b] = 1;
  }
  rep(i,m){
    int c,d; cin >> c >> d;
    block.insert({--c,--d});
  }
  rep(i,h){
    rep(j,w-1){
      if(block.count({i,j+1})) continue;
      r[i][j+1] |= r[i][j];
    }
  }
  rep(i,h){
    for(int j = w-1; 0 < j; j--){
      if(block.count({i,j-1})) continue;
      l[i][j-1] |= l[i][j];
    }
  }
  rep(j,w){
    rep(i,h-1){
      if(block.count({i+1,j})) continue;
      d[i+1][j] |= d[i][j];
    }
  }
  rep(j,w){
    for(int i = h-1; 0 < i; i--){
      if(block.count({i-1,j})) continue;
      u[i-1][j] |= u[i][j];
    }
  }
  int ans = 0;
  rep(i,h) rep(j,w) ans += u[i][j]|d[i][j]|r[i][j]|l[i][j];
  cout << ans;
  cout << endl;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define Go_ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long int
#define llu long long unsigned int
#define pii pair<int ,int>
#define PII pair<ll ,ll>

const int Max=1e9+100;
const ll  MAX=1e18+500;
const int sz=1510;

bool vist[sz][sz];
int mark[sz][sz];

int main()
{
    Go_
    int a,b,c,i,j,k,q,p,x,y,ct,ct1,m,l,r,x1,y1,mn,h,sum1,in,z,mid,n,mx;
    char ch;
    double d;
    string str1,str2,str;
    bool bl,bl1;
    int t,cs=1;

    cin>>n>>m>>a>>b;

    memset(mark, 0, sizeof mark);

    pii light[a];

    for(int i=0;i<a;i++) cin>>light[i].first>>light[i].second;

    ct=0;
    for(int i=1;i<=b;i++){
        cin>>x>>y;
        mark[x][y]=-1;
    }

    for(int i=0;i<a;i++)
    {
        int x, y;
        x=light[i].first, y=light[i].second;
        if(vist[x][y]==false) ++ct;
        vist[x][y]=true;

        j=y;
        while(j<=m && mark[x][j] != -1){
            if(vist[x][j]==false) ++ct;
            vist[x][j]=true;
            ++j;
        }

        j=y;
        while(j>0 && mark[x][j] != -1){
            if(vist[x][j]==false) ++ct;
            vist[x][j]=true;
            --j;
        }

        j=x;
        while(j<=n && mark[j][y] != -1){
            if(vist[j][y]==false) ++ct;
            vist[j][y]=true;
            ++j;
        }

        j=x;
        while(j>=1 && mark[j][y] != -1){
            if(vist[j][y]==false) ++ct;
            vist[j][y]=true;
            --j;
        }

        mark[x][y]=-1;
    }

    cout<<ct<<'\n';

}


#include<bits/stdc++.h>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define all(x) (x).begin(),(x).end()
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
using Graph = vector<vector<int>>;

// dijkstra
struct edge{ll to, cost;};
typedef pair<ll,ll> P;
struct graph{
  ll V;
  vector<vector<edge> > G;
  vector<ll> d;

  graph(ll n){
    init(n);
  }

  void init(ll n){
    V = n;
    G.resize(V);
    d.resize(V);
    rep(i,V){
      d[i] = INF;
    }
  }

  void add_edge(ll s, ll t, ll cost){
    edge e;
    e.to = t, e.cost = cost;
    G[s].push_back(e);
  }

  void dijkstra(ll s){
    rep(i,V){
      d[i] = INF;
    }
    d[s] = 0;
    priority_queue<P,vector<P>, greater<P> > que;
    que.push(P(0,s));
    while(!que.empty()){
      P p = que.top(); que.pop();
      ll v = p.second;
      if(d[v]<p.first) continue;
      for(auto e : G[v]){
        if(d[e.to]>d[v]+e.cost){
          d[e.to] = d[v]+e.cost;
          que.push(P(d[e.to],e.to));
        }
      }
    }
  }
};
// dijkstra end

class UnionFind
{
public:
  ll par[100005];
  ll depth[100005];
  ll nGroup[100005];

  UnionFind(ll n) {
    init(n);
  }

  void init(ll n) {
    for(ll i=0; i<n; i++) {
      par[i] = i;
      depth[i] = 0;
      nGroup[i] = 1;
    }
  }

  ll root(ll x) {
    if(par[x] == x) {
      return x;
    } else {
      return par[x] = root(par[x]);
    }
  }

  bool same(ll x, ll y) {
    return root(x) == root(y);
  }

  void unite(ll x, ll y) {
    x = root(x);
    y = root(y);
    if(x == y) return;

    if(depth[x] < depth[y]) {
      par[x] = y;
      nGroup[y] += nGroup[x];
      nGroup[x] = 0;
    } else {
      par[y] = x;
      nGroup[x] += nGroup[y];
      nGroup[y] = 0;
      if(depth[x] == depth[y])
        depth[x]++;
    }
  }
};
// unionfind end

// nCr
const ll MAX = 500010;
ll fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (ll i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// nCr end

// tree DP
vl c (200000, 0);
vector<ll> color (200000, 0);
vl ok(200000, 0);
void dfs(const Graph &G, ll v, ll p, ll d) {

    if(color[c[v]] == 0) ok[v]++;

    color[c[v]]++;
    for (auto nv : G[v]) {
        if (nv == p) continue;
        dfs(G, nv, v, d+1);
    }
    color[c[v]]--;
    return;
}
// tree DP end
int main() {
  ll n; cin>>n;
  cout << n - 1 << endl;
}

#include <bits/stdc++.h>
#define INF 2000000000
#define MOD 1000000007
#define MAXN 200005
#define REP(temp, init_val, end_val) for (int temp = init_val; temp <= end_val; ++temp)
#define REPR(temp, init_val, end_val) for (int temp = init_val; temp >= end_val; --temp)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> intpair;
int read(){
    int f = 1, x = 0;
    char c = getchar();
    while (c < '0' || c > '9'){if(c == '-') f = -f; c = getchar();}
    while (c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar();
    return f * x;
}
inline int lowbit(int x){
    return x & (-x);
}
inline int modadd(int x, int y){
    return (x + y >= MOD ? x + y - MOD: x + y);
}
inline int sgn(int x){
    return (x < 0 ? -1: (x > 0 ? 1: 0));
}
template<typename T>
T gcd(T a, T b){
    return (!b) ? a: gcd(b, a % b);
}
int poww(int a, int b){
    int res = 1;
    while (b > 0){
        if (b & 1) res = 1ll * res * a % MOD;
        a = 1ll * a * a % MOD, b >>= 1;
    }
    return res;
}
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
const int ddx[] = {-1, -1, -1, 0, 0, 1, 1, 1}, ddy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/

int n, c1, c2, c3, c4;
void init(){
    n = read();
    char s[4];
    scanf("%s", s);
    c1 = s[0] - 'A';
    scanf("%s", s);
    c2 = s[0] - 'A';
    scanf("%s", s);
    c3 = s[0] - 'A';
    scanf("%s", s);
    c4 = s[0] - 'A';
}
void solve(){
    int tt = c1 * 8 + c2 * 4 + c3 * 2 + c4;
    if (tt <= 3 || tt == 5 || tt == 7 || tt == 13 || tt == 15){
        printf("1\n");
    } else if (tt == 4 || tt == 10 || tt == 11 || tt == 12){
        if (n == 2) printf("1\n");
        else printf("%d\n", poww(2, n - 3));
    } else {
        int f0 = 1, f1 = 1;
        n -= 2;
        while (n > 0){
            int f2 = modadd(f0, f1);
            f0 = f1, f1 = f2;
            --n;
        }
        printf("%d\n", f0);
    }
}
int main(){
    int T = 1;
    while (T--){
        init();
        solve();
    }
    return 0;
}


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

int main() {
    int a, b;
    cin >> a >> b;

    if(a + b >= 15 && b >= 8) cout << 1 << endl;
    else if(a + b >= 10 && b >= 3) cout << 2 << endl;
    else if(a + b >= 3) cout << 3 << endl;
    else cout << 4 << endl;

    return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define all(x) x.begin(), x.end()
#define pb push_back
#define vl vector<ll>
#define pii pair<int, int>
#define ull unsigned long long
using namespace std;
typedef tuple<ll, ll, ll> ti;
const ll INF=1e17;
void dijkstra(vector<vector<pair<int, int>>>& adj, int x) {
    int n = adj.size();
    priority_queue<pair<ll, int>> q;
    vector<bool> processed(n+1, false);
    vl dist(n+1, LONG_MAX);
    dist[x] = 0;
    q.push({0, x});
    while (!q.empty()) {
        int a = q.top().second;
        q.pop();
        if (processed[a]) continue;
        processed[a] = true;
        for (auto it: adj[a]) {
            int b = it.first, w = it.second;
            if (dist[b] > dist[a] + w) {
                dist[b] = dist[a] + w;
                q.push({-dist[b], b});
            }
        }
    }
}
bool cmp(pair<int, int>& a, pair<int, int>& b) {
    if (a.first == b.first) return a.second < b.second;
    return a.first > b.first;
}
string decimalToBinary(ll n)
{
    //finding the binary form of the number and
    //coneverting it to string.
    string s = bitset<64> (n).to_string();

    //Finding the first occurance of "1"
    //to strip off the leading zeroes.
    const auto loc1 = s.find('1');

    if(loc1 != string::npos)
        return s.substr(loc1);

    return "0";
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int a, b;
    cin>>a>>b;
    ll ms = a + b;
    if (ms < 3) {
        cout<<4;
    } else {
        if (ms >= 15 && b >= 8) cout<<1;
        else if (ms >= 10 && b >= 3) cout<<2;
        else cout<<3;
    }
    return 0;
}


#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; debug_print(__VA_ARGS__); } while(false)
template<typename T, typename... Ts> void debug_print(const T &t, const Ts &...ts) { cerr << t; ((cerr << ", " << ts), ...); cerr << endl; }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, const T& b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, const T& b) { if (a < b) {a = b; return true; } return false; }
template<typename T, typename... Ts> void print(const T& t, const Ts&... ts) { cout << t; ((cout << ' ' << ts), ...); cout << '\n'; }
template<typename... Ts> void input(Ts&... ts) { (cin >> ... >> ts); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    string s, t;
    input(n, s, t);
    if (count(s.begin(), s.end(), '1') != count(t.begin(), t.end(), '1')) {
        print(-1);
        return 0;
    }
    int ans = 0;
    int a = 0, b = 0;
    rep(i, 0, n) {
        if (s[i] == t[i]) continue;
        a = max(i, a);
        b = max(i, b);
        while (a < n && s[a] == '1') a++;
        while (b < n && t[b] == '1') b++;
        if (s[i] == '1') {
            swap(s[i], s[a]);
            ans++;
        }
        if (t[i] == '1') {
            swap(t[i], t[b]);
            ans++;
        }
    }
    print(ans);

    return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define prec(n) fixed<<setprecision(n)
using namespace std;
typedef long long ll;
typedef unsigned long long int ull ;

const int MOD=1e9+7;
const char nl = '\n';

int check (int n ) {
	int k1 = n ;
  	int c = 0 ;
  	while( k1 != 0 ){
    	int tmp = k1 % 10 ;
      	if ( tmp == 7 )
          	c = 1 ;
     	k1 /= 10 ;
    }
  	int k2 = n ;
  	int c1 = 0 ;
  	while( k2 != 0 ){
    	int tmp = k2 % 8 ;
      	if ( tmp == 7 )
          	c1 = 1 ;
     	k2 /= 8 ;
    }
  	if ( c1 == 1 || c == 1 )
      	return 0 ;
  	else
      	return 1 ;

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
   	int n  ;
  	cin >> n ;
  	int ans = 0 ;
  	for (int i = 1 ; i <= n ; i++)
      	if ( check(i) == 1 )
          	ans++;
  	cout << ans;

    return 0;
}


#include "bits/stdc++.h"
using namespace std;
#define ll long long

ll dp[5050][5050];
ll sdp[5050];
ll mod = 998244353;
ll invs[5050];
ll mis[5050][5050];

ll gcd(ll a, ll b, ll &x, ll &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    ll x1, y1;
    ll d = gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}

int main()
{
    ll n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        for (int x = 0; x <= m; x++)
        {
            if (i > 0)
                mis[i][x] = mis[i - 1][x] * (m - x);
            else
                mis[i][x] = 1;
            mis[i][x] %= mod;
        }
    }
    for (int x = 1; x <= m; x++)
    {
        ll x1, y1;
        ll g = gcd(x, mod, x1, y1);
        x1 = (x1 % mod + mod) % mod;
        invs[x] = x1;
    }
    for (int i = 1; i <= m; i++)
    {
        dp[0][i] = 1;
    }
    sdp[0] = m;
    for (int i = 1; i < n; i++)
    {
        for (ll x = 1; x <= m; x++)
        {
            ll mi = mis[i][0];
            ll mix = mi - mis[i][x] + mod;
            mix %= mod;
            dp[i][x] = sdp[i - 1] + mi - (mix)*invs[x] + mod;
            dp[i][x] %= mod;
            sdp[i] += dp[i][x] + mod;
            sdp[i] %= mod;
        }
    }
    cout << sdp[n - 1] << endl;
    return 0;
}

#include<cstdio>
#include<algorithm>
using namespace std;

int main(){
    unsigned N, M;
    scanf("%u %u", &N, &M);
    int H[N];
    for(unsigned i = 0; i < N; ++i){
        scanf("%d", H + i);
    }
    int W[M];
    for(unsigned i = 0; i < M; ++i){
        scanf("%d", W + i);
    }
    sort(H, H + N);
    sort(W, W + M);
    unsigned candidate = 0, answer = 999999999, id = 0, last;
    for (unsigned i = 0; i < N; i++){
        candidate += i&1 ? -H[i] : H[i];
    }
    for (unsigned i = 0; i < M && (!i || W[i-1] < H[N-1]); i++){
        last = id;
        id = distance(H, lower_bound(H, H + N, W[i]));
        for (unsigned j = last; j < id; j++){
            candidate += H[j]* ((j&1) ? 2 : -2);
        }
        candidate = candidate - (i ? W[i-1] : 0)*((last&1) ? 1 : -1) + W[i]*((id&1) ? 1 : -1);
        if(answer > candidate) answer = candidate;
    }
    printf("%u\n", answer);
	return 0;
}