#include "bits/stdc++.h"
using namespace std;
#ifndef LOCAL
#define endl '\n'
#endif

#define fr(i, a, b) for(int i = a; i <= b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define pf push_front
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
#define lb lower_bound
#define ub upper_bound

typedef long long ll;
typedef long double f80;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int pct(int x) { return __builtin_popcount(x); }
int pct(ll x) { return __builtin_popcountll(x); }
int bt(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
int bt(ll x) { return 63 - __builtin_clzll(x); } // floor(log2(x))
int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); }
ll cdiv(ll a, ll b) { return a / b + !(a < 0 || a % b == 0); }
int nxt_C(int x) { int c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; }
ll nxt_C(ll x) { ll c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; }

vector<int> get_bits(int mask) {
	vector<int> bb;
	while(mask) { int b = bt(mask); bb.pb(b); mask ^= (1 << b); }
	reverse(all(bb));
	return bb;
}

int get_mask(vector<int> v) {
	int mask = 0;
	for(int x : v) { mask ^= (1 << x); }
	return mask;
}

template<typename T>
void uniq(vector<T> &v) { sort(all(v)); v.resize(unique(all(v)) - v.begin()); }

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll rand(ll l, ll r){
	uniform_int_distribution<ll> uid(l, r);
	return uid(rng);
}

void sc() {}

template <typename Head, typename... Tail>
void sc(Head &H, Tail &... T) { cin >> H; sc(T...); }

#ifdef LOCAL
#define debug(...) cerr << "[L:" << __LINE__ << "][" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

// #ifndef LOCAL
// string to_string(__int128 x) {
// 	string s = "";
// 	bool neg = 0;
// 	if(x < 0) { s += "-"; neg = 1; x = -x; }
// 	if(!x) s += '0';
// 	while(x) {
// 		int rem = x % 10;
// 		s += to_string(rem);
// 		x /= 10;
// 	}
// 	reverse(s.begin() + neg, s.end());
// 	return s;
// }
// #endif

const int mod = 1e9 + 7; // 998244353;

int pwr(int a,ll b) {
	int ans = 1;
	while(b) {
		if(b & 1) ans = (ans * 1LL * a) % mod;
		a = (a * 1LL * a) % mod;
		b >>= 1;
	}
	return ans;
}

/*
	Lookout for overflows!!
	Check array sizes!!
	Clear before test cases!!
	Use the correct modulo!!
	Check for corner cases!!
	Are you forgetting something?!
	Read problem statement carefully!!!
*/

const int N = 1e5 + 5;
int dsu[N], sz[N];
bool odd[N], par[N];

int root(int u) {
    while(u != dsu[u]) u = dsu[u];
    return u;
}

int get_par(int u) {
    int p = 0;
    while(u != dsu[u]) {
        p ^= par[u];
        u = dsu[u];
    }
    return p;
}

void uni(int u,int v) {
    int uu = root(u), vv = root(v);
    if(uu == vv) {
        if(get_par(u) == get_par(v))
            odd[uu] = 1;
        return;
    }
    if(sz[uu] > sz[vv]) {
        swap(uu, vv), swap(u, v);
    }
    dsu[uu] = vv;
    sz[vv] += sz[uu];
    odd[vv] |= odd[uu];
    par[uu] = get_par(u) ^ get_par(v) ^ 1;
}

void solve() {
	int n, m;
    sc(n, m);
    fr(i, 1, n) {
        sz[i] = 1;
        dsu[i] = i;
        odd[i] = 0;
        par[i] = 0;
    }
    fr(i, 1, m) {
        int u, v;
        sc(u, v);
        uni(u, v);
    }
    int c1 = 0, c2 = 0, c3 = 0;
    fr(i, 1, n) {
        if(i == dsu[i]) {
            if(sz[i] == 1) {
                c1++;
            }
            else if(odd[i]) {
                c2++;
            }
            else {
                c3++;
            }
        }
    }
    ll ans = 0;
    while(c1) {
        ans += 2 * n - 1;
        n--;
        c1--;
    }
    while(c2) {
        ans += 2 * (c2 + c3) - 1;
        c2--;
    }
    ans += (c3 * 1LL * (c3 - 1)) * 2 + 2LL * c3;
    cout << ans;
}

int main() {
	ios :: sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
	// cin >> t;
	for(int tt = 1; tt <= t; tt++) {
		solve();
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll,ll> pll;
const ll mod=1e9+7;
//const ll mod=998244353;
const ll inf=1LL<<61;

int main() {
  ll t;
  cin >> t;
  while(t--) {
    ll n;
    cin >> n;
    vec a(n);
    for(ll i=0;i<n;i++) cin >> a[i];
    string s;
    cin >> s;
    vec al;
    ll f=0;
    for(ll i=n-1;i>=0;i--) {
      for(ll t:al) {
        a[i]=min(a[i],a[i]^t);
      }
      al.push_back(a[i]);
      if(a[i]&&s[i]=='1') {
        f=1;
      }
    }
    cout << f << endl;
  }
}


// 有點不耐煩會破壞偉大的計劃

#include <bits/stdc++.h>

#define append push_back

using namespace std;
using ll = long long;

const int N = 1e5 + 3;
const int M = 1e6 + 7;
const long long MOD = 1e9 + 7;
const long long INF = 1e14 + 9;
const int SM = 1e3 + 5;
const int logN = 22, BL = 400;
const double EPS = 1e-9, PI = acos(-1);
const int dx[] = {1, 0, 0, -1, -1, 1, -1, 1};
const int dy[] = {0, 1, -1, 0, -1, 1, 1, -1};

void debug() {
	cerr << "\n";
}
template<typename Type1, typename... Type2>
void debug(Type1 a, Type2... b) {
    cerr << a << " ";
    debug(b...);
}

int main() {
//    freopen(".in", "r", stdin);
//    freopen(".out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    long long n, curMin = INF, ans = 0; cin >> n;

    vector<long long> a(n);

    for(long long i = 0; i < n; i++) {
        cin >> a[i];
        curMin = min(curMin, a[i]);
        if(curMin >= a[i]) ans++;
    }

    cout << ans;

    return 0;
}

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

using ll = long long;
constexpr ll mod = 1e9 + 7;
#define endl '\n'
class SegTree {
  constexpr static int DEFAULT = INT_MAX;
  int Combine(int l, int r) { return min(l,r); }

  vector<int> seg_tree;
  int n = 0;

  void Build(vector<int>& A, int v, int tl, int tr) {
    if (tl == tr) {
      seg_tree[v] = A[tl];
    } else {
      int tm = tl + (tr - tl) / 2;
      Build(A, v*2, tl, tm);
      Build(A, v*2+1, tm+1, tr);
      seg_tree[v] = Combine(seg_tree[v*2], seg_tree[v*2+1]);
    }
  }
 public:

  SegTree(vector<int>& A) {
    n = A.size();
    seg_tree.resize(4*n);
    Build(A, 1, 0, n-1);
  }

  SegTree(int _n) : n(_n) {
    //seg_tree.resize(4*n);
    seg_tree.resize(4*n, DEFAULT);
  }

  int Query(int l, int r, int tl, int tr, int v) {
    if (l > r) return DEFAULT;
    if (l == tl && r == tr) return seg_tree[v];
    int tm = tl + (tr - tl) / 2;
    return Combine(Query(l, min(r, tm), tl, tm, v*2),
                   Query(max(l, tm+1), r, tm+1, tr, v*2+1));
  }

  int Query(int l, int r) {
    return Query(l, r, 0, n-1, 1);
  }

  void Update(int pos, int new_val, int tl, int tr, int v) {
    if (tl == tr) {
      seg_tree[v] = new_val;
      return;
    }

    int tm = tl + (tr - tl) / 2;
    if (pos <= tm) {
      Update(pos, new_val, tl, tm, v*2);
    } else {
      Update(pos, new_val, tm+1, tr, v*2+1);
    }
    seg_tree[v] = Combine(seg_tree[v*2], seg_tree[v*2+1]);
  }

  void Update(int pos, int new_val) {
    Update(pos, new_val, 0, n-1, 1);
  }
};

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

  ll n;
  cin >> n;
  vector<int> A(n);
  for (auto& _a : A) cin >> _a;
  SegTree stree(A);
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (stree.Query(0, i) == A[i]) ++ans;
  }
  cout << ans << endl;

}


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

int main() {
  string s;
  cin >> s;
  int sum = 0;
  vector<int> v(s.size());
  for(int i=0; i<s.size(); i++) {
    v[i] = s[i]-'0';
    sum += v[i];
  }
  if(stoi(s)%sum==0) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;

int f(int n) {
  int ret = 0;
  while (n >= 1) {
    ret += n % 10;
    n /= 10;
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  if (n % f(n) == 0) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <chrono>
#include <random>
#include <functional>

using namespace std;

#define int long long

const int MOD = 998244353;
const int N = 1e5 + 5;

int add(int a, int b) {
	return (a + b) % MOD;
}

int mul(int a, int b) {
	return (a * b) % MOD;
}

int binpow(int a, int b) {
	int res = 1;
	while (b) {
		if (b & 1) res = mul(res, a);
		a = mul(a, a);
		b >>= 1;
	}
	return res;
}

int divv(int a, int b) {
	return mul(a, binpow(b, MOD - 2));
}

int f[N];

void init() {
	f[0] = 1;
	for (int i = 1; i < N; i++) {
		f[i] = mul(i, f[i - 1]);
	}
}

int C(int n, int k) {
	return divv(f[n], mul(f[k], f[n - k]));
}

int cnt_array(int cnt, int n) {
	if (cnt == 0) return 0;
	return C(n + cnt - 1, n);
}

int solve(int db, int on, int n, int rk) {
	rk -= db;
	int ans = 0;
	for (int sel = 0; sel <= db && sel <= n; sel++) {
		int cnt = cnt_array(on + sel, n - sel);
		cnt = mul(cnt, C(db, sel));
		cnt = mul(cnt, binpow(2, sel));
		ans = add(ans, cnt);
	}
	return ans;
}

int n, k;

int solve_i(int i) {
	int db = 0;
	int on = 0;
	int mid = 0;
	for (int x = 1; x <= k; x++) {
		if (1 <= (i - x) && (i - x) <= k) {
			if (2 * x == i) {
				mid = 1;
			} else {
				db++;
			}
		} else {
			on++;
		}
	}
	db /= 2;
	if (mid) {
		int ff = solve(db, on, n, k - 1);
		int ss = solve(db, on, n - 1, k - 1);
		return add(ff, ss);
	} else {
		return solve(db, on, n, k);
	}
}

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

	init();

	cin >> k >> n;
	for (int i = 2; i <= 2 * k; i++) {
		cout << solve_i(i) << '\n';
	}
}

#include <bits/stdc++.h>

#define mp make_pair
#define X first
#define Y second
#define FOE(x, a) for(auto x : a)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FOD(i, a, b) for(int i = a; i >= b; i--)

using namespace std;

typedef long long ll;
typedef pair <int, int> ii;

const int N = 2010;
const int MOD = 998244353;

void add(int &x, int y){
    x += y;
    x -= (x >= MOD) * MOD;
}

void sub(int &x, int y){
    x -= y;
    x += (x < 0) * MOD;
}

int n, k, d[2 * N], C[2 * N][2 * N], pow2[N];

int nCk(int n, int k){
    if (n < 0 || n < k || k > n || k < 0)
        return 0;
    return C[n][k];
}

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

    cin >> k >> n;

    C[0][0] = 1;
    FOR(i, 1, 2 * max(n, k)){
        C[i][0] = 1;
        FOR(j, 1, i){
            add(C[i][j], C[i - 1][j]);
            add(C[i][j], C[i - 1][j - 1]);
        }
    }

    pow2[0] = 1;
    FOR(i, 1, max(n, k))
        pow2[i] = (ll) pow2[i - 1] * 2 % MOD;

    FOR(i, 1, k)
        FOR(j, i + 1, k)
            d[i + j]++;

    FOR(x, 2, 2 * k)
        if (x % 2 == 1){
            int ans = 0;
            FOR(i, 0, d[x])
                add(ans, (ll) pow2[i] * nCk(k - 2 * d[x] + n - 1, n - i) % MOD * nCk(d[x], i) % MOD);
            cout << ans << endl;
        } else {
            int ans = 0;
            FOR(i, 0, d[x]){
                add(ans, (ll) pow2[i] * nCk(k - 2 * d[x] + n - 2, n - i) % MOD * nCk(d[x], i) % MOD);
                add(ans, (ll) pow2[i] * nCk(k - 2 * d[x] + n - 3, n - i - 1) % MOD * nCk(d[x], i) % MOD);
            }
            cout << ans << endl;
        }

    int ans = 0;

    return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstring>
using namespace std;
typedef long long ll;
int a[100100];
int c[100100];
bool check(int x,int i)
{
    if(c[x]<a[i])
    {
        return 1;
    }
    else
    {
        return 0;
    }

}
int bs(int l, int r,int i)
{
    while (l < r)
    {
        int mid = l + r >> 1;
        if (check(mid,i)) r = mid;    // check()判断mid是否满足性质
        else l = mid + 1;
    }
    return l;
}
int main()
{
    int n;
    cin >> n;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i];
    }
    int cnt=1;
    c[1] = a[1];
    for(int i=2;i<=n;i++)
    {
        int pos = bs(1,cnt,i);
        if(c[pos]<a[i])
        {
            c[pos] = a[i];
        }
        else
        {
            cnt++;
            c[cnt] = a[i];
        }

    }
    cout << cnt << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
typedef long long ll;
#define all(x) (x).begin(),(x).end()

int main(){
	ll n; cin>>n;
	vector<ll> v(5);
	rep(i,5) cin>>v[i];
	sort(all(v));
	cout<<(n+v[0]-1)/v[0]+4<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;

#define N 600000
#define NIL -1

//array
int key[N];
int parent[N];
int leftc[N];
int rightc[N];
//root
int root=NIL;

//function insert
void insert(int z){
  int x,y;

  y=NIL;
  x=root;

  while(x!=NIL){
    y=x;
    if(key[z]<key[x]) x=leftc[x];
    else x=rightc[x];
  }

  parent[z]=y;

  if(y==NIL) root=z;

  else if(key[z]<key[y]) leftc[y]=z;

  else rightc[y]=z;

}

//function search
int find(int now,int data){
  if(now==NIL ||data==key[now]) return now;

  if(data<key[now]) return find(leftc[now],data);
  else return find(rightc[now],data);
}

int treeminimum(int x){
  while(leftc[x]!=NIL){
    x=leftc[x];
  }
  return x;
}

int treesuccessor(int x){
  int y;
  if(rightc[x]!=NIL) return treeminimum(rightc[x]);
  y=parent[x];
  while(y!=NIL &&x==rightc[x]){
    x=y;
    y=parent[y];
  }
  return y;
}

//function delete
void treedelete(int z){
  int x,y;
  if(leftc[z]==NIL ||rightc[z]==NIL) y=z;
  else y=treesuccessor(z);

  if(leftc[y]!=NIL) x=leftc[y];
  else x=rightc[y];

  if(x!=NIL) parent[x]=parent[y];
  if(parent[y]==NIL) root=x;
  else if(y==leftc[parent[y]]) leftc[parent[y]]=x;
  else rightc[parent[y]]=x;

  if(y!=z) key[z]=key[y];

}

//print
void inorderprint(int now){
  if(now!=NIL){
    inorderprint(leftc[now]);
    printf(" %d",key[now]);
    inorderprint(rightc[now]);
  }
}

void preorderprint(int now){
  if(now!=NIL){
    printf(" %d",key[now]);
    preorderprint(leftc[now]);
    preorderprint(rightc[now]);
  }
}

int main(){

  int n,i,j,data;
  int z=0;
  int del;
  char str[10];
  //initialize array
  for(i=0;i<N;i++){
    parent[i]=NIL;
    key[i]=NIL;
    rightc[i]=NIL;
    leftc[i]=NIL;
  }
  //load n
  scanf("%d",&n);
  //load commnad and key
  for(i=0;i<n;i++){
    scanf("%s",str);
    if(strcmp(str,"insert")==0) {
      scanf("%d",&data);
      key[z]=data;
      insert(z);
      z++;
    }
    else if(strcmp(str,"find")==0){
      scanf("%d",&data);
      if(find(root,data)!=NIL) printf("yes\n");
      else printf("no\n");
    }
    else if(strcmp(str,"delete")==0){
      scanf("%d",&data);
      del=find(root,data);
      treedelete(del);
    }
    else if(strcmp(str,"print")==0){
      inorderprint(root);
      cout <<endl;
      preorderprint(root);
      cout <<endl;
    }
  }

  return 0;
}

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

struct Node {
    int key;
    Node *parent, *left, *right;
};

Node *NIL, *root;

void insert(int x) {
    Node *p = NIL;
    Node *u = root;
    Node *in = (Node *)malloc(sizeof(Node));
    in->key = x;
    in->left = NIL;
    in->right = NIL;

    while (u != NIL) {
        p = u;
        if (in->key < u->key) {
            u = u->left;
        } else {
            u = u->right;
        }
    }

    in->parent = p;
    if (p == NIL) {
        root = in;
    } else if (in->key < p->key) {
        p->left = in;
    } else {
        p->right = in;
    }
}

bool find(Node *u, int x) {
    while (u != NIL && u->key != x) {
        if (x < u->key) {
            u = u->left;
        } else {
            u = u->right;
        }
    }

    if (u == NIL) {
        return false;
    } else {
        return true;
    }
}

Node * findP(Node *u, int x) {
    while (u != NIL && u->key != x) {
        if (x < u->key) {
            u = u->left;
        } else {
            u = u->right;
        }
    }

    return u;
}

Node * getMinimum(Node *u) {
    while (u->left != NIL) {
        u = u->left;
    }

    return u;
}

Node * getSuccessor(Node *u) {
    if (u->right != NIL) {
        return getMinimum(u->right);
    }

    Node *d = u->parent;
    while (d != NIL && u == d->right) {
        u = d;
        d = d->parent;
    }

    return d;
}

void deleteNode(Node *u) {
    Node *d, *x;
    if (u->left == NIL || u->right == NIL) {
        d = u;
    } else {
        d = getSuccessor(u);
    }

    if (d->left != NIL) {
        x = d->left;
    } else {
        x = d->right;
    }

    if (x != NIL) {
        x->parent = d->parent;
    }

    if (d->parent == NIL) {
        root = x;
    } else if (d == d->parent->left) {
        d->parent->left = x;
    } else {
        d->parent->right = x;
    }

    if (d != u) {
        u->key = d->key;
    }
}

void printPre(Node *u) {
    if (u == NIL) {
        return;
    }

    printf(" %d", u->key);
    printPre(u->left);
    printPre(u->right);
}

void printIn(Node *u) {
    if (u == NIL) {
        return;
    }

    printIn(u->left);
    printf(" %d", u->key);
    printIn(u->right);
}

int main() {
    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
        string com;
        cin >> com;
        if (com == "insert") {
            int x;
            scanf("%d", &x);
            insert(x);
        } else if (com == "find") {
            int x;
            scanf("%d", &x);
            if (find(root, x)) {
                cout << "yes" << endl;
            } else {
                cout << "no" << endl;
            }
        } else if (com == "delete") {
            int x;
            scanf("%d", &x);
            auto u = findP(root, x);
            deleteNode(u);
        } else {
            printIn(root);
            printf("\n");
            printPre(root);
            printf("\n");
        }
    }

    return 0;
}


#include <bits/stdc++.h>
#define rep(i, start, end) for (long long i = start; i < end; ++i)
#define repreverse(i, start, end) for (long long i = start; i >= end; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define len(x) ((long long)(x).size())
#define lcm(a, b) ((a) / __gcd((a), (b)) * (b))
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vllvll = vector<vll>;
using vc = vector<char>;
using pll = pair<ll, ll>;
template<class T> void print1d(T x,ll n=-1){if(n==-1)n=x.size();rep(i,0,n){cout<<x[i]<<' ';}cout<<'\n';}
template<class T> void print2d(T x,ll r=-1,ll c=-1){if(r==-1)r=x.size();if(c==-1)c=x[0].size();rep(i,0,r)print1d(x[i],c);}
template<class T, class U> bool haskey(T mp, U key) { return mp.find(key) != mp.end(); }
template<class T, class U> bool isin(T el, U container) { return find(all(container), el) != container.end(); }
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<class T> bool even(T n) { return ! (n & 1); }
template<class T> bool odd(T n) { return n & 1; }
template<class T> ll rup(T a, T b) { return a % b ? a / b + 1 : a / b; }
template<class T> ld deg2rad(T deg) { return M_PI * deg / 180.0; }
template<class T> ld rad2deg(T rad) { return 180.0 * rad / M_PI; }
const long double pi = M_PI;
const long long big = 1LL << 50;
const long long inf = 1LL << 60;
const long long mod = 1e9 + 7;
ll intpow(ll a, ll n, ll _mod=numeric_limits<ll>::max()) { ll p=1; while (n) { if (n&1) p=p*a%_mod; a=a*a%_mod; n>>=1; } return p; }
ll modc(ll a, char op, ll b, ll _mod=mod) { a %= _mod; b %= _mod; ll res = 1; switch(op) {
        case '+': res = (a + b) % _mod; break;
        case '-': res = (a - b) % _mod; break;
        case '*': res = a * b % _mod; break;
        case '/': res = modc(a, '*', modc(b, '^', _mod-2, _mod), _mod); break;
        case '^': res = intpow(a, b, _mod); break;
        case 'P': rep(i, a-b+1, a+1) res = modc(res, '*', i, _mod); break;
        case 'C': res = modc(modc(a, 'P', b, _mod), '/', modc(b, 'P', b, _mod)); break; } if (res < 0) res += _mod; return res; }

int main()
{
        ll H, W, N;
        cin >> H >> W >> N;
        ll bigger = max(H, W);
        cout << rup(N, bigger) << endl;
}

#include <stdio.h>

int main() {
  int h, w, g, result;
  scanf("%d %d %d", &h, &w, &g);
  result = (h > w) ? (g+h-1)/h : (g+w-1)/w;
  printf("%d\n", result);
  return 0;
}


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

int soredake(int point, int kazu, int bonasu, int goal) {
  int tokukazu = goal / point;
  if (goal % point > 0) {
    tokukazu++;
  }
  if (tokukazu > kazu) {
    tokukazu = kazu;
  }
  return tokukazu;
}

int search(vector<vector<int>> questions, int goal) {

  int minimum = 0;
  for (int i = 0; i < questions.size(); i++) {
    minimum += questions.at(i).at(1);
  }
  for (int i = 0; i < questions.size(); i++) {
    int point = questions.at(i).at(0);
    int kazu = questions.at(i).at(1);
    int bonasu = questions.at(i).at(2);
    int goukei = questions.at(i).at(3);

    vector<vector<int>> q = questions;
    q.erase(q.begin() + i);
    if (goukei < goal) {
      int nokori = search(q, goal - goukei) + kazu;
      if (nokori < minimum) {
        minimum = nokori;
      }
    } else {
      int nokori = soredake(point, kazu, bonasu, goal);
      if (nokori < minimum) {
        minimum = nokori;
      }
    }
  }
  return minimum;
}

int main() {
  int d, g;
  cin >> d >> g;

  vector<vector<int>> questions(d, vector<int>(4));
  for (int i = 0; i < d; i++) {
    questions.at(i).at(0) = 100 * (i + 1);
    cin >> questions.at(i).at(1);
    cin >> questions.at(i).at(2);
    questions.at(i).at(3) =
        questions.at(i).at(0) * questions.at(i).at(1) + questions.at(i).at(2);
  }

  cout << search(questions, g) << endl;
}

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

int main() {
	int D, G;
	cin >> D >> G;
	vector<int> p(D), c(D);
	for (int i = 0; i < D; i++)
	{
		cin >> p.at(i) >> c.at(i);
	}

	int min_cnt = 1<<30;
	for (int bit = 0; bit < (1<<D); bit++)
	{
		int pnt = 0;
		int cnt = 0;
		for (int i = 0; i < D; i++)
		{
			if ((bit >> i) & 1)
			{
				pnt += p.at(i) * 100 * (i + 1) + c.at(i);
				cnt += p.at(i);
			}
		}
		if (pnt >= G) { min_cnt = min(cnt, min_cnt); continue; }
		else
		{
			for (int i = D-1; i >= 0; i--) {
				if ((bit >> i) & 1) continue;
				for (int j = 0; j < p.at(i)-1; j++)
				{
					if (pnt >= G) { min_cnt = min(cnt, min_cnt); continue; }
					pnt += 100 * (i + 1);
					cnt++;
				}
			}
			if (pnt >= G) min_cnt = min(cnt, min_cnt);
		}
	}
	cout << min_cnt << endl;
}


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

int main() {
  vector<int> x(3);
  int k, y;
  for(int i=0;i<3;i++) cin >> x.at(i);
  cin >> k;
  sort(x.begin(), x.end());
  y = x.at(2);
  for(int i=0;i<k;i++) y = 2*y;
  cout << x.at(0) + x.at(1) + y << endl;
}

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

int main() {
 int a,b,c,k;
 cin >> a >> b >> c >> k;
 rep(i,k){
   if(max({a,b,c}) == a) a *= 2;
   else if(max({a,b,c}) == b) b *= 2;
   else c *= 2;
 }
 cout << a+ b + c << endl;
 }


# include <bits/stdc++.h>
# define sz(x) (int)((x).size())
# define F first
# define S second
# define pb push_back
# define ub upper_bound
# define lb lower_bound
# define all(x) x.begin(), x.end()
# define pqueue priority_queue
# define mset multiset
# define umap unordered_map
# define Speed() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
# define rep(i,n) for(int i=0;i<n;++i)
# define forn(i,n) for(int i=1;i<=n;++i)
# define lr(i,l,r) for(int i=l;(l>r?i>=r:i<=r);(l>r?--i:++i))

using namespace std;

template<class T> void umin(T &a, T b) { a = min(a, b); }
template<class T> void umax(T &a, T b) { a = max(a, b); }

typedef long long ll;
typedef pair<int, int> pii;

const int mod = 1e9+7;
const int N = 2e5+5;

void solve(){
	int a,b;
	cin>>a>>b;
	if(b==1){
		cout<<0;
	}
	else{
		int ans=1;
		int cur=a;
		while(cur<b){
			int prev=cur;
			for(int i=1;i<=prev && cur<b;++i){
				cur--;
				cur+=a;
				ans++;
			}
		}
		cout<<ans;
	}

}

int main(){
	Speed();
	int T=1;
	//cin>>T;
	while(T--)
		solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=305;
int a,b,c,tmp;
double n,dp[N][N][N];
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>tmp;
		if(tmp==1)a++;
		else if(tmp==2)b++;
		else c++;
	}
	for(int i=0;i<=n;i++)
	{
		for(int j=0;j<=n;j++)
		{
			for(int k=0;k<=n;k++)
			{
				if(!i&&!j&&!k)continue;
				if(i)dp[i][j][k]+=dp[i-1][j+1][k]*i;
				if(j)dp[i][j][k]+=dp[i][j-1][k+1]*j;
				if(k)dp[i][j][k]+=dp[i][j][k-1]*k;
				dp[i][j][k]=dp[i][j][k]/(i+j+k);
				dp[i][j][k]+=n/(i+j+k);
			}
		}
	}
	cout<<fixed<<setprecision(12)<<dp[c][b][a]<<endl;
	return 0;
}

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

long long dp[200003][3];
long long ara[200003],n;

long long solve(long long idx,long long sta,long long lim)
{

    long long rr=lim-sta;

    if(idx+rr==n)return ara[idx];
    else if(dp[idx][sta]!=-1)return dp[idx][sta];
    else
    {

        long long p;

        p=ara[idx]+solve(idx+2,sta,lim);

        if(sta+1<=lim)
        {
            p=max(p,ara[idx]+solve(idx+3,sta+1,lim));
        }

        dp[idx][sta]=p; return dp[idx][sta];

    }

}

int main()
{

    long long a,i,j,st=1,ans=LLONG_MIN;

    scanf("%lld",&n);

    for(i=1;i<=n;i++)
    {
        scanf("%lld",&a);  ara[i]=a; dp[i][0]=-1; dp[i][1]=-1; dp[i][2]=-1;
    }

    if(n%2==1)
    {
        ans=max(ans,solve(1,0,2));

        st=2;
    }

    if(st==2){memset(dp,-1,sizeof dp);}
    ans=max(ans,solve(st,0,1));

    memset(dp,-1,sizeof dp);
    ans=max(ans,solve(st+1,0,0));

    printf("%lld\n",ans);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

map<int, int> num, pos;
set<int> s;
vector<int> v;

const int N = 1e5+10;
const int k = 30;

int up[N][k];
int L;
void calc(int i) {
	//cout << "this->" << *s.lower_bound(-(v[i]+L)) << endl;
	up[i][0] = num[*s.lower_bound(-(v[i]+L))];
	//cout << up[i][0] << endl;
	for(int j = 1; j < k; j++) {
		up[i][j] = up[up[i][j-1]][j-1];
	}
	/*cout << "i=" << i << ": " <<  endl;
	for(int j = 0; j < k; j++) cout << "k=" << j << " " << up[i][j] << endl;
	cout << endl;
	*/
}

int solve(int a, int b) {
	if(a>b) swap(a, b);
	int sum = 0;
	int cur = a;
	while(cur != b) {
		for(int j = 0; j < k-1; j++) {
			if(up[cur][j] >= b) {
				if(j==0) {
					sum++;
					cur = b;
				} else {
					cur = up[cur][j-1];
					sum += (1<<(j-1));
				}
				break;
			}
		}
	}

	return sum;
}

int main() {
	int n; cin >> n;
	v.resize(n+1);
	int cnt = 1;
	for(int i = 1; i <= n; i++) {
		int xi; cin >> xi;
		num[-xi] = cnt;
		pos[cnt] = xi;
		cnt++;
		s.insert(-xi);
		v[i] = xi;
	}
	cin >> L;
	for(int i = 0; i < k; i++) up[n+1][i] = n;
	for(int i = n; i > 0; i--) {
		calc(i);
	}

	int q; cin >> q;
	while(q--) {
		int a, b; cin >> a >> b;
		cout << solve(a, b) << "\n";
	}
}

// Date: 2020-03-19

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

typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii;
#define FIO ios::sync_with_stdio(false);cin.tie(0)
#define rep(i, a, b) for(int i = int(a); i <= int(b); ++i)
#define per(i, b, a) for(int i = int(b); i >= int(a); --i)
#define mem(x, y) memset(x, y, sizeof(x))
#define all(x) (x).begin(),(x).end()
#define D(x) cout<<#x<<" = "<<x<<endl;
#define SZ(x) ((int)x.size())
#define mk make_pair
#define pb push_back
#define fi first
#define se second
const LL INF = 1e18;
const LL mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
LL qpow(LL x, LL y, LL MOD) {LL a=1; while(y){ if(y&1) a=a*x%MOD; x=x*x%MOD; y>>=1; } return a;}

int r[N][30], x[N], n, L, Q, dep;

void init() {
    rep(i, 1, n) r[i][0] = upper_bound(x+i, x+n+1, x[i]+L)-x-1;
    rep(d, 1, 20) {
        rep(i, 1, n) {
            r[i][d] = r[r[i][d-1]][d-1];
        }
    }
}

int main() {
    FIO;
    cin >> n;
    rep(i, 1, n) cin >> x[i];
    cin >> L >> Q;
    init();
    while(Q--) {
        int u, v;
        cin >> u >> v;
        if(u > v) swap(u, v);
        int ans = 0;
        for(int i = 20; i >= 0; --i) {
            if(r[u][i] < v) {
                ans += (1 << i);
                u = r[u][i];
            }
        }
        cout << ans+1 << '\n';
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>
#define mat vector<vector<int>>
const int inf = 1 << 30;
const ll linf = 1e18;
const db EPS = 1e-7;
template<class T> void chmin(T& x, T y){if(x > y) x = y;}
template<class T> void chmax(T& x, T y){if(x < y) x = y;}

ll N, M;
vector<int> to[100010];
int idx[100010];
int edge[100010];
int node[100010];
int oe[100010];
int judge[100010];
int es[200010];
ll ans = 0;

void dfs(int v, int id, int type){
    idx[v] = id;
    node[id]++;
    oe[v] = type;
    for(auto u : to[v]){
        if(idx[u] != 0){
            if(oe[u] == type) judge[id] = true;
        }else{
            dfs(u, id, (type + 1) & 1);
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> M;
    ans = N * N;

    for(int i = 0; i < M; i++){
        int u, v;
        cin >> u >> v;
        es[i] = u;
        to[u].emplace_back(v);
        to[v].emplace_back(u);
    }

    int id = 1;
    for(int i = 1; i <= N; i++){
        if(idx[i] == 0) dfs(i, id++, 0);
    }
    for(int i = 0; i < M; i++){
        edge[idx[es[i]]]++;
    }
    ll nsum = 0, graph = 0, oc = 0;
    for(int i = 1; i < id; i++){
        if(node[i] == 1) continue;
        nsum += node[i];
        graph++;
        if(!judge[i]) oc++;
    }
    cout << ans - (nsum * nsum) + (graph * graph) + (oc * oc) << endl;
    return 0;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N=100010;
bool w[N],v[N];
int fa[N],sz[N];
inline int gi() {
    int x=0,o=1;
    char ch=getchar();
    while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    if(ch=='-') o=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*o;
}
inline int find(int x,int &y) {
    if(fa[x]==x) return x;
    fa[x]=find(fa[x],y);
    return v[x]^=y,y=v[x],fa[x];
}
int main() {
    int n,m,A=0,B=0,C=0;
    cin>>n>>m;
    for(int i=1;i<=n;i++) fa[i]=i,sz[i]=1;
    for(int i=1;i<=m;i++) {
	int X=0,Y=0,x=find(gi(),X),y=find(gi(),Y);
	if(x!=y) fa[x]=y,v[x]=X^Y^1,w[y]|=w[x],sz[y]+=sz[x];
	else w[x]|=X^Y^1;
    }
    for(int i=1;i<=n;i++)
	if(fa[i]==i) {
	    if(sz[i]==1) ++A;
	    else if(w[i]) ++B;
	    else ++C;
	}
    cout<<B+C+C+2LL*B*C+2LL*C*(C-1)+2LL*A*n-1LL*A*A+1LL*B*(B-1);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
#define cans cout << ans << endl
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
typedef long long ll;

int main(){
    int n,q;
    cin >> n >> q;
    string s;
    cin >> s;
    vector<int> sum(n+1);
    vector<int> l(q),r(q);
    rep(i,q) cin >> l[i] >> r[i];
    sum[0] = 0;
    for(int i=1; i<s.size(); i++){
        if(s[i-1]=='A' && s[i]=='C') sum[i+1]=sum[i]+1;
        else
        sum[i+1] = sum[i];
    }
    for(int i=0; i<q; i++){
        int ans = sum[r[i]]-sum[l[i]];
        cout << ans << endl;
    }
    return 0;
}

#include <iostream>
#include <map>
#include <set>
using namespace std;
int main()
{
  for(;;){
    int n;
    cin >> n;
    if(n==0) break;
    multimap< double,set<string> > list;
    for(int i=0; i<n; i++){
      string name;
      int p,a,b,c,d,e,f,s,m;
      cin >> name >> p >> a >> b >> c >> d >> e >> f >> s >> m;
      int num = f + f * (m-1);
      int time = a + b + c + (d + e) * m;
      double value = (num * s - p) / (double)time;
      multimap< double,set<string> >::iterator it = list.find(value);
      if(it != list.end()){
        (*it).second.insert(name);
      }else{
        set<string> s;
        s.insert(name);
        list.insert(make_pair< double,set<string> >(value,s));
      }
    }
    multimap< double,set<string> >::reverse_iterator rit = list.rbegin();
    while(rit != list.rend()){
      for(set<string>::iterator it = (*rit).second.begin(); it != (*rit).second.end(); it++){
        cout << (*it) << endl;
      }
      rit++;
    }
    cout << "#" << endl;
  }
  return 0;
}


#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
#define ll long long
const int mod = 1e9 + 7;
multiset<int> s;

int main() {
    int n;
    cin >> n;
    s.clear();
    for (int i = 1; i <= n; i++) {
        int tmp;
        scanf("%d", &tmp);
        auto id = s.lower_bound(tmp);
        if (id != s.begin()) s.erase(--id);
        s.insert(tmp);
    }
    cout << s.size();

}

#include <iostream>
#include <math.h>
using namespace std;
int main(void){
    int X;
    cin >> X;
    X = pow(X, 3);
    cout << X << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
int n, k;
string s[][7] = {{"aacd","bbcd","cdaa","cdbb"}, {"aa.ab",".bbab","a.cca","a.a.a","bbabb"}, {"aacd..","bbcd..","cd..aa","cd..bb","..aacd","..bbcd"},{"aacd...","bbcd...","cd.aa..","cd...bb","..a.baa","..a.b.b","..bbaab"}};
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin >> n;
	k = n%4;
	if(n < 3) cout << -1 << '\n';
	else if(n == 3) cout << "aab\nb.b\nbaa\n";
	else rep(i,n){
		string b(n,'.');
		if(i<n-k-4) b.replace(i/4*4,4,s[0][i%4]);
		else b.replace(n-k-4,k+4,s[k][i-n+k+4]);
		cout << b << '\n';
	}
}

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

const int N = 1003;
int n;
char ans[N][N];
char m4[4][5] = {
	"aacd",
	"bbcd",
	"cdaa",
	"cdbb"
};
char m5[5][6] = {
	"aabbc",
	"hii.c",
	"h..jd",
	"g..jd",
	"gffee"
};
char m6[6][7] = {
	"aacd..",
	"bbcd..",
	"..aacd",
	"..bbcd",
	"ab..aa",
	"ab..bb"
};
char m7[7][8] = {
	"aabbcc.",
	"dd.dd.a",
	"..d..da",
	"..d..db",
	"dd.dd.b",
	"..d..dc",
	"..d..dc",
};

char get(int x, int y, int type) {
	switch (type) {
		case 0 : return m4[x][y];
		case 1 : return m5[x][y];
		case 2 : return m6[x][y];
		case 3 : return m7[x][y];
		default : return '.';
	}
}
int main() {
	memset(ans, '.', sizeof(ans));
	scanf("%d", &n);
	if(n == 2) {printf("-1\n"); return 0;}
	else if(n == 3) {printf("aa.\n..a\n..a"); return 0;}
	for(int i = 0; i < 4 + n % 4; i++)
		for(int j = 0; j < 4 + n % 4; j++)
			ans[n - n % 4 - 4 + i][n - n % 4 - 4 + j] = get(i, j, n % 4);
	for(int k = 0; k * 4 < n / 4 * 4 - 4; k++)
		for(int i = 0; i < 4; i++)
			for(int j = 0; j < 4; j++)
				ans[4 * k + i][4 * k + j] = get(i, j, 0);
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < n; j++)
			printf("%c", ans[i][j]);
		printf("\n");
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<set>
#define MAX 30
#include<string>
#include<vector>
using namespace std;
int main(){
	int n;
	while(cin>>n,n){
		int k;
		cin>>k;
		vector<string> vec;
		vector<int> ind;
		set<string> st;
		bool isDone[10001];
		for(int i=0;i<10001;i++){
			isDone[i] = false;
		}

		for(int i=0;i<n;i++){
			string s;
			cin>>s;
			vec.push_back(s);
			ind.push_back(i);
		}
		sort(vec.begin(),vec.end());
//		cout<<"---"<<endl;
		do{
			int hash = 0;
			for(int i=0;i<k;i++){
				hash = hash*10 + ind[i];
			}
			if(isDone[hash]) continue;
			isDone[hash] = true;
			string t="";
			for(int i=0;i<k;i++){
				t+=vec[ind[i]];
			}
			st.insert(t);
//			cout<<"------"<<endl;
		}while(next_permutation(ind.begin(),ind.end()));
		cout<<st.size()<<endl;
	}
	return 0;
}

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int n,k,a[10],power[6];
vector<string>vec;
int main(){
	while(true){
		cin>>n>>k;vec.clear();power[0]=1;for(int i=1;i<5;i++)power[i]=power[i-1]*n;
		if(n==0 && k==0)break;
		for(int i=0;i<n;i++)cin>>a[i];
		for(int i=0;i<power[k];i++){
			int bit[4];for(int j=0;j<k;j++)bit[j]=(i/power[j])%n;
			bool OK=true;
			if(k==2 && (bit[0]==bit[1]))OK=false;
			if(k==3 && (bit[0]==bit[1] || bit[0]==bit[2] || bit[1]==bit[2]))OK=false;
			if(k==4 && (bit[0]==bit[1] || bit[0]==bit[2] || bit[1]==bit[2]))OK=false;
			if(k==4 && (bit[0]==bit[3] || bit[1]==bit[3] || bit[2]==bit[3]))OK=false;
			if(OK==true){
				string S="";
				for(int j=0;j<k;j++)S+=to_string(a[bit[j]]);
				vec.push_back(S);
			}
		}
		sort(vec.begin(),vec.end());int cnt=1;
		for(int i=1;i<vec.size();i++){
			if(vec[i-1]!=vec[i])cnt++;
		}
		cout<<cnt<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define ALL(v) v.begin(), v.end()
#define MOD 1000000007
#define MAX 510000
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

//cout << fixed << setprecision(10);//
//最小公倍数//
ll gcd(ll x, ll y) {
	if (x == 0) return y;
	return gcd(y%x, x);
}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }

//逆元mod(mを法とするaの逆元)//
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}

//Cmonp//
ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int 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;
	}
}

// 二項係数計算
ll COM(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

//nが大

ll com(ll n,ll m){
	if(n<=0 || m<=0 || n==m){
		return 1;
	}
	if(n<m){
		return 0;
	}
	ll k=1;
	for(ll i=1;i<=m;i++){
       k*=(n-i+1);
	   k%=MOD;
	   k*=modinv(i,MOD);
	   k%=MOD;
	}

	return k;
}

////////////////////////////////////////////////////////////////////

int main() {
ll n,m,k;
cin>>n>>m>>k;

ll ml=com(n*m-2,k-2);

ll ans=0;
for(int i=1;i<=n-1;i++){
	ans+=i*m*m*(n-i);
	ans%=MOD;
}

for(int i=1;i<=m-1;i++){
	ans+=i*n*n*(m-i);
	ans%=MOD;
}

ans*=ml;
ans%=MOD;
cout<<ans<<endl;

return 0;

}

#include <iostream>

using namespace std;

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int 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;
    }
}

long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
    long long n, m, k; cin >> n >> m >> k;

    COMinit();

    long long ans = 0;
    for (int i=0; i < n; ++i) {
        for (int j=0; j < m; ++j) {
            if (i * j == 0) {
                ans += (((n - i) * (m - j)) % MOD) * (i + j);
            } else {
                ans += 2 * ((((n - i) * (m - j)) % MOD) * (i + j) % MOD);
            }
            ans %= MOD;
        }
    }
    cout << (ans * COM(n*m-2, k-2)) % MOD << endl;
}


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

using ll=long long;

void solve(){
    int n;
    cin>>n;
    cout<<(n-2)*180<<endl;
}

signed main(){
    //while(1)
    solve();
}

#include <bits/stdc++.h>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REP(i,n) for(int (i)=0;(i)<=(int)(n);(i)++)
#define rep1(i,x,n) for(int (i)=(x);(i)<(int)(n);(i)++)
#define REP1(i,x,n) for(int (i)=(x);(i)<=(int)(n);(i)++)
#define rrep(i,x) for(int i=((int)(x)-1);i>=0;i--)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using Int = long long;

//INT_MAX = 2147483647;
//LLONG_MAX = 9223372036854775807;
//ULLONG_MAX = 18446744073709551615;
const int MOD = 1000000007;
const ll INF = numeric_limits<ll>::max();
const int inf = 1e8;
typedef pair<int,int> P;
typedef std::priority_queue<int> IntPrioQueue;
typedef std::priority_queue<int, std::vector<int>, std::greater<int> > IntReversePrioQueue;

//少数点表示
//cout << std::fixed << std::setprecision(14) << 値

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

    string ans="";

    ll n,m;
    cin >> n;
    m = n;

    while(m>0){
        m--;
        char s = 'a'+ m%26;
        ans.push_back(s);
        m /= 26;
    }
    reverse(ans.begin(),ans.end());
    cout << ans << endl;

    return 0;
}


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

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> a(h);
  for(int i = 0; i < h; i++) cin >> a.at(i);

  vector<vector<int>> check(h, vector<int>(w));
  int ans = -1;
  queue<pair<int, int>> bfs;
  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++){
      if(a.at(i).at(j) == '.') continue;
      bfs.push({i, j});
      check.at(i).at(j) = 1;
    }
  }
  vector<int> dx = {0, 0, -1, 1}, dy = {1, -1, 0, 0};
  while(bfs.size() > 0){
    ans++;
    queue<pair<int, int>> next;
    while(bfs.size() > 0){
      next.push(bfs.front());
      bfs.pop();
    }
    while(next.size() > 0){
      int y = next.front().first, x = next.front().second;
      next.pop();
      for(int i = 0; i < 4; i++){
        int nx = x + dx.at(i), ny = y + dy.at(i);
        if(nx < 0 || nx >= w || ny < 0 || ny >= h) continue;
        if(check.at(ny).at(nx)) continue;
        check.at(ny).at(nx) = 1;
        bfs.push({ny, nx});
      }
    }
  }

  cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i=0; i<(int)(n); i++)
int main(){
int H, W, ans=1; cin >> H >> W; string A[H];
REP(i, H){ cin >> A[i]; }
int B[H][W]={}; vector< pair<int, int> > C;
REP(i, H){ REP(j, W){
  if(A[i][j]=='#'){  B[i][j]=1; C.push_back(make_pair(i, j)); }
} }
REP(i, H*W){
if((int)C.size()==H*W){ cout << ans-1 << "\n"; return 0; }
int a=C[i].first, b=C[i].second, c=B[a][b]+1;
if(a>0&&!B[a-1][b]){ B[a-1][b]=c; C.push_back(make_pair(a-1, b)); }
if(b>0&&!B[a][b-1]){ B[a][b-1]=c; C.push_back(make_pair(a, b-1)); }
if(a<H-1&&!B[a+1][b]){ B[a+1][b]=c; C.push_back(make_pair(a+1, b)); }
if(b<W-1&&!B[a][b+1]){ B[a][b+1]=c; C.push_back(make_pair(a, b+1)); }
ans=c;
}
}


// REDO

#include <iostream>

using namespace std;

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

  long long cnt = 0, b = 0, a;
  for (int i = 0; i < N; ++i) {
    cin >> a;
    if (a > x) {
      cnt += a - x;
      a = x;
    }
    if (b + a > x) {
      cnt += b + a - x;
      b = x - b;
    } else {
      b = a;
    }
  }

  cout << cnt << endl;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a[4],e,f;
    for(int i=0;i<4;i++){
        cin>>a[i];
    }

    cin>>f>>e;

    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            if(a[j]<a[j+1]){
                swap(a[j],a[j+1]);
            }
        }
    }
    if(e<f){
        swap(e,f);
    }
    cout<<a[0]+a[2]+a[1]+e<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <map>
#include <sstream>
#include <cstdio>
#include <complex>
#include <cstring>
using namespace std;

const int MAX= 10000100;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
#define ll long long int
#define gcd(a,b) __gcd(a,b)
#define pb(a) push_back(a)
int GCD(int a, int b) {if(!b) return a; return gcd(b, a%b);}
int lcm(int a, int b) {return a*b / gcd(a, b);}

int m[100][100];
int sx,sy,gx,gy;
int w,h;
bool dfs(int x,int y,int c){
  if(x==gx&&y==gy)return true;
  if(m[y][x] == 0)return false;
  m[y][x] = 0;
  //cout<<x<<" "<<y<<" "<<c<<endl;
  int dx[] = {0,0,-1,1}, dy[] = {1,-1,0,0};
  bool judge=false;
  rep(i,4){
    if(m[y+dy[i]][x+dx[i]] == c)
      judge |= dfs(x+dx[i],y+dy[i],c);
  }
  return judge;
}

int main(void){
  while(cin>>h>>w,h+w){
    rep(i,h)rep(j,w)m[i][j]=0;

    cin>>sx>>sy>>gx>>gy;

    int n,c,d,x,y;
    cin>>n;
    rep(i,n){
      cin>>c>>d>>x>>y;
      //cout<<c<<" "<<d<<" "<<x<<" "<<y<<endl;
      if(d){
	rep(i,4)rep(j,2)m[i+y][j+x] = c;
      }else{
	//cout<<x<<" "<<y<<endl;
	rep(i,2)rep(j,4)m[i+y][j+x] = c;
      }
    }
    //m[sy][sx] = 8; m[gy][gx] = 9;
    //cout<<"  ";rep(i,w)cout<<i%10<<" ";cout<<endl;
    //rep(i,h){cout<<i%10<<" ";rep(j,w)cout<<m[i][j]<<" ";cout<<endl;}
    cout<<(dfs(sx,sy,m[sy][sx])?"OK":"NG")<<endl;
  }
}

#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cfloat>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>

using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define EACH( v, c ) for ( auto &v : c )

#define ITER( c ) __typeof( (c).begin() )
#define IREP( c, it ) for ( ITER(c) it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

const int dir[][2] = {
	1, 0,
	0, 1,
	-1, 0,
	0, -1
};

string solve( VVI board, int sy, int sx, int gy, int gx )
{
	const int H = board.size(), W = board[0].size();

	int color = board[ sy ][ sx ];

	queue< PII > que;
	que.push( MP( sy, sx ) );

	set< PII > visited;
	visited.insert( MP( sy, sx ) );

	while ( !que.empty() )
	{
		auto cur = que.front();
		que.pop();

		if ( cur == MP( gy, gx ) )
		{
			return "OK";
		}

		REP( d, 0, 4 )
		{
			auto next = cur;

			next.fst += dir[d][0];
			next.snd += dir[d][1];

			if ( !( 0 <= next.fst && next.fst < H && 0 <= next.snd && next.snd < W ) ||
				 EXIST( visited, next ) ||
				 board[ next.fst ][ next.snd ] != color )
			{
				continue;
			}

			que.push( next );
			visited.insert( next );
		}
	}

	return "NG";
}

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

	while ( true )
	{
		int w, h;
		cin >> w >> h;

		if ( !( w | h ) )
		{
			break;
		}

		int sx, sy, gx, gy, n;
		cin >> sx >> sy >> gx >> gy >> n;

		sx--;
		sy--;
		gx--;
		gy--;

		VVI board( h, VI( w, 0 ) );

		REP( i, 0, n )
		{
			int c, d, x, y;
			cin >> c >> d >> x >> y;

			x--;
			y--;

			if ( !d )
			{
				REP( j, 0, 2 )
				{
					REP( k, 0, 4 )
					{
						board[ y + j ][ x + k ] = c;
					}
				}
			}
			else
			{
				REP( j, 0, 4 )
				{
					REP( k, 0, 2 )
					{
						board[ y + j ][ x + k ] = c;
					}
				}
			}
		}

		cout << solve( board, sy, sx, gy, gx ) << endl;
	}

	return 0;
}


// #Sazaの1日1AC
#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
const int INF=mod;
const double PI=3.14159265358979323846;

//マクロ
//型エイリアス
using ll = long long;
using P=pair<int, int>;
using vl=vector<long long>;
using vvl=vector<vector<long long>>;
using vP=vector<pair<long long, long long>>;
//ショートカット
#define rep(i,n) for(int i = 0; i < n; i++)
#define rep2(i,k,n) for(int i = k; i < n; i++) //半開区間
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define mkp make_pair
//入力
#define vin(v,N) for(long long id=0;id<N;id++) cin>>v.at(id)
#define lin(n) long long n;cin>>n
#define chin(x) char x;cin>>x;
#define sin(s) string s;cin>>s;
#define vlin(v,N) vector<long long>v(N);for(long long i=0;i<N;i++)cin>>v.at(i)

//関数
//最大公約数
long long gcd(long long m, long long n){
  long long a=max(m,n);
  long long b=min(m,n);
  long long r=a%b;
  while(r!=0){
    a=b; b=r; r=a%b;
  }
  return b;
}
// 最小公倍数
long long lcd(long long m, long long n){
  return m*n/gcd(m,n);
}
//繰り返し二乗法
long long repeat_squaring(long long x, long long n){
  if(n==0) return 1;
  else if(n%2==0){
    long long t=repeat_squaring(x,n/2);
    return t*t%mod;
  }
  else
    return x*repeat_squaring(x,n-1);
}
//素因数分解(mapで返す)
map<long long,long long> factor(long long p){
  ll p1=p;
  map<long long,long long>ret;
  for(long long i=2;i*i<=p1;i++){
     while(p%i==0){
      ret[i]++;
      p/=i;
    }
  }
  if(p!=1)
  ret[p]++;
  return ret;
}
//素数判定
bool is_prime(long long N){
  for(long long i=2;i*i<=N;i++){
    if(N%i==0)
      return false;
  }
  return true;
}
//最大値更新
void chmax(long long &a,long long b){
  a=max(a,b);
}
//最小値更新
void chmin(long long &a,long long b){
  a=min(a,b);
}

//構造体

/////////////////⊂('ω'⊂ )))Σ≡GO!/////////////////
int dh[] = {1, 0, -1, 0};
int dw[] = {0, 1, 0, -1};

int main(){
  int H, W; cin >> H >> W;
  vector<vector<char>> grid(H, vector<char>(W));
  rep(i, H) rep(j, W) cin >> grid[i][j];

  vector<vector<int>> table(H, vector<int>(W, -1));

  queue<P> Q;
  rep(i, H){
    rep(j, W){
      if(grid[i][j] == '#'){
        Q.push(mkp(i, j));
        table[i][j] = 0;
      }
    }
  }

  while(Q.size() > 0){
    int h, w;
    tie(h, w) = Q.front(); Q.pop();

    rep(i, 4){
      int nh = h + dh[i], nw = w + dw[i];
      if(0 <= nh && nh < H && 0 <= nw && nw < W && table[nh][nw] == -1){
        table[nh][nw] = table[h][w] + 1;
        Q.push(mkp(nh, nw));
      }
    }
  }

  int res = -1;

  rep(i, H){
    rep(j, W){
      res = max(res, table[i][j]);
      //cout << table[i][j];
    }
  }

  cout << res << endl;
}

#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;
int main() {
  int N,T;
  cin >> N >> T;
  int A[N],B[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
  }
  int DP[N][T+1];
  int invDP[N][T+1];
  for (int i = 0; i <= T; i++) {
    if (A[0] <= i) {
      DP[0][i] = B[0];
    } else {
      DP[0][i] = 0;
    }
  }
  for (int i = 0; i <= T; i++) {
    if (A[N-1] <= i) {
      invDP[N-1][i] = B[N-1];
    } else {
      invDP[N-1][i] = 0;
    }
  }

  for (int i = 1; i < N; i++) {
    for (int j = 0; j <= T; j++) {
      if (A[i] > j) {
        DP[i][j] = DP[i-1][j];
      } else {
        DP[i][j] = max(DP[i-1][j],DP[i-1][j-A[i]] + B[i]);
      }
    }
  }

  for (int i = N-2; i >= 0; i--) {
    for (int j = 0; j <= T; j++) {
      if (A[i] > j) {
        invDP[i][j] = invDP[i+1][j];
      } else {
        invDP[i][j] = max(invDP[i+1][j],invDP[i+1][j-A[i]] + B[i]);
      }
    }
  }
  int ans = 0;
  for (int i = 1; i < N-1; i++) {
    int res = 0;
    for (int j = 0; j <= T-1; j++) {
      res = max(res,DP[i-1][j] + invDP[i+1][T-1-j]);
    }
    ans = max(ans,res+B[i]);
  }
  ans = max(ans,B[0] + invDP[1][T-1]);
  ans = max(ans,B[N-1] + DP[N-2][T-1]);
  cout << ans << endl;
  return 0;
}


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

struct node{
    int key;
    node *p,*l,*r;
};

int n;
node *N;

void tree_insert(node *z){
    node *y=NULL;
    node *x=N;
    while(x!=NULL){
        y=x;
        if(x->key>z->key)x=x->l;
        else if(x->key<z->key)x=x->r;
    }
    z->p=y;
    if(y==NULL)N=z;
    else if(y->key<z->key)y->r=z;
    else y->l=z;
}

node *find_num(int num){
    node *x=N;
    while(x!=NULL){
        if(x->key==num){
            return x;
        }
        if(x->key>num)x=x->l;
        else x=x->r;
    }
    return x;
}

void tree_find(int num){
    node *x=find_num(num);
    if(x!=NULL)cout<<"yes"<<endl;
    else cout<<"no"<<endl;
}

void in(node *x){
    if(x->l!=NULL)in(x->l);
    cout<<" "<<x->key;
    if(x->r!=NULL)in(x->r);
}

void pre(node *x){
    cout<<" "<<x->key;
    if(x->l!=NULL)pre(x->l);
    if(x->r!=NULL)pre(x->r);
}

node* successor(node *z){
    node *x;
    x=z->r;
    while(x->l!=NULL)x=x->l;
    return x;
}

void tree_delete(node *z){
    node *y;
    if(z->l==NULL||z->r==NULL)y=z;
    else y=successor(z);
    node *x;
    if(y->l!=NULL)x=y->l;
    else x=y->r;
    if(x!=NULL)x->p=y->p;

    if(y->p==NULL) N=x;
    else if(y==y->p->l)y->p->l=x;
    else y->p->r=x;

    if(y!=z) z->key=y->key;
    delete(y);
}

void print(){
    node *x=N;
    in(x);
    cout<<endl;
    pre(x);
    cout<<endl;
}

int main()
{
    N=NULL;
    cin>>n;
    int i;
    for(i=0;i<n;i++){
        string s;
        cin>>s;
        if(s=="insert"){
            int num;
            cin>>num;
            node *z=new node();
            z->key=num;z->l=z->r=z->p=NULL;
            tree_insert(z);
        }
        if(s=="find"){
            int num;
            cin>>num;
            tree_find(num);
        }
        if(s=="print"){
            print();
        }
        if(s=="delete"){
            int num;
            cin>>num;
            node *z=find_num(num);
            tree_delete(z);
        }
    }
    return 0;
}

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define db long double
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define mp make_pair
#define all(x) (x).begin(), (x).end()

void dout() { cerr << '\n'; }

template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
    cerr << " " << H;
    dout(T...);
}

#ifdef LOCAL
    #define dbg(...) cerr << #__VA_ARGS__, dout(__VA_ARGS__)
#else
    #define dbg(...) ;
#endif

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair <int, int> pii;

const int N = 1003;
char ans[4][7][7], m[N][N];
string arr[4] = {"adeeadffbbghccgh",
"adf..adf..beehhb.giiccgjj",
"adgg..adhh..beii..be..jjcf..kkcf..ll",
"adg....adg....be.ii..be.jj..cff..llc.h..mm..hkknn"
};

void parse(int r) {
    int a = 4 + r;
    for (int i = 0; i < a; i++) {
        for (int j = 0; j < a; j++) {
            ans[r][i][j] = arr[r][i * a + j];
        }
    }
}

void spread(int x, int r) {
    int a = 4 + r;
    for (int i = x; i < x + a; i++) {
        for (int j = x; j < x + a; j++) {
            m[i][j] = ans[r][i - x][j - x];
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL);
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
    #endif

    for (int i = 0; i < 4; i++) {
        parse(i);
    }

    int n;
    cin >> n;
    if (n <= 2) {
        cout << -1;
        return 0;
    }
    if (n == 3) {
        cout << "acc\na.d\nbbd";
        return 0;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            m[i][j] = '.';
        }
    }
    int rem = n % 4;
    spread(0, rem);
    int times = (n - (4 + rem)) / 4, cur = 4 + rem;
    while (times--) {
        spread(cur, 0);
        cur += 4;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << m[i][j];
        }
        cout << '\n';
    }
}


#include <iostream>
#include <string>
#include <vector>

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

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main() {
  int A, B;
  cin >> A >> B;
  int mx;
  mx = std::max(A+B, std::max(A-B, A*B));

  cout << mx << endl;

  return 0;
}

#include <bits/stdc++.h>

#define REP(i, x) REPI(i, 0, x)
#define REPI(i, a, b) for (int i = int(a); i < int(b); ++i)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
using namespace std;

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

  int A, B, C;
  cin >> A >> B >> C;
  int tmp = A + B;
  tmp = max(tmp, A - B);
  tmp = max(tmp, A * B);
  cout << tmp << endl;

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl << endl;}
#define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll,ll> P;

//ａをbで割る
long long mod_div(long long a,long long b){
	if(a % b == 0)return a/b;
	long long tmp = MOD - 2,c = b,ret = 1;
	while(tmp > 0){
		if(tmp & 1){
			ret *= c;ret %= MOD;
		}
		c *= c;c %= MOD;tmp >>= 1;
	}
	return a*ret%MOD;
}

#define MAX_K 333333
vector<long long> kaijo(MAX_K);
long long combination(long long n, long long r){
    if(n < r || n < 0 || r < 0)
        return 0;
	if(kaijo[0] != 1){
		kaijo[0] = 1;
		for(long long i = 1;i < MAX_K;i++)kaijo[i] = (kaijo[i-1] * i) % MOD;
	}
    long long ret = kaijo[n];
	long long tmp = (kaijo[r] * kaijo[n-r]) % MOD;
    return mod_div(ret,tmp);
}

ll mpow(ll a,ll b){
	ll ret = 1;
	ll tmp = a;
	for(ll i = 0;i <= 60;i++){
		if(b & (1LL << i)){
			ret *= tmp;
			ret %= MOD;
		}
		tmp *= tmp;
		tmp %= MOD;
	}
	return ret;
}

ll comb[1111][1111];
ll dp[1111][1111];

int main(){

	ll n,a,b,c,d;cin >> n >> a >> b >> c >> d;
	dp[0][0] = 1;

	REP(i,1111){
		REP(j,1111){
			if(j == 0 || i == j)comb[i][j] = 1;
			else comb[i][j] = (comb[i-1][j] + comb[i-1][j-1]) % MOD;
		}
	}

	REP(i,b+1){
		REP(j,n+1)if(dp[i][j]){
			dp[i+1][j] += dp[i][j];
			dp[i+1][j] %= MOD;
			if(i < a)continue;
			ll kake = 1;
			for(ll k = 1;k <= d;k++){
				ll tmp = i * k;
				if(j + tmp > n)break;
				kake = (kake * comb[n-j-tmp+i][i]) % MOD;
				kake = mod_div(kake, k);
				if(k < c)continue;
				dp[i+1][j+tmp] += (dp[i][j] * kake) % MOD;
				dp[i+1][j+tmp] %= MOD;
			}
		}
	}

	SHOW2d(comb,10,10);
	SHOW2d(dp,10,10);

	cout << dp[b+1][n] << endl;

	return 0;
}


#include <algorithm>
#include <array>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include <cassert>

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

int gcd(int a, int b) {
    if(a < b) {
        swap(a, b);
    }
    if(b < 1)
        return -1;
    if(a % b == 0)
        return b;
    return gcd(b, a % b);
}

int main() {
    int N, K;
    cin >> N >> K;
    vector<int> A(N);
    rep(i, N)
        cin >> A[i];

    if(A.size() == 1) {
        if(A[0] != K) {
            cout << "IMPOSSIBLE" << endl;
        } else {
            cout << "POSSIBLE" << endl;
        }
        return 0;
    }

    sort(A.begin(), A.end());
    if(A.back() < K) {
        cout << "IMPOSSIBLE" << endl;
        return 0;
    }

    int gcd_val = A[0];
    for(int i=1; i<N; ++i) {
        gcd_val = gcd(gcd_val, A[i]);
    }

    rep(i, N) {
        if(A[i] == K) {
            cout << "POSSIBLE" << endl;
            return 0;
        }
        if(A[i] < K) {
            continue;
        }
        A[i] -= K;

        if( A[i] % gcd_val == 0) {
            cout << "POSSIBLE" << endl;
            return 0;
        }
    }
        cout << "IMPOSSIBLE" << endl;

    return 0;
}

#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }

//素因数分解
template <typename T>
vector<pair<T, T>> prime_factor(T n) {
    vector<pair<T, T>> ret;
    T tmp=0;
    if(n%2==0){
        tmp=0;while(n%2==0){tmp++;n/=2;}
        ret.push_back(make_pair(2, tmp));
    }
    if(n%3==0){
        tmp=0;while(n%3==0){tmp++;n/=3;}
        ret.push_back(make_pair(3, tmp));
    }
    for (T i=5;i*i<=n;i+=4) {
        if(n%i==0){
            tmp=0;while(n%i==0){tmp++;n/=i;}
            ret.push_back(make_pair(i,tmp));
        }
        i+=2;
        if(n%i==0){
            tmp=0;while(n%i==0){tmp++;n/=i;}
            ret.push_back(make_pair(i,tmp));
        }
    }
    if(n!=1)ret.push_back(make_pair(n,1));
    return ret;
}

int main(){
    ll a,b;
    in(a,b);
    ll gcdv=__gcd(a,b);
    vector<pair<ll,ll>> prime=prime_factor(gcdv);
    out(prime.size()+1);
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define MOD (long long)(1e9+7)
#define INF (1LL<<60)
#define rep(i,n) for(ll i = 0; i < (n); i++)
#define rep1(i,n) for(ll i = 1; i <= (n); i++)

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

// 最大公約数
ll gcd(ll a, ll b)
{
  if(b == 0) return a;
  return gcd(b, a % b);
}

// mod m におけるa の逆元
ll modinv(ll a, ll m) {
  ll b = m, u = 1, v = 0;
  while (b) {
    ll t = a / b;
    a -= t * b; swap(a, b);
    u -= t * v; swap(u, v);
  }
  u %= m;
  if (u < 0) u += m;
  return u;
}

// 素因数分解
vector<pair<ll, ll>> prim;
void pf(ll n)
{
  ll s = sqrt(n);
  ll r = 0;
  for(ll i = 2; i <= s; i++) {
    if((n % i) == 0) {
      r = 0;
      do {
	r++;
	n = n / i;
      } while((n % i) == 0);
      prim.push_back({i, r});
    }
  }
  if(n > s) {
    prim.push_back({n, 1});
  }
}

void solve()
{
  ll N; cin >> N;
  vector<ll> a(N + 1); rep1(i, N) cin >> a[i];
  vector<ll> b(N + 1, 0);

  ll ans = 0;
  bool ok = true;

  for(ll i = N; i > 0; i--) {
    ll cnt = 0;
    for(ll j = i; j <= N; j += i) {
      cnt += b[j];
    }
    b[i] = (cnt + a[i]) % 2 ;
    //    cerr << i << "]" << cnt << "->" << b[i] << endl;
    ans += b[i];
  }
  cout << ans << endl;
  if(ans > 0) {
    rep1(i, N) {
      if(b[i] == 1) cout << i << " " ;
    }
    cout << endl;
  }

}

int main(void)
{
  // ll t; cin >> t; rep(i, t)
  solve();
}

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second
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; }
const int INF = (1<<30) - 1;
const ll LINF = (1LL<<60) - 1;

vector<ll> enum_divisors(ll n){
    vector<ll> res;
    for (ll i = 1; i * i <= n; ++i) {
        if (n % i == 0){
            res.push_back(i);
            if (n/i != i) res.push_back(n/i);
        }
    }
    sort(res.begin(), res.end());
    return res;
}

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

    vector<int> sum(n+1, 0);
    vector<int> num(n+1, -1);
    vector<int> res;
    for (int i = n-1; i >= 0; --i) {
        int tmp;
        if (sum[i+1] % 2 != a[i]) {
            tmp = 1;
        } else {
            tmp = 0;
        }

        if (num[i+1] != -1 && num[i+1] != tmp) {
            cout << -1 << endl;
            return 0;
        }
        num[i+1] = tmp;
        if (tmp == 1) {
            res.push_back(i+1);
            auto V = enum_divisors(i+1);
            for (auto v : V) {
                sum[v]++;
            }
        }
    }
    cout << res.size() << endl;
    for (auto v : res) {
        cout << v << endl;
    }
}


/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.08.18 17:43:20
**/

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

int main() {
    int m;cin >> m;
    ll total = 0;
    ll ans = 0;
    for (int i = 0; i < m; i++) {
        ll d,c;cin >> d >> c;
        ans += c;
        total += (d * c - 1) % 9 + 1;
        ans += (d * c - 1) / 9;
    }
    ans += (total - 1) / 9;
    cout << ans - 1 << endl;
    return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for (ll i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)
using namespace std;

typedef long long ll;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline void dump(vector<T> v) { for (auto& x : v) cerr << x << " "; cerr << endl; }
template<class T> inline void dump(vector<pair<T, T>> v) { for (auto& p : v) cerr << p.first << " " << p.second << endl; }
template<class T> inline void dump(vector<vector<T>> vv) { for (auto& v : vv) {for (auto& x : v) cerr << x << " "; cerr << endl;} }

const ll INF = 1e9;
const long long INFL = 1LL<<60;

int main() {
  int m;
  cin >> m;

  map<ll, ll> mp;

  rep(i, m) {
    ll d, c;
    cin >> d >> c;
    mp[d] += c;
  }

  ll ans = 0;

  bool flag = true;
  while (flag) {
    flag = false;
    rep(i, 10) {
      if (mp[i] > 1) flag = true;
      ll num = mp[i] / 2;
      ans += num;
      mp[i] -= num * 2;
      if (i + i < 10) mp[i+i] += num;
      else {
        mp[1] += num;
        mp[(i + i) % 10] += num;
      }
    }
  }

  vector<ll> a;
  rep(i, 10) {
    if (mp[i] != 0) a.push_back(i);
  }

  while (a.size() > 1) {
    int x = a.back(); a.pop_back();
    int y = a.back(); a.pop_back();
    if (x + y < 10) a.push_back(x + y);
    else {
      a.push_back(1);
      a.push_back((x + y) % 10);
    }
    ans++;
  }

  cout << ans << endl;

  return 0;
}


#include<stdio.h>
#define MAX 100000
int A[MAX], n;

int partition(int p, int r){
	int tmp,x = A[r];
	int j,i = p - 1;
	for (j = p; j < r; j++){
		if (A[j] <= x){
			i = i + 1;
			tmp = A[i];
			A[i] = A[j];
			A[j] = tmp;
		}
	}
	tmp = A[i+1];
	A[i+1] = A[r];
	A[r] = tmp;
	return i + 1;
}

int main(){
	int i,j;
	scanf("%d", &n);
	for (i = 0; i < n; i++){
		scanf("%d", &A[i]);
	}
	j = partition(0, n-1);
	for (i = 0; i < n; i++){
		if (i){
			printf(" ");
		}
		if (i == j){
			printf("[%d]", A[i]);
		}
		else{
			printf("%d", A[i]);
		}
	}
	printf("\n");
	return 0;
}

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

int partition(vector<int>& A, int p, int r){
    int i = p - 1;
    for (int j = p; j < r; j++) {
        if (A[j] <= A[r]) {
            i++;
            swap(A[i], A[j]);
        }
    }
    i++;
    swap(A[i], A[r]);
    return i;
}

int main(){
    int n;
    cin >> n;
    vector<int> A(n);
    for (auto& a : A) cin >> a;
    int pos = partition(A, 0, n - 1);
    for (int i = 0; i < pos; i++) {
        cout << A[i] << " ";
    }
    cout << "[" << A[pos] << "] ";
    for (int i = pos + 1; i < n; i++) {
        cout << A[i] << (i < n - 1 ? " " : "\n");
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <set>
using namespace std;

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

template <typename T>
struct RUQ //calculateはminimum
{
    int n;
    const T ex = 1 << 30; //dat 初期値(minなので)
    const T em = 1 << 30; //lazy 初期値(minなので)
    vector<T> dat, lazy; //tree本体
    RUQ(vector<T> v){
        int x = 1;
        int i;
        n = v.size();
        while(x < n) x <<= 1;
        n = x;
        dat.resize(2*n-1, ex);
        lazy.resize(2*n-1, em);
        for(i = 0;i < v.size();++i){
            dat[i + n - 1] = v[i];
        }
        for(i = n-2;i >= 0;--i){
            dat[i] = calculate(dat[2*i+1], dat[2*i+2]);
        }
    }

    T calculate(T a, T b){
        return min(a, b); //minで使う
    }

    void eval(int now){
        if(lazy[now] == em) return;
        if(now < n - 1){ //最小値なのでそのまま代入
            lazy[2*now + 1] = lazy[now];
            lazy[2*now + 2] = lazy[now];
        }
        dat[now] = lazy[now];
        lazy[now] = em;
    }

    void update(int a, int b, T x, int now, int l, int r){
        eval(now);
        if(a <= l && r<= b){
            lazy[now] = x; //上から書き換える。最小値なので代入する
            eval(now);
        }else if(a < r && l < b){
            update(a, b, x, 2*now + 1, l, (l + r)/2);
            update(a, b, x, 2*now + 2, (l + r)/2, r);
            dat[now] = calculate(dat[2*now + 1], dat[2*now + 2]);
        }
    }
    void update(int a, int b, T x){update(a, b, x, 0, 0, n);}

    T query(int a, int b){return query_sub(a, b, 0, 0, n);}
    T query_sub(int a, int b, int now, int l, int r){
        eval(now);
        if(r <= a || b <= l) return ex;
        else if(a <= l && r <= b) return dat[now];
        else{
            T vl = query_sub(a, b, 2*now + 1, l, (l + r)/2);
            T vr = query_sub(a, b, 2*now + 2, (l + r)/2, r);
            return calculate(vl, vr);
        }
    }

    inline T operator[](int a) { return query(a, a + 1); }
    void print() {
        for (int i = 0; i < 2 * n - 1; ++i) {
            cout << (*this)[i];
            if (i != n) cout << ",";
        }
        cout << endl;
    }
};

int main()
{
    cin.tie(0); ios::sync_with_stdio(false);
    int i;
    int h,w;
    cin >> h >> w;
    vector<int> v(w, 0);
    const int INF = 1e9;
    set<int> st;
    rep(i,w) st.insert(i);
    RUQ<int> ruq(v);
    rep(i,h){
        int a,b;
        cin >> a >> b;
        --a;    --b;
        auto x = st.lower_bound(a);
        auto y = st.upper_bound(b);
        if(x == y){
            ruq.update(a, b+1, INF);

            int ans = ruq.query(0, w);

            if(ans == INF) cout << -1 << endl;
            else cout << ans + i + 1 << endl;
        }else if(b+1 == w){
            st.erase(x, y);
            ruq.update(a, b+1, INF);

            int ans = ruq.query(0, w);

            if(ans == INF) cout << -1 << endl;
            else cout << ans + i + 1 << endl;
        }else{
            auto it = prev(y);
            int ind = *it;
            st.erase(x, y);
            st.insert(b+1);
            int tmp = ruq.query(ind, ind+1) + (b + 1 - ind);
            ruq.update(b+1, b+2, min(tmp, ruq.query(b+1, b+2)));
            ruq.update(a, b+1, INF);

            int ans = ruq.query(0, w);

            if(ans == INF) cout << -1 << endl;
            else cout << ans + i + 1 << endl;
        }
    }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef pair<long,long> P;
int main(){
  long H,W;cin>>H>>W;
  set<P> S,T;
  for(long i=0;i<W;i++){S.insert(P(i,0));T.insert(P(0,i));}
  for(long k=0;k<H;k++){
    long a,b;cin>>a>>b;a--;
    long y=LONG_MAX;
    for(auto itr=S.lower_bound(P(a,-1));itr!=S.end()&&(itr->first)<b;){
      P x=*itr;
      y=min(y, x.second+b-x.first);
      T.erase(P(x.second,x.first));
      itr=S.erase(itr);
    }// ゴール: 移動回数 i,jとして, b, j+b-i, このうち, 最も移動回数が少ないものだけ残す
    if(y!=LONG_MAX&&b<W){S.insert(P(b,y)); T.insert(P(y,b));}
    cout<<(T.size()?(T.begin()->first)+k+1:-1)<<endl;
  }
}

/*

S: ゴール, 何回の移動でいけるか
T: 何回の移動でいけるか, ゴール

T.begin()を持って来ればいける
Sで更新を素早くみる

*/


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
int main(){
    string a;
    int num[101],cou=0,ans;
    while(cin>>a){
        for(int i=0;i<a.size();i++){
        if(a[i]=='I')num[i]=1;
        if(a[i]=='V')num[i]=5;
        if(a[i]=='X')num[i]=10;
        if(a[i]=='L')num[i]=50;
        if(a[i]=='C')num[i]=100;
        if(a[i]=='D')num[i]=500;
        if(a[i]=='M')num[i]=1000;
    }
    ans=num[a.size()-1];
    for(int i=a.size()-1;i>0;i--){
        if(num[i]>num[i-1])ans=ans-num[i-1];
        if(num[i]<=num[i-1])ans=ans+num[i-1];
    }
    cout<<ans<<endl;
}
return 0;
}

#include <iostream>
using namespace std;

int convert( char c )
{
    switch ( c )
    {
    case 'I':   return 1;
    case 'V':   return 5;
    case 'X':   return 10;
    case 'L':   return 50;
    case 'C':   return 100;
    case 'D':   return 500;
    case 'M':   return 1000;
    }
}

int main( void )
{
    string input;
    int answer, prev;
    while ( cin >> input )
    {
        answer = 0;
        prev = 0;

        for ( string ::reverse_iterator it = input.rbegin(); it != input.rend(); it++ )
        {
            int next = convert( *it );
            if ( prev <= next )
            {
                answer += next;
            }
            else
            {
                answer -= next;
            }
            prev = next;
        }

        cout << answer << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define re register
#define SIZE 10005
#define LL long long
#define MOD 1000000007
using namespace std;
string k;
int d;
int maxn[SIZE],cnt;
LL dp[SIZE][105];
inline LL dfs(int pos,int sta,bool lim)
{
	if(pos>cnt)
		return sta==0?1:0;
	if(!lim && dp[pos][sta]!=-1)
		return dp[pos][sta];
	int up=lim?maxn[pos]:9;
	LL ret=0;
	for(re int i=0;i<=up;++i)
		ret=(ret+dfs(pos+1,(sta+i)%d,lim&&(i==maxn[pos])))%MOD;
	if(!lim)
		dp[pos][sta]=ret;
	return ret;
}
inline LL work()
{
	memset(dp,-1,sizeof(dp));
	cnt=k.size()-1;
	for(re int i=0;i<=cnt;++i)
		maxn[i]=k[i]-'0';
	return dfs(0,0,1);
}
int main()
{
	cin>>k;
	cin>>d;
	LL ans=(work()-1+MOD)%MOD;
	printf("%lld\n",ans);
	return 0;
}
//3373

/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
       return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}
void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}

bool Check(ll num, ll pos) {
        return (bool)(num & (1LL << pos));
}
 long long int sumXOR(ll arr[], int n)
{
     long long int sum = 0;
    for (int i = 0; i <=60; i++)
    {
        //  Count of zeros and ones
        long long zc = 0, oc = 0;

        // Individual sum at each bit position
        long long int idsum = 0;
        for (int j = 0; j < n; j++)
        {
           /* if (arr[j] % 2 == 0)
                zc++;
            else
                oc++;
            arr[j] /= 2;*/

            if(Check(arr[j],i))zc++;

            else oc++;
        }

        // calculating individual bit sum
        idsum = (oc * ((zc * ((1ll << i)%Mod))%Mod))%Mod;

        // final sum
        sum =(sum+ idsum)%Mod;
    }
    return sum%Mod;
}
int main()
{

   fastread();

   ll i,j,n,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r,ans=0;

      bool flag=false;

      string str;

        cin>>n;

        long long ara[n+2];

        for(i=0; i<n; i++)
            cin>>ara[i];

         cout<<sumXOR(ara,n)<<endl;

}


#include <iostream>
#include <algorithm>
#include <fstream>
using namespace std;

int N = 0, M = 0;
int points[1001];
int scores[1002001];

bool check(int a, int c){
    if(a >= (N + 2) * (N + 1) / 2 - 1){
        return true;
    }
    return (scores[a] <= c && scores[a + 1] > c);
}

int search2(int a, int b, int c){
    int pib = a + b - b / 2;
    if(check(pib, c)){
        return pib;
    }else if(scores[pib] < c){
        return search2(pib, b / 2, c);
    }else{
        return search2(a, b / 2, c);
    }
}

int main(){
    for(;;){
        for(int i = 0; i < 1001; i++){
            points[i] = 0;
        }
        for(int i = 0; i < 1002001; i++){
            scores[i] = 0;
        }
        if(cin >> N){
            if(N == 0){
                break;
            }
            cin >> M;
            for(int i = 1; i <= N; i++){
                cin >> points[i];
            }
            for(int i = 0; i <= N; i++){
                for(int j = i; j <= N; j++){
                    scores[(2 * N - i + 1) * i / 2 + j] = points[i] + points[j];
                }
            }
            int mxsco = 0;
            sort(scores, scores + ((N + 2) * (N + 1)) / 2);
            for(int i = 0; i < (N + 2) * (N + 1) / 2; i++){
                if(scores[i] > M){
                    break;
                }
                int nosco = scores[i] + scores[search2(0, (N + 2) * (N + 1) / 2, M - scores[i])];
                if(nosco >= mxsco &&  nosco <= M){
                    mxsco = nosco;
                }
            }
            cout << mxsco << endl;
        }else{
            break;
        }
    }
    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(){

	int n,m;
	while(cin>>n>>m&&!(n==0&&m==0)){
		int scores[1001];
		vector<int> comb;
		for(int i = 0; i < n; i++){
			cin>>scores[i];
		}
		scores[n]=0;
		for(int i = 0; i < n+1; i++){
			for(int j = 0; j < n+1; j++){
				int sum=scores[i]+scores[j];
				if(sum<=m)
					comb.push_back(sum);
			}
		}
		sort(comb.begin(),comb.end());
		comb.erase(unique(comb.begin(),comb.end()));

		int maxNum=0;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				int left = (scores[i]+scores[j]);
				if(left<=m&&comb[0]+left<=m){
					vector<int>::iterator it = lower_bound(comb.begin(),comb.end(),m-left);
					if(it==comb.end()){
						maxNum=max(maxNum,comb[comb.size()-1]+left);
					}
					else{
						if(*it==(m-left)){
							maxNum=m;
						}
						else{
							it--;
							maxNum=max(maxNum,*it+left);
						}
					}
				}
			}
		}
		cout<<maxNum<<endl;
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
int n,m;
ll x,sum,ans;
map<ll,int>mmp;
int main()
{
    scanf("%d%d",&n,&m);
    sum=0;
    ans=0;
    for(int i=0;i<n;i++)
    {
        scanf("%lld",&x);
        sum+=x;
        ans+=mmp[sum%m];
        if(sum%m==0) ans++;
        mmp[sum%m]++;
    }
    printf("%lld\n",ans);
    return 0;
}

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

int n,q;
int c[500005],pre[500005],ans[500005];
vector<pair<int,int>> qs[500005];
int bit[2000005];

int sum(int pos){
    int res=0;
    while(pos>0){
        res+=bit[pos];
        pos-=pos&-pos;
    }
    return res;
}

void add(int pos,int val){
    while(pos<=n){
        bit[pos]+=val;
        pos+=pos&-pos;
    }
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin>>n>>q;
    for(int i=1;i<=n;i++)cin>>c[i];
    for(int i=1;i<=q;i++){
        int l,r;
        cin>>l>>r;
        qs[r].emplace_back(l,i);
    }
    for(int i=1;i<=n;i++){
        add(i,1);
        if(pre[c[i]])add(pre[c[i]],-1);
        pre[c[i]]=i;
        for(pair<int,int> &p:qs[i]){
            ans[p.second]=sum(i)-sum(p.first-1);
        }
    }
    for(int i=1;i<=q;i++)cout<<ans[i]<<'\n';

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define erep(i,a,n) for(int i = a;i<=n;i++)
typedef long long ll;
#define int long long
#define vint vector<int>
#define vvint vector<vector<int>>
#define vstring vector<string>
#define vdouble vector<double>
#define vll vector<ll>:
#define vbool vector<bool>
#define INF 1101010101010101010
#define MOD 1000000007
#define P pair<int,int>
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }

const int MAXN = 1010;
const int MAXV = MAXN*(MAXN-1)/2;

vint to[MAXV];
int id[MAXN][MAXN];

bool visited[MAXV];
int dp[MAXV];
bool calculated[MAXV];

int dfs(int a){
  if(visited[a]){
    if(!calculated[a]){
      return -1;
    }
    return dp[a];
  }
  visited[a] = true;
  dp[a] = 1;
  for(int p:to[a]){
    int u = dfs(p);
    if(u == -1){
      return -1;
    }
    dp[a] = max(dp[a],u+1);
  }
  calculated[a] = true;
  return dp[a];
}

int toID(int a,int b){
  if(a < b) swap(a,b);
  return id[a][b];
}

signed main(){
  int n;
  cin >> n;
  vvint A(n,vint(n-1));
  rep(i,0,n){
    rep(j,0,n-1){
      cin >> A[i][j];
      A[i][j]--;
    }
  }
  int v = 0;
  rep(i,0,n){
    rep(j,0,n){
      if(i > j){
        id[i][j] = v;
        v++;
      }
    }
  }
  rep(i,0,n){
    rep(j,0,n-1){
      A[i][j] = toID(i,A[i][j]);
    }
  }
  rep(i,0,n){
    rep(j,0,n-2){
      to[A[i][j+1]].push_back(A[i][j]);
    }
  }
  int ans = 0;
  rep(i,0,v){
    int res = dfs(i);
    if(res == -1){
      cout << -1 << endl;
      return 0;
    }
    ans = max(ans,res);
  }
  cout << ans << endl;
}

#include <bits/stdc++.h>

using namespace std;
using ll =long long;
typedef pair<int,int> P;
#define SORT(a) sort((a).begin(),(a).end())
#define rSORT(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
//Write From this Line

int main()
{
	int n;
	cin >> n;
	vector<vector<int>> a(n, vector<int> (n-1));
	rep(i,n){
		rep(j,n-1){
			cin >> a[i][j];
			a[i][j]--;
		}
		reverse(a[i].begin(), a[i].end());
	}

	vector<P> q;
	auto check = [&](int i){
		if(a[i].size() == 0) return;
		int j = a[i].back();
		if(a[j].size() == 0) return;
		if(a[j].back() == i){
			P p(i,j);
			if(p.second < p.first) swap(p.first, p.second);
			q.push_back(p);
		}
	};
	rep(i,n){
		check(i);
	}
	int day = 0;
	while(q.size() > 0){
		day++;
		sort(q.begin(), q.end());
		q.erase(unique(q.begin(), q.end()), q.end());
		vector<P> prevQ;
		swap(prevQ, q);

		for(P p: prevQ){
			int i = p.first, j = p.second;
			a[i].pop_back();
			a[j].pop_back();
		}
		for(P p: prevQ){
			int i = p.first, j = p.second;
			check(i);
			check(j);
		}
	}
	rep(i,n){
		if(a[i].size() != 0){
			cout << -1 << endl;
			return 0;
		}
	}
	cout << day << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i = j;i<k;i++)
int w,h,xs,ys,xg,yg,n;
int m[102][102];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool DFS(int x,int y,int c){
  if(m[x][y]!=c)return false;
  if(x==xg && y == yg)return true;
  m[x][y]=-1;
  bool ans =false;
  rep(i,0,4){
    ans = max(ans,DFS(x+dx[i],y+dy[i],c));
  }
  return ans;
}
int main(){
  cin >>w>>h;
  while(w!=0){
    cin>>xs>>ys>>xg>>yg>>n;
    xs++;
    ys++;
    xg++;
    yg++;
    rep(i,0,102)rep(j,0,102)m[i][j]=-1;
    rep(i,0,n){
      int c,d,x,y;
      cin >>c>>d>>x>>y;
      x++;
      y++;
      if(d==0){
        rep(j,x,x+4)rep(k,y,y+2)m[j][k]=c;
      }else{
        rep(j,x,x+2)rep(k,y,y+4)m[j][k]=c;
      }
    }
    string ans[2]={"NG","OK"};
    if(m[xs][ys]==-1)cout <<"NG"<<endl;
    else cout << ans[DFS(xs,ys,m[xs][ys])]<<endl;
    cin >> w>>h;
  }
  return 0;
}

#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;

using namespace std;

const double PI = 3.14159265358979323846;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define rep(i, n) for (ll i = 0; i < (n); ++i)
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

double equal(double a, double b) {
    return fabs(a - b) < DBL_EPSILON;
}

std::istream &operator>>(std::istream &in, set<int> &o) {
    int a;
    in >> a;
    o.insert(a);
    return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
    ll a;
    in >> a;
    o.push(a);
    return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);
//const ll mod = 1e10;

typedef priority_queue<ll, vector<ll>, greater<ll> > PQ_ASK;

class MatrixSum {
    vector<vector<ll>> sum;
public:
    MatrixSum(ll x, ll y) {
        sum = vector<vector<ll>>(x, vector<ll>(y));
    }

    void add(ll x, ll y) {
        sum[x][y]++;
    }

    ll get(ll x, ll y) {
        if (x == -1 || y == -1) {
            return 0;
        }
        if (x == sum.size() || y == sum[x].size()) {
            return 0;
        }
        return sum[x][y];
    }

    void setUp() {
        for (ll x = 0; x < sum.size(); x++) {
            for (ll y = 0; y < sum[x].size(); y++) {
                sum[x][y] += get(x - 1, y) + get(x, y - 1) - get(x - 1, y - 1);
            }
        }
    }

    ll getSum(ll xs, ll ys, ll xe, ll ye) {
        return get(xe, ye) - get(xs - 1, ye) - get(xe, ys - 1) + get(xs - 1, ys - 1);
    }

};

int main() {
    int h, w, q;
    cin >> h >> w >> q;
    vector<vector<char>> grid(h, vector<char>(w));
    rep(y, h) rep(x, w) cin >> grid[y][x];

    MatrixSum b(h, w), bv(h, w), bh(h, w);

    rep(y, h) rep(x, w) if (grid[y][x] == '1') b.add(y, x);
    rep(y, h) rep(x, w - 1) if (grid[y][x] == '1' && grid[y][x + 1] == '1') bh.add(y, x);
    rep(y, h - 1) rep(x, w) if (grid[y][x] == '1' && grid[y + 1][x] == '1') bv.add(y, x);

    b.setUp();
    bv.setUp();
    bh.setUp();
    rep(_, q) {
        int y1, x1, y2, x2;
        cin >> y1 >> x1 >> y2 >> x2;
        y1--;
        x1--;
        y2--;
        x2--;

        int bc = b.getSum(y1, x1, y2, x2);
        int bvc = y1 == y2 ? 0 : bv.getSum(y1, x1, y2 - 1, x2);
        int bhc = x1 == x2 ? 0 : bh.getSum(y1, x1, y2, x2 - 1);

        int ans = bc - bvc - bhc;

        cout << ans << endl;
    }

}


#include<stdio.h>
#define N 8
#define FREE -1
#define NOT_FREE 1

int row[N],col[N],dpos[2*N-1],dneg[2*N-1];

int x[N][N];
void initialize(void);
void printBoard(void);
void recursive(int);

int main(){
  int i,j;

  initialize();

  for( i = 0 ; i < N ; i++ ){
    for(j = 0 ; j < N ; j++ ){
      x[i][j] = 0;

    }
  }

  int k;
  int r,c;

  scanf("%d",&k);

  for( i = 0 ; i < k ; i++ ){
    scanf("%d%d",&r,&c);
    x[r][c] = 1;
  }

  recursive(0);

  return 0;
}

void initialize(){
  int i,j;
  for( i = 0 ; i < N ; i++ ) {
    row[i] = FREE;
    col[i] = FREE;
  }

  for( i = 0 ; i < 2*N-1 ; i++ ){
    dpos[i] = FREE;
    dneg[i] = FREE;
  }

}

void printBoard(){
  int i,j;

  for( i = 0 ; i < N ; i++ ){
    for( j = 0 ; j < N ; j++ ){
      if(x[i][j]){
    if(row[i] != j) return;
      }
    }
  }

  for( i = 0 ; i < N ; i++ ){
    for( j = 0 ; j < N ; j++ ){
      if(row[i] == j ) printf("Q");
      else printf(".");
    }
    printf("\n");
  }
}

void recursive(int i){
  if( i == N ){
    printBoard();
    return;
  }

  int j;

  for( j = 0 ; j < N ; j++ ){
    if( NOT_FREE == col[j] || NOT_FREE == dpos[i+j] || NOT_FREE == dneg[i-j+N-1]) continue;

    row[i] = j;
    col[j] = dpos[i+j] = dneg[i-j+N-1] = NOT_FREE;

    recursive(i+1);

    row[i] = col[j] = dpos[i+j] = dneg[i-j+N-1] = FREE;
  }
}

#include <algorithm>
#include <cmath>
#include <cstdio>

#define int long long
#define dotimes(i, n) for (int i : range(n))

using namespace std;

class range {
  class iterator {
    int i, s;
  public:
    iterator(int i, int s): i(i), s(s) {}
    int& operator*() {
      return i;
    }
    void operator++() {
      i += s;
    }
    bool operator!=(iterator const& that) {
      return i < that.i;
    }
  };
  iterator b, e;
public:
  range(int e): b({0, 1}), e({e, 1}) {}
  range(int b, int e): b({b, 1}), e({e, 1}) {}
  range(int b, int e, int s): b({b, s}), e({e, s}) {}
  iterator begin() {
    return b;
  }
  iterator end() {
    return e;
  }
};

int rint() {
  int n;
  scanf("%lld", &n);
  return n;
}

void wint(int n) {
  printf("%lld\n", n);
}

template<typename T>
inline int size(T container) {
  return static_cast<int>(container.size());
}

template<typename T>
inline bool maxs(T& a, T const& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template<typename T>
inline bool mins(T& a, T const& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  const int k = rint();
  int rf[8] = {}, p[8 - k];
  bool cf[8] = {};
  dotimes(i, k) {
    int r = rint();
    int c = rint();
    rf[r] = c + 1;
    cf[c] = true;
  }
  for (int i = 0, j = 0, c = 0; i < 8; i++)
    if (!rf[i]) {
      while (cf[c])
        c++;
      p[j++] = ++c;
    }
  int a[8];
  for (;; next_permutation(p, p + 8 - k)) {
    for (int i = 0, j = 0; i < 8; i++)
      a[i] = rf[i] ? rf[i] : p[j++];
    dotimes(i, 8)
      dotimes(j, i)
        if (abs(a[j] - a[i]) == abs(j - i))
          goto failed;
    break;
  failed:
    ;
  }
  dotimes(i, 8) {
    dotimes(j, 8)
      putchar(a[i] == j + 1 ? 'Q' : '.');
    putchar('\n');
  }
  return 0;
}


#include<stdio.h>
#include<string.h>
#define N 8
int board[N][N];
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_13_A -- backtracking
//chech previous part but there are fixed point before that
//so check all of them
bool safe(int row, int col){
	//chec the column
	if(board[row][col]==1) return true;
	for(int i = 0; i<N; i++){
		if(board[row][i] == 1) return false;
	}
	//check the row
	for(int i = 0; i<N; i++){
		if(board[i][col] == 1) return false;
	}
	//check the diagional
	int i = row, j = col;
	while(i>=0 && j>=0){
		if(board[i][j]==1) return false;
		i--; j--;
	}
	i = row, j = col;
	while(i<N && j<N){
		if(board[i][j]==1) return false;
		i++; j++;
	}
	i = row, j = col;
	while(i>=0 && j<N){
		if(board[i][j]==1) return false;
		i--; j++;
	}
	i = row, j = col;
	while(i<N && j>=0){
		if(board[i][j]==1) return false;
		i++; j--;
	}
	//printf("check\n");
	return true;
}

//for each row([0,7]),check the column
bool solve_board(int col){
	//all chess placed
	if(col>=N) return true;
	for(int i = 0; i<N; i++){
		if(board[i][col]==1){
			if(solve_board(col+1)) return true;
		}
		else {
		if(safe(i,col)){
			//printf("%d %d\n",i,col);
			board[i][col] = 1;
			//recur--key points
			if(solve_board(col+1)) return true;
			board[i][col] = 0;
		}}

	}
	return false;
}

int main(){
	int n = 0;
	memset(board,0,sizeof(board));
	scanf("%d",&n);
	for(int i = 0; i<n; i++){
		int a, b;
		scanf("%d %d",&a,&b);
		board[a][b] = 1;
	}
	//for each column, check the row
	solve_board(0);
	for(int i = 0; i<N; i++){
		for(int j = 0; j< N; j++){
			if(board[i][j]==0) printf(".");
			else printf("Q");
		}
		printf("\n");
	}
}

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

#define int long long

int V, E, Q, a, b;
vector<int> G[100001];
vector<int> rG[100001];
vector<int> vs;
bool used[100001];
int cmp[100001];

auto dfs(int v) -> void {
    used[v] = true;
    for (int i = 0; i < G[v].size(); ++i)
        if (!used[G[v][i]])
            dfs(G[v][i]);

    vs.push_back(v);
}

auto rdfs(int v, int k) -> void{
    used[v] = true;
    cmp[v] = k;
    for (int i = 0; i < rG[v].size(); i++)
        if (!used[rG[v][i]])
            rdfs(rG[v][i], k);
}

/**
 * ?????£?????????????§£
 * @return ?????£???????????????????????????????????????????????§???
 */
auto scc() -> int {
    fill(used, used + V, false);
    vs.clear();
    for (int v = 0; v < V; v++) {
        if (!used[v]) dfs(v);
    }
    fill(used, used + V, false);
    int k = 0;
    for (int i = vs.size() - 1; i >= 0; i--)
        if (!used[vs[i]])
            rdfs(vs[i], k++);

    return k;
}

auto main() -> signed {
    cin >> V >> E;
    for (int i = 0; i < E; i++) {
        cin >> a >> b;
        G[a].push_back(b);
        rG[b].push_back(a);
    }
    int k = scc();

    cin >> Q;
    for (int i = 0; i < Q; i++) {
        cin >> a >> b;
        cout << ((cmp[a] == cmp[b]) ? 1 : 0) << endl;
    }
    return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <string>
#include <cstring>
#include <numeric>
#include <iomanip>
using namespace std;
//
const int inf=1e9+7;//0x3f
//
struct UnionFind{
	//
	vector<int> par,ran;
	//
	UnionFind(int n){
		init(n);
	}
	//
	void init(int n){
		par.resize(n);
		ran.resize(n,0);
		for(int i=0;i<n;i++){
			par[i]=i;
		}
	}
	//
	int find(int x){
		return (x==par[x]?x:par[x]=find(par[x]));
	}
	//
	bool same(int x,int y){
		return find(x)==find(y);
	}
	//
	void unite(int x,int y){
		x=find(x);
		y=find(y);
		if(x==y){
			return;
		}else{
			if(ran[x]<ran[y]){
				par[x]=y;
			}else{
				par[y]=x;
				if(ran[x]==ran[y]){
					ran[x]++;
				}
			}
		}
	}
	//
};
//
int main(){
	int N,K,L;
	cin>>N>>K>>L;
	UnionFind road(N),train(N);
	for(int i=0;i<K;i++){
		int p,q;
		cin>>p>>q;
		p--;q--;
		road.unite(p,q);
	}
	for(int i=0;i<L;i++){
		int r,s;
		cin>>r>>s;
		r--;s--;
		train.unite(r,s);
	}
	int r[200000],t[200000];
	for(int i=0;i<N;i++){
		r[i]=road.find(i);
		t[i]=train.find(i);
	}
	map<pair<int,int>,int> M;
	for(int i=0;i<N;i++){
		M[make_pair(r[i],t[i])]++;
	}
	for(int i=0;i<N;i++){
		if(i){
			cout<<" ";
		}
		cout<<M[make_pair(r[i],t[i])];
	}
	cout<<endl;
	return 0;
}

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

#define fr(i,n) _back
#define pb	push_back
#define eb	emplace_back
#define mk	make_pair
#define fi	first
#define se	second
#define endl	'\n'

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int T = 2e5 + 3;

int pai[3][T];
int peso[3][T];
vii g[T];
int n,k,l;

void init() {
    for(int i = 0; i < 3; i++)
        for(int j = 0; j <= n; j++) {
            pai[i][j] = j;
            peso[i][j] = 1;
        }
}

int find(int op, int x) {
    return (pai[op][x] == x? x : pai[op][x] = find(op,pai[op][x]));
}

void join(int op, int x, int y) {
    x = find(op,x);
    y = find(op,y);
    if(x == y) return;
    if(peso[op][x] < peso[op][y]) swap(x,y);
    pai[op][y] = x;
    peso[op][x] += peso[op][y];
}

int main() {
    ios_base::sync_with_stdio(false);
    cin >> n >> k >> l;
    init();

    while(k--) {
        int a,b; cin >> a >> b;
        if(find(0,a) != find(0,b)) join(0,a,b);
    }

    while(l--) {
        int a,b; cin >> a >> b;
        if(find(1,a) != find(1,b)) join(1,a,b);
    }

    for(int i = 1; i <= n; i++)
        g[find(0,i)].eb(find(1,i),i);

    for(int i = 1; i <= n; i++) {
        sort(g[i].begin(), g[i].end());
        for(int j = 1; j < g[i].size(); j++)
            if(g[i][j].fi == g[i][j-1].fi)
                join(2,g[i][j].se,g[i][j-1].se);
    }

    for(int i = 1; i <= n; i++) cout << peso[2][find(2,i)] << " ";
    cout << endl;

    return 0;
}


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

const ll INF = 0x3f3f3f3f3f3f3f3f;
const int MAX_N = 1e5+20;
int n;
int a[MAX_N],b[MAX_N],c[MAX_N];
int Q, K;
int x[MAX_N], y[MAX_N];

struct edge { int to,  cost; };
vector<edge> G[MAX_N];
ll dist[MAX_N];

void add_edge(int from, int to, int cost)
{
	G[from].push_back(edge{to, cost});
	G[to].push_back(edge{from, cost});
}

void dfs(int x, ll d)
{
	dist[x] = d;
	for(auto e : G[x]){
		int t = e.to, c = e.cost;
		if(dist[t] == INF){
			dfs(t, d+c);
		}
	}
}

void solve()
{
	for(int i=0;i<n-1;++i) add_edge(a[i],b[i],c[i]);
	memset(dist,0x3f,sizeof(dist));
	dfs(K, 0);
	for(int i=0;i<Q;++i){
		cout<<dist[x[i]]+dist[y[i]]<<'\n';
	}
}

int main()
{
	cin >> n;
	for(int i=0;i<n-1;++i){
		cin >> a[i] >> b[i] >> c[i];
	}
	cin >> Q >> K;
	for(int i=0;i<Q;++i){
		cin >> x[i] >> y[i];
	}
	solve();
	return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MAX 100010
#define INFTY 1e18
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
struct edge{ll to, cost;};

ll n;
ll dist[MAX];
bool check[MAX];
vector<edge> G[MAX];
void dijkstra(ll s)
{
	priority_queue<P, vector<P>, greater<P>> pq;

	for(ll i = 0; i < n; i++)
	{
		dist[i] = INFTY;
		check[i] = false;
	}

	pq.push(P(0, s));
	dist[s] = 0;
	while (!pq.empty())
	{
		P p = pq.top(); pq.pop();
		ll minv = p.first;
		ll u = p.second;
		check[u] = true;
		if (dist[u] < minv) continue;
		rep(i, G[u].size())
		{
			if (check[G[u][i].to]) continue;
			if (dist[G[u][i].to] > dist[u] + G[u][i].cost)
			{
				dist[G[u][i].to] = dist[u] + G[u][i].cost;
				pq.push(P(dist[G[u][i].to], G[u][i].to));
			}
		}
	}
}

signed main(void)
{
	cin >> n;
	rep(i, n - 1)
	{
		int a, b;
		ll c;
		cin >> a >> b >> c;
		a--; b--;
		G[a].push_back({b, c});
		G[b].push_back({a, c});
	}
	int q, k;
	cin >> q >> k;
	k--;
	dijkstra(k);
	rep(i, q)
	{
		int x, y;
		cin >> x >> y;
		x--; y--;
		cout << dist[x] + dist[y] << endl;
	}
	return 0;
}