import core.bitop, std.algorithm, std.conv, std.stdio, std.string;
alias f = n => (n *= 2) - n.popcnt;
void main() {foreach (t; 0..readln.strip.to!int) readln.strip.to!long.f.writeln;}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(T)(ref T x, T y) { x = (x + y) % mod; }
void mods(T)(ref T x, T y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(T)(ref T x, T y) { x = (x * y) % mod; }
void modpow(T)(ref T x, T y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(T)(ref T x, T y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD;

		auto cnt = new long[](64);
		cnt[0] = 1;
		foreach (i; 1..64)
		{
			cnt[i] = cnt[i-1] * 2 + 1;
		}

		foreach (i; 0..64)
		{
			auto bit = 1L << i;
			if (n & bit)
			{
				ans[ti] += cnt[i];
			}
		}
	}

	foreach (e; ans)
	{
		writeln(e);
	}
	stdout.flush;
	debug readln;
}

import core.bitop,std.conv,std.stdio,std.string;
alias f=n=>(n*=2)-n.popcnt;
void main(){foreach(t;0..readln.strip.to!int)readln.strip.to!long.f.writeln;}

import std.stdio;
import std.stdio;
import std.algorithm;
import std.range;
import std.functional;
import std.conv;
import std.string;
import std.math;
import core.bitop;

ulong diff(ulong a, ulong b) {
	ulong res = 0;
	foreach (bit; 0 .. 64) {
		if ((a & (1UL << bit)) != (b & (1UL << bit))) {
			res++;
		}
	}
	return res;
}

ulong calc(ulong a) {
	if ((a & (a - 1)) == 0) {
		return a | (a - 1);
	}
	else {
		ulong highestBit = 1UL << bsr(a);
		return calc(a - highestBit) + calc(highestBit);
	}
}

void main() {
	// ulong res = 0;
	// foreach (i; 0 .. 1000) {
	// 	res += diff(i, i + 1);
	// 	write("f(", (i + 1).to!string.padLeft(' ', 10), ") = ", res.to!string(2).padLeft('0', 10));
	// 	writeln("; ", calc(i + 1).to!string(2).padLeft('0', 10));
	// }
	int tt = readln().chomp.to!int;
	outer: foreach (t; 0 .. tt) {
		ulong n = readln().chomp.to!ulong;
		// writeln(n.to!string(2).padLeft('0', 64));
		writeln(calc(n));
	}
}


import std.algorithm;
import std.container;
import std.conv;
import std.format;
import std.range;
import std.stdio;
import std.string;

void main() {
    string s = readln.strip;
    int n = to!int(s.length);

    int[26] min_idx = n;

    for (int i = n-1; i >= 0; i--) {
        min_idx[s[i]-'a'] = i;
    }

    debug {
        writefln!"%s"(min_idx);
    }

    foreach (i; 0 .. n) {
        int j = s[i] - 'a';
        if (min_idx[0 .. j].any!( (a) => a < i )) {
            write("Ann\n");
        } else {
            write("Mike\n");
        }
    }
}

import std.algorithm;
import std.container;
import std.conv;
import std.format;
import std.range;
import std.stdio;
import std.string;

void main() {
    string s = readln.strip;
    int n = to!int(s.length);

    int[26] min_idx = n;

    for (int i = n-1; i >= 0; i--) {
        min_idx[s[i]-'a'] = i;
    }

    debug {
        writefln!"%s"(min_idx);
    }

    foreach (i; 0 .. n) {
        int j = s[i] - 'a';
        if (min_idx[0 .. j].find!( (a,b) => a < b )(i).empty) {
            writeln("Mike");
        } else {
            writeln("Ann");
        }
    }
}

import std.algorithm;
import std.container;
import std.conv;
import std.format;
import std.range;
import std.stdio;
import std.string;

void main() {
    string s = readln.strip;
    int n = to!int(s.length);

    int[26] min_idx = n;

    for (int i = n-1; i >= 0; i--) {
        min_idx[s[i]-'a'] = i;
    }

    debug {
        writefln!"%s"(min_idx);
    }

    foreach (i; 0 .. n) {
        int j = s[i] - 'a';
        if (min_idx[0 .. j].any!( (a) => a < i )) {
            writeln("Ann");
        } else {
            writeln("Mike");
        }
    }
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void main() {
  try {
    for (; ; ) {
      const S = readToken();

      char mn = 'z' + 1;
      foreach (c; S) {
        writeln((mn < c) ? "Ann" : "Mike");
        chmin(mn, c);
      }
    }
  } catch (EOFException e) {
  }
}

import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static string[] s_rd;
T RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
string RDR()() { return readln.chomp; }
T[] ARR(T = long)(in string str, T fix = 0) { auto r = str.split.to!(T[]); r[] += fix; return r; }
T[] RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[][] RDA2(T = long)(size_t n, T[] fix = []) { auto r = new T[][](n); foreach (i; 0..n) { r[i] = readln.chomp.split.to!(T[]); foreach (j, e; fix) r[i][j] += e; } return r; }
size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
long lcm(long x, long y) { return x * y / gcd(x, y); }

//long mod = 10^^9 + 7;
long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }

void main()
{
	auto s = RD!string;
	char best = s[0];
	writeln("Mike");
	foreach (c; s[1..$])
	{
		if (c <= best)
		{
			writeln("Mike");
		}
		else
		{
			writeln("Ann");
		}
		best = min(best, c);
	}

	stdout.flush();
	debug readln();
}


import core.bitop;
import std.algorithm;
import std.array;
import std.bitmanip;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import std.range.interfaces;

void main()
{
    readln;

    auto r = () => readln.chomp.split.map!(to!int).array;
    auto c = r();
    auto a = r();

    debug { c.writeln; a.writeln; }

    auto ans = 0;
    int aidx = 0;
    foreach (e; c) {
        if (e > a[aidx]) continue;

        ans += 1;
        ++aidx;

        if (aidx == a.length) break;
    }

    ans.writeln;
}

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto M = s[1];
    auto C = readln.split.map!(to!int).array;
    auto A = readln.split.map!(to!int).array;
    int ans = 0;

    for (int i = 0, p = 0; i < N && p < M; ++i) {
        if (C[i] <= A[p]) {
            ans += 1;
            p += 1;
        }
    }

    ans.writeln;
}

void main(){
  import std.stdio, std.string, std.conv, std.algorithm;

  int n, m; rd(n, m);
  auto c=readln.split.to!(int[]);
  auto a=readln.split.to!(int[]);

  int cnt=0;
  for(int i=0, j=0; i<n && j<m; ){
    if(c[i]<=a[j]){
      cnt++;
      i++; j++;
    }else{
      i++;
    }
  }

  writeln(cnt);
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x) e=l[i].to!(typeof(e));
}


import std.algorithm;
import std.array;
import std.range;
import std.stdio;
import std.string;

immutable int letters = 26;
immutable int mod = 10 ^^ 9 + 7;

void main () {
	int n;
	while (readf (" %s", &n) > 0) {
		readln;
		auto s = readln.strip.map !(x => x - 'a').array;

		auto next = new int [] [] (n, letters);
		foreach (let; 0..letters) {
			int pos = n;
			foreach_reverse (i; 0..n) {
				if (s[i] == let) pos = i;
				next[i][let] = pos;
			}
		}

		auto f = new long [] [] (2, n + 1);
		int b = 0;
		f[b][] = 0;
		f[b][0] = 1;
		foreach (num; 0..n)
		{
			b ^= 1;
			f[b][] = 0;
			foreach (pos; 0..n) {
				auto cur = f[!b][pos];
				auto ptr = next[pos].ptr;
				foreach (let; 0..letters) f[b][*(ptr + let)] += cur;
			}
			foreach (pos; 0..n) f[b][pos] %= mod;
		}

		int res = 0;
		foreach (pos; 0..n) res = (res + f[b][pos]) % mod;
		writeln (res);
	}
}

import core.bitop;
import std.algorithm;
import std.array;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

immutable int letters = 26;
immutable int mod = 10 ^^ 9 + 7;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		readln;
		auto s = readln.strip.map !(x => x - 'a').array;

		auto next = new int [] [] (n, letters);
		foreach (let; 0..letters)
		{
			int pos = n;
			foreach_reverse (i; 0..n)
			{
				if (s[i] == let)
				{
					pos = i;
				}
				next[i][let] = pos;
			}
		}

		auto f = new long [] [] (2, n + 1);
		int b = 0;
		f[b][] = 0;
		f[b][0] = 1;
		foreach (num; 0..n)
		{
			b ^= 1;
			f[b][] = 0;
			foreach (pos; 0..n)
			{
				auto cur = f[!b][pos];
				auto ptr = next[pos].ptr;
				f[b][*(ptr +  0)] += cur;
				f[b][*(ptr +  1)] += cur;
				f[b][*(ptr +  2)] += cur;
				f[b][*(ptr +  3)] += cur;
				f[b][*(ptr +  4)] += cur;
				f[b][*(ptr +  5)] += cur;
				f[b][*(ptr +  6)] += cur;
				f[b][*(ptr +  7)] += cur;
				f[b][*(ptr +  8)] += cur;
				f[b][*(ptr +  9)] += cur;
				f[b][*(ptr + 10)] += cur;
				f[b][*(ptr + 11)] += cur;
				f[b][*(ptr + 12)] += cur;
				f[b][*(ptr + 13)] += cur;
				f[b][*(ptr + 14)] += cur;
				f[b][*(ptr + 15)] += cur;
				f[b][*(ptr + 16)] += cur;
				f[b][*(ptr + 17)] += cur;
				f[b][*(ptr + 18)] += cur;
				f[b][*(ptr + 19)] += cur;
				f[b][*(ptr + 20)] += cur;
				f[b][*(ptr + 21)] += cur;
				f[b][*(ptr + 22)] += cur;
				f[b][*(ptr + 23)] += cur;
				f[b][*(ptr + 24)] += cur;
				f[b][*(ptr + 25)] += cur;
			}
			foreach (pos; 0..n)
			{
				f[b][pos] %= mod;
			}
		}

		int res = 0;
		foreach (pos; 0..n)
		{
			res += f[b][pos];
			if (res >= mod)
			{
				res -= mod;
			}
		}
		writeln (res);
	}
}


import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;

//	Input
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }
int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }

//	chmin/chmax
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }

int N;
long[] A;

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		A = new long[N];
		foreach (i; 0 .. N) {
			A[i] = readLong;
		}

		long[] ASum = new long[N + 1];
		foreach (i; 0 .. N) {
			ASum[i + 1] = ASum[i] + A[i];
		}

		long ans;

		if (ASum[N] % 3 == 0) {
			const target = ASum[N] / 3;
			long cnt;
			foreach (i; 1 .. N) {
				if (ASum[i] == target * 2) {
					ans += cnt;
				}
				if (ASum[i] == target) {
					++cnt;
				}
			}
		}

		writeln(ans);

	}
	} catch (EOFException) {}
}

import std.stdio, std.string;

void solve(int[] s)
{
    auto sum = 0;
    foreach (val; s)
    {
        sum += val;
    }
    if (sum % 3 != 0)
    {
        writeln("0");
        return;
    }
    sum /= 3;
    auto n = cast(int)s.length;
    auto cnt = new int[n];
    auto last = 0;
    auto acc = 0;
    for (int i = n - 1; i >= 0; -- i)
    {
        cnt[i] = last;
        acc += s[i];
        if (acc == sum)
        {
            ++ cnt[i];
        }
        last = cnt[i];
    }
    long ans = 0;
    last = 0;
    acc = 0;
    foreach (i; 0 .. n)
    {
        acc += s[i];
        if (acc == sum && i + 2 < n)
        {
            ans += cnt[i + 2];
        }
    }
    writeln(ans);
}

int main(string[] args)
{
    int n;
    while (readf("%d\n", &n) == 1)
    {
        auto s = new int[n];
        foreach (i; 0 .. n)
        {
            readf(" %d", &s[i]);
        }
        readln();
        solve(s);
    }
    return 0;
}


module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,std.parallelism,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,
	core.stdc.stdio : freopen;
alias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");alias long lint;const int mod=1000000007;
alias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;
alias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,"fi",Y,"se")(x_,y_);}
	big gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	int sz(T)(T a) if(hasLength!T){return cast(int)a.length;}
	size_t lowb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	return s==ptrs.length;
}
bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	readln;
	return s==ptrs.length;
}
nothrow auto inf(in char* name) {return freopen(name,"r",core.stdc.stdio.stdin);}
nothrow auto ouf(in char* name) {return freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------

void main()
{
	version(ONLINE_JUDGE){}
	else
	{
		assert(freopen("input.txt","r",core.stdc.stdio.stdin));
		assert(freopen("output.txt","w",core.stdc.stdio.stdout));
	}
	int n,q,m;
loop:while(read(n,m,q))
	{
		auto g=new int[][n+2],r=new long[][n+2],ss=new long[][n+2],comps=new int[][n+2];
		auto c=new int[n+2],d=new int[n+2];
		int pos=1;
		foreach(i;0..m)
		{
			int u,v;
			read(u,v);
			g[u]~=v;
			g[v]~=u;
		}
		void dfs0(int v,int pr=-1)
		{
			comps[pos]~=v;
			c[v]=pos;
			foreach(x;g[v])if(x!=pr)dfs0(x,v);
		}
		void dfs(int v,int pr=-1,int h=0)
		{
			d[v]=max(d[v],h);
			foreach(x;g[v])if(x!=pr)dfs(x,v,h+1);
		}
		foreach(i;1..n+1)
		{
			if(c[i]==0)
			{
				dfs0(i);
				dfs(i);
				int ans=0,v=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
					d[x]=0;
				}
				dfs(v);
				ans=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
				}
				dfs(v);
				foreach(x;comps[pos])
				{
					r[pos]~=d[x];
				}
				sort(r[pos]);
				ss[pos]=r[pos].dup;
				foreach_reverse(j;0..sz(ss[pos])-1)
				{
					ss[pos][j]+=ss[pos][j+1];
				}
				pos++;
			}
		}
		real cel(int x,int y)
		{
			long ans;
			long diam=max(r[x].back,r[y].back);
			foreach(v;parallel(r[x]))
			{
				auto p=r[y].upb(diam-v-1);
				ans+=p*diam;
				if(p<ss[y].length)ans+=ss[y][p]+(ss[y].length-p)*(v+1);
			}
			return ans*1.00000L/(1L*r[x].length*r[y].length);
		}
		alias getans=memoize!cel;
		foreach(ii;0..q)
		{
			int u,v;
			read(u,v);
			int p1=c[u],p2=c[v];
			if(r[p1].length>r[p2].length)swap(p2,p1);
			if(p1==p2)write("-1\n");
			else writef("%.10f\n",getans(p1,p2));
		}
	}
}

module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,std.parallelism,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,
	core.stdc.stdio : freopen;
alias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");alias long lint;const int mod=1000000007;
alias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;
alias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,"fi",Y,"se")(x_,y_);}
	big gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	int sz(T)(T a) if(hasLength!T){return cast(int)a.length;}
	size_t lowb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	return s==ptrs.length;
}
bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	readln;
	return s==ptrs.length;
}
nothrow auto inf(in char* name) {return freopen(name,"r",core.stdc.stdio.stdin);}
nothrow auto ouf(in char* name) {return freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------

void main()
{
	version(ONLINE_JUDGE){}
	else
	{
		assert(freopen("input.txt","r",core.stdc.stdio.stdin));
		assert(freopen("output.txt","w",core.stdc.stdio.stdout));
	}
	int n,q,m;
loop:while(read(n,m,q))
	{
		auto g=new int[][n+2],r=new long[][n+2],ss=new long[][n+2],comps=new int[][n+2];
		auto c=new int[n+2],d=new int[n+2];
		int pos=1;
		foreach(i;0..m)
		{
			int u,v;
			read(u,v);
			g[u]~=v;
			g[v]~=u;
		}
		void dfs0(int v,int pr=-1)
		{
			comps[pos]~=v;
			c[v]=pos;
			foreach(x;g[v])if(x!=pr)dfs0(x,v);
		}
		void dfs(int v,int pr=-1,int h=0)
		{
			d[v]=max(d[v],h);
			foreach(x;g[v])if(x!=pr)dfs(x,v,h+1);
		}
		foreach(i;1..n+1)
		{
			if(c[i]==0)
			{
				dfs0(i);
				dfs(i);
				int ans=0,v=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
					d[x]=0;
				}
				dfs(v);
				ans=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
				}
				dfs(v);
				foreach(x;comps[pos])
				{
					r[pos]~=d[x];
				}
				sort(r[pos]);
				ss[pos]=r[pos].dup;
				foreach_reverse(j;0..sz(ss[pos])-1)
				{
					ss[pos][j]+=ss[pos][j+1];
				}
				pos++;
			}
		}
		real cel(int x,int y)
		{
			long ans;
			long diam=max(r[x].back,r[y].back);
			foreach(v;r[x])
			{
				auto p=assumeSorted(r[y]).lowerBound(diam-v-1).length;
				ans+=p*diam;
				if(p<ss[y].length)ans+=ss[y][p]+(ss[y].length-p)*(v+1);
			}
			return ans*1.00000L/(1L*r[x].length*r[y].length);
		}
		alias getans=memoize!cel;
		foreach(ii;0..q)
		{
			int u,v;
			read(u,v);
			int p1=c[u],p2=c[v];
			if(r[p1].length>r[p2].length)swap(p2,p1);
			if(p1==p2)write("-1\n");
			else writef("%.10f\n",getans(p1,p2));
		}
	}
}

module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,std.parallelism,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,
	core.stdc.stdio : freopen;
alias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");alias long lint;const int mod=1000000007;
alias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;
alias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,"fi",Y,"se")(x_,y_);}
	big gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	int sz(T)(T a) if(hasLength!T){return cast(int)a.length;}
	size_t lowb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	return s==ptrs.length;
}
bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	readln;
	return s==ptrs.length;
}
nothrow auto inf(in char* name) {return freopen(name,"r",core.stdc.stdio.stdin);}
nothrow auto ouf(in char* name) {return freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------

void main()
{
	version(ONLINE_JUDGE){}
	else
	{
		assert(freopen("input.txt","r",core.stdc.stdio.stdin));
		assert(freopen("output.txt","w",core.stdc.stdio.stdout));
	}
	int n,q,m;
loop:while(read(n,m,q))
	{
		auto g=new int[][n+2],r=new long[][n+2],ss=new long[][n+2],comps=new int[][n+2];
		auto c=new int[n+2],d=new int[n+2];
		int pos=1;
		foreach(i;0..m)
		{
			int u,v;
			read(u,v);
			g[u]~=v;
			g[v]~=u;
		}
		void dfs0(int v,int pr=-1)
		{
			comps[pos]~=v;
			c[v]=pos;
			foreach(x;g[v])if(x!=pr)dfs0(x,v);
		}
		void dfs(int v,int pr=-1,int h=0)
		{
			d[v]=max(d[v],h);
			foreach(x;g[v])if(x!=pr)dfs(x,v,h+1);
		}
		foreach(i;1..n+1)
		{
			if(c[i]==0)
			{
				dfs0(i);
				dfs(i);
				int ans=0,v=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
					d[x]=0;
				}
				dfs(v);
				ans=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
				}
				dfs(v);
				foreach(x;comps[pos])
				{
					r[pos]~=d[x];
				}
				sort(r[pos]);
				ss[pos]=r[pos].dup;
				foreach_reverse(j;0..sz(ss[pos])-1)
				{
					ss[pos][j]+=ss[pos][j+1];
				}
				pos++;
			}
		}
		real cel(int x,int y)
		{
			long ans;
			long diam=max(r[x].back,r[y].back);
			foreach(v;r[x])
			{
				auto p=r[y].upb(diam-v-1);
				ans+=p*diam;
				if(p<ss[y].length)ans+=ss[y][p]+(ss[y].length-p)*(v+1);
			}
			return ans*1.00000L/(1L*r[x].length*r[y].length);
		}
		alias getans=memoize!cel;
		foreach(ii;0..q)
		{
			int u,v;
			read(u,v);
			int p1=c[u],p2=c[v];
			if(r[p1].length>r[p2].length)swap(p2,p1);
			if(p1==p2)write("-1\n");
			else writef("%.10f\n",getans(p1,p2));
		}
	}
}

module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,std.parallelism,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,
	core.stdc.stdio : freopen;
alias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");alias long lint;const int mod=1000000007;
alias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;
alias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,"fi",Y,"se")(x_,y_);}
	big gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	int sz(T)(T a) if(hasLength!T){return cast(int)a.length;}
	size_t lowb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	return s==ptrs.length;
}
bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	readln;
	return s==ptrs.length;
}
nothrow auto inf(in char* name) {return freopen(name,"r",core.stdc.stdio.stdin);}
nothrow auto ouf(in char* name) {return freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------

void main()
{
	version(ONLINE_JUDGE){}
	else
	{
		assert(freopen("input.txt","r",core.stdc.stdio.stdin));
		assert(freopen("output.txt","w",core.stdc.stdio.stdout));
	}
	int n,q,m;
loop:while(read(n,m,q))
	{
		auto g=new int[][n+2],r=new long[][n+2],ss=new long[][n+2],comps=new int[][n+2];
		auto c=new int[n+2],d=new int[n+2];
		int pos=1;
		foreach(i;0..m)
		{
			int u,v;
			read(u,v);
			g[u]~=v;
			g[v]~=u;
		}
		void dfs0(int v,int pr=-1)
		{
			comps[pos]~=v;
			c[v]=pos;
			foreach(x;g[v])if(x!=pr)dfs0(x,v);
		}
		void dfs(int v,int pr=-1,int h=0)
		{
			d[v]=max(d[v],h);
			foreach(x;g[v])if(x!=pr)dfs(x,v,h+1);
		}
		foreach(i;1..n+1)
		{
			if(c[i]==0)
			{
				dfs0(i);
				dfs(i);
				int ans=0,v=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
					d[x]=0;
				}
				dfs(v);
				ans=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
				}
				dfs(v);
				foreach(x;comps[pos])
				{
					r[pos]~=d[x];
				}
				sort(r[pos]);
				ss[pos]=r[pos].dup;
				foreach_reverse(j;0..sz(ss[pos])-1)
				{
					ss[pos][j]+=ss[pos][j+1];
				}
				pos++;
			}
		}
		real cel(int x,int y)
		{
			long ans;
			long diam=max(r[x].back,r[y].back);
			auto a=assumeSorted(r[y]);
			foreach(v;r[x])
			{
				auto p=a.lowerBound(diam-v-1).length;
				ans+=p*diam;
				if(p<ss[y].length)ans+=ss[y][p]+(ss[y].length-p)*(v+1);
			}
			return ans*1.00000L/(1L*r[x].length*r[y].length);
		}
		alias getans=memoize!cel;
		foreach(ii;0..q)
		{
			int u,v;
			read(u,v);
			int p1=c[u],p2=c[v];
			if(r[p1].length>r[p2].length)swap(p2,p1);
			if(p1==p2)write("-1\n");
			else writef("%.10f\n",getans(p1,p2));
		}
	}
}

module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,std.parallelism,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,
	core.stdc.stdio : freopen;
alias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");alias long lint;const int mod=1000000007;
alias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;
alias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,"fi",Y,"se")(x_,y_);}
	big gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	int sz(T)(T a) if(hasLength!T){return cast(int)a.length;}
	size_t lowb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	return s==ptrs.length;
}
bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	readln;
	return s==ptrs.length;
}
nothrow auto inf(in char* name) {return freopen(name,"r",core.stdc.stdio.stdin);}
nothrow auto ouf(in char* name) {return freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------

void main()
{
	version(ONLINE_JUDGE){}
	else
	{
		assert(freopen("input.txt","r",core.stdc.stdio.stdin));
		assert(freopen("output.txt","w",core.stdc.stdio.stdout));
	}
	int n,q,m;
loop:while(read(n,m,q))
	{
		auto g=new int[][n+2],r=new long[][n+2],ss=new long[][n+2],comps=new int[][n+2];
		auto c=new int[n+2],d=new int[n+2];
		int pos=1;
		foreach(i;0..m)
		{
			int u,v;
			read(u,v);
			g[u]~=v;
			g[v]~=u;
		}
		void dfs0(int v,int pr=-1)
		{
			comps[pos]~=v;
			c[v]=pos;
			foreach(x;g[v])if(x!=pr)dfs0(x,v);
		}
		void dfs(int v,int pr=-1,int h=0)
		{
			d[v]=max(d[v],h);
			foreach(x;g[v])if(x!=pr)dfs(x,v,h+1);
		}
		foreach(i;1..n+1)
		{
			if(c[i]==0)
			{
				dfs0(i);
				dfs(i);
				int ans=0,v=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
					d[x]=0;
				}
				dfs(v);
				ans=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
				}
				dfs(v);
				foreach(x;comps[pos])
				{
					r[pos]~=d[x];
				}
				sort(r[pos]);
				ss[pos]=r[pos];
				foreach_reverse(j;0..ss[pos].length-1)
				{
					ss[pos][j]+=ss[pos][j+1];
				}
				pos++;
			}
		}
		real cel(int x,int y)
		{
			long ans;
			long diam=max(r[x].back,r[y].back);
			foreach(v;r[x])
			{
				auto p=upb(r[y],diam-v-1);
				ans+=p*diam;
				if(p<r[y].length)ans+=ss[y][p]+(ss[y].length-p)*(v+1);
			}
			return ans*1.00000L/(1L*r[x].length*r[y].length);
		}
		alias getans=memoize!cel;
		foreach(ii;0..q)
		{
			int u,v;
			read(u,v);
			int p1=c[u],p2=c[v];
			if(r[p1].length>r[p2].length)swap(p2,p1);
			if(p1==p2)write("-1\n");
			else writef("%.10f\n",getans(p1,p2));
		}
	}
}

module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,std.parallelism,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,
	core.stdc.stdio : freopen;
alias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");alias long lint;const int mod=1000000007;
alias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;
alias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,"fi",Y,"se")(x_,y_);}
	big gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	int sz(T)(T a) if(hasLength!T){return cast(int)a.length;}
	size_t lowb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	return s==ptrs.length;
}
bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	readln;
	return s==ptrs.length;
}
nothrow auto inf(in char* name) {return freopen(name,"r",core.stdc.stdio.stdin);}
nothrow auto ouf(in char* name) {return freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------

void main()
{
	version(ONLINE_JUDGE){}
	else
	{
		assert(freopen("input.txt","r",core.stdc.stdio.stdin));
		assert(freopen("output.txt","w",core.stdc.stdio.stdout));
	}
	int n,q,m;
loop:while(read(n,m,q))
	{
		auto g=new int[][n+2],r=new long[][n+2],ss=new long[][n+2],comps=new int[][n+2];
		auto c=new int[n+2],d=new int[n+2];
		int pos=1;
		foreach(i;0..m)
		{
			int u,v;
			read(u,v);
			g[u]~=v;
			g[v]~=u;
		}
		void dfs0(int v,int pr=-1)
		{
			comps[pos]~=v;
			c[v]=pos;
			foreach(x;g[v])if(x!=pr)dfs0(x,v);
		}
		void dfs(int v,int pr=-1,int h=0)
		{
			d[v]=max(d[v],h);
			foreach(x;g[v])if(x!=pr)dfs(x,v,h+1);
		}
		foreach(i;1..n+1)
		{
			if(c[i]==0)
			{
				dfs0(i);
				dfs(i);
				int ans=0,v=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
					d[x]=0;
				}
				dfs(v);
				ans=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
				}
				dfs(v);
				foreach(x;comps[pos])
				{
					r[pos]~=d[x];
				}
				sort(r[pos]);
				ss[pos]=r[pos];
				foreach_reverse(j;0..sz(ss[pos])-1)
				{
					ss[pos][j]+=ss[pos][j+1];
				}
				pos++;
			}
		}
		real cel(int x,int y)
		{
			long ans;
			long diam=max(r[x].back,r[y].back);
			auto a=assumeSorted(r[y]);
			foreach(v;r[x])
			{
				auto p=a.lowerBound(diam-x-1).length;
				ans+=p*diam;
				if(p<r[y].length)ans+=ss[y][p]+(ss[y].length-p)*(v+1);
			}
			return ans*1.00000L/(1L*r[x].length*r[y].length);
		}
		alias getans=memoize!cel;
		foreach(ii;0..q)
		{
			int u,v;
			read(u,v);
			int p1=c[u],p2=c[v];
			if(r[p1].length>r[p2].length)swap(p2,p1);
			if(p1==p2)write("-1\n");
			else writef("%.10f\n",getans(p1,p2));
		}
	}
}

module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,std.parallelism,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,
	core.stdc.stdio : freopen;
alias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");alias long lint;const int mod=1000000007;
alias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;
alias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,"fi",Y,"se")(x_,y_);}
	big gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	int sz(T)(T a) if(hasLength!T){return cast(int)a.length;}
	size_t lowb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	return s==ptrs.length;
}
bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	readln;
	return s==ptrs.length;
}
nothrow auto inf(in char* name) {return freopen(name,"r",core.stdc.stdio.stdin);}
nothrow auto ouf(in char* name) {return freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------

void main()
{
	version(ONLINE_JUDGE){}
	else
	{
		assert(freopen("input.txt","r",core.stdc.stdio.stdin));
		assert(freopen("output.txt","w",core.stdc.stdio.stdout));
	}
	int n,q,m;
loop:while(read(n,m,q))
	{
		auto g=new int[][n+2],r=new long[][n+2],ss=new long[][n+2],comps=new int[][n+2];
		auto c=new int[n+2],d=new int[n+2];
		int pos=1;
		foreach(i;0..m)
		{
			int u,v;
			read(u,v);
			g[u]~=v;
			g[v]~=u;
		}
		void dfs0(int v,int pr=-1)
		{
			comps[pos]~=v;
			c[v]=pos;
			foreach(x;g[v])if(x!=pr)dfs0(x,v);
		}
		void dfs(int v,int pr=-1,int h=0)
		{
			d[v]=max(d[v],h);
			foreach(x;g[v])if(x!=pr)dfs(x,v,h+1);
		}
		foreach(i;1..n+1)
		{
			if(c[i]==0)
			{
				dfs0(i);
				dfs(i);
				int ans=0,v=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
					d[x]=0;
				}
				dfs(v);
				ans=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
				}
				dfs(v);
				foreach(x;comps[pos])
				{
					r[pos]~=d[x];
				}
				sort(r[pos]);
				ss[pos]=r[pos];
				foreach_reverse(j;0..sz(ss[pos])-1)
				{
					ss[pos][j]+=ss[pos][j+1];
				}
				pos++;
			}
		}
		double cel(int x,int y)
		{
			long ans;
			long diam=max(r[x].back,r[y].back);
			auto a=assumeSorted(r[y]);
			foreach(v;r[x])
			{
				auto p=a.lowerBound(diam-v-1).length;
				ans+=p*diam;
				if(p<ss[y].length)ans+=ss[y][p]+(ss[y].length-p)*(v+1);
			}
			return ans*1.00000/(1L*r[x].length*r[y].length);
		}
		alias getans=memoize!cel;
		foreach(ii;0..q)
		{
			int u,v;
			read(u,v);
			int p1=c[u],p2=c[v];
			if(r[p1].length>r[p2].length)swap(p2,p1);
			if(p1==p2)write("-1\n");
			else writef("%.10f\n",getans(p1,p2));
		}
	}
}

module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,std.parallelism,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,
	core.stdc.stdio : freopen;
alias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");alias long lint;const int mod=1000000007;
alias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;
alias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,"fi",Y,"se")(x_,y_);}
	big gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	int sz(T)(T a) if(hasLength!T){return cast(int)a.length;}
	size_t lowb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	return s==ptrs.length;
}
bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	readln;
	return s==ptrs.length;
}
nothrow auto inf(in char* name) {return freopen(name,"r",core.stdc.stdio.stdin);}
nothrow auto ouf(in char* name) {return freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------

void main()
{
	version(ONLINE_JUDGE){}
	else
	{
		assert(freopen("input.txt","r",core.stdc.stdio.stdin));
		assert(freopen("output.txt","w",core.stdc.stdio.stdout));
	}
	int n,q,m;
loop:while(read(n,m,q))
	{
		auto g=new int[][n+2],r=new long[][n+2],ss=new long[][n+2],comps=new int[][n+2];
		auto c=new int[n+2],d=new int[n+2];
		int pos=1;
		foreach(i;0..m)
		{
			int u,v;
			read(u,v);
			g[u]~=v;
			g[v]~=u;
		}
		void dfs0(int v,int pr=-1)
		{
			comps[pos]~=v;
			c[v]=pos;
			foreach(x;g[v])if(x!=pr)dfs0(x,v);
		}
		void dfs(int v,int pr=-1,int h=0)
		{
			d[v]=max(d[v],h);
			foreach(x;g[v])if(x!=pr)dfs(x,v,h+1);
		}
		foreach(i;1..n+1)
		{
			if(c[i]==0)
			{
				dfs0(i);
				dfs(i);
				int ans=0,v=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
					d[x]=0;
				}
				dfs(v);
				ans=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
				}
				dfs(v);
				foreach(x;comps[pos])
				{
					r[pos]~=d[x];
				}
				sort(r[pos]);
				ss[pos]=r[pos];
				foreach_reverse(j;0..sz(ss[pos])-1)
				{
					ss[pos][j]+=ss[pos][j+1];
				}
				pos++;
			}
		}
		real cel(int x,int y)
		{
			long ans;
			long diam=max(r[x].back,r[y].back);
			foreach(v;r[x])
			{
				auto p=r[y].upb(diam-v-1);
				ans+=p*diam;
				if(p<ss[y].length)ans+=ss[y][p]+(ss[y].length-p)*(v+1);
			}
			return ans*1.00000L/(1L*r[x].length*r[y].length);
		}
		alias getans=memoize!cel;
		foreach(ii;0..q)
		{
			int u,v;
			read(u,v);
			int p1=c[u],p2=c[v];
			if(r[p1].length>r[p2].length)swap(p2,p1);
			if(p1==p2)write("-1\n");
			else writef("%.10f\n",getans(p1,p2));
		}
	}
}

module main;
import std.bigint,std.container,std.array,std.algorithm,std.conv,std.parallelism,
	std.functional,std.math,std.numeric,std.string,std.range,std.complex,
	std.typecons,std.regex,std.random,std.uni,std.traits,std.stdio,std.variant,
	core.stdc.stdio : freopen;
alias RedBlackTree Rbt;alias redBlackTree rbt;alias BigInt big;
alias pair(X,Y)=Tuple!(X,"fi",Y,"se");alias long lint;const int mod=1000000007;
alias pair!(int,int) pii;alias pair!(long,long) pll;alias BoyerMooreFinder strfind;
alias make_pair mp;alias binary_search bins;alias gcd=std.numeric.gcd;
pure nothrow
{
	T lcm (T)(auto ref T a,auto ref T b){return a/gcd(a,b)*b;}
	pair!(X,Y) make_pair(X,Y)(in X x_,in Y y_){return tuple!(X,"fi",Y,"se")(x_,y_);}
	big gcd(big a,big b){while(b){a%=b;swap(a,b);}return a;}
	@property X sqr(X)(in X a_) {return a_*a_;}
	@property X cub(X)(in X a_) {return a_*a_*a_;}
	int sz(T)(T a) if(hasLength!T){return cast(int)a.length;}
	size_t lowb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		size_t l=0,r=a.length;
		while(r-l>1)
		{
			auto m=(l+r)>>1;
			if(!(a[m]<g))r=m;
			else l=m;
		}
		return (!(a[l]<g))?l:r;
	}
	size_t upb(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		if(pos==a.length)return pos;
		else return a[pos]==g?pos+1:pos;
	}
	size_t binf(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		auto pos=lowb(a,g);
		return (g==a[pos])?pos:a.length;
	}
	bool binary_search(T,X)(T a,auto ref X g)
	 if(isRandomAccessRange!T && hasLength!T && is(typeof(g<a[0])==bool))
	{
		return binf(a,g)!=a.length;
	}
}
void putarr(X)(in X a,in char ch=' '){foreach(const ref i;a)write(i,ch);}
void putarr(X)(in X a,in size_t n=a.length,in size_t m=a.front.length)
{
	foreach(i;0..n)
	{
		foreach(j;0..m)write(a[i][j],' ');
		writeln;
	}
}
bool getarr(X)(X a,in size_t n)
{bool b=1; foreach(ref i;a[0..n])b=input(&i);return b;}
bool input(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	return s==ptrs.length;
}
bool read(T...)(auto ref T ptrs) if (ptrs.length > 0)
{
	size_t s=0;
	foreach(ref e;ptrs)
	{
		if(isSomeChar!(Unqual!(typeof(e))))s+=readf(" %c",&e);
		else s+=readf(" %s",&e);
	}
	readln;
	return s==ptrs.length;
}
nothrow auto inf(in char* name) {return freopen(name,"r",core.stdc.stdio.stdin);}
nothrow auto ouf(in char* name) {return freopen(name,"w",core.stdc.stdio.stdout);}
@property auto arread(T)(){return readln.splitter.map!(to!(T)).array;}
//foreach_reverse isAlpha isNumber isWhite isLower isUpper toLower toUpper mixin
//cumulativeFold return static heapify join filter foreach
//------------------------------------------program beginning--------------------------

void main()
{
	version(ONLINE_JUDGE){}
	else
	{
		assert(freopen("input.txt","r",core.stdc.stdio.stdin));
		assert(freopen("output.txt","w",core.stdc.stdio.stdout));
	}
	int n,q,m;
loop:while(read(n,m,q))
	{
		auto g=new int[][n+2],r=new long[][n+2],ss=new long[][n+2],comps=new int[][n+2];
		auto c=new int[n+2],d=new int[n+2];
		int pos=1;
		foreach(i;0..m)
		{
			int u,v;
			read(u,v);
			g[u]~=v;
			g[v]~=u;
		}
		void dfs0(int v,int pr=-1)
		{
			comps[pos]~=v;
			c[v]=pos;
			foreach(x;g[v])if(x!=pr)dfs0(x,v);
		}
		void dfs(int v,int pr=-1,int h=0)
		{
			d[v]=max(d[v],h);
			foreach(x;g[v])if(x!=pr)dfs(x,v,h+1);
		}
		foreach(i;1..n+1)
		{
			if(c[i]==0)
			{
				dfs0(i);
				dfs(i);
				int ans=0,v=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
					d[x]=0;
				}
				dfs(v);
				ans=0;
				foreach(x;comps[pos])
				{
					if(ans<d[x])
					{
						ans=d[x];
						v=x;
					}
				}
				dfs(v);
				foreach(x;comps[pos])
				{
					r[pos]~=d[x];
				}
				sort(r[pos]);
				ss[pos]=r[pos];
				foreach_reverse(j;0..ss[pos].length-1)
				{
					ss[pos][j]+=ss[pos][j+1];
				}
				pos++;
			}
		}
		real cel(int x,int y)
		{
			long ans;
			long diam=max(r[x].back,r[y].back);
			auto a=assumeSorted(r[y]);
			foreach(v;r[x])
			{
				auto p=a.lowerBound(diam-x-1).length;
				ans+=p*diam;
				if(p<r[y].length)ans+=ss[y][p]+(ss[y].length-p)*(v+1);
			}
			return ans*1.00000L/(1L*r[x].length*r[y].length);
		}
		alias getans=memoize!cel;
		foreach(ii;0..q)
		{
			int u,v;
			read(u,v);
			int p1=c[u],p2=c[v];
			if(r[p1].length>r[p2].length)swap(p2,p1);
			if(p1==p2)write("-1\n");
			else writef("%.10f\n",getans(p1,p2));
		}
	}
}


import std.algorithm;
import std.stdio;

void main() {
    int N, K;
    readf(" %s %s", &N, &K);
    int[] A = new int[N];
    foreach (i, ref a; A) {
        readf(" %s", &a);
        if (a < 0 && K > 0) {
            a *= -1;
            K--;
        }
    }
    sort(A);
    while (K > 0) {
        A[0] *= -1;
        K--;
    }
    writeln(reduce!("a + b")(0, A));
}

import std.stdio : write, writeln, writefln, stdin;
import std.array;
import std.range;
import std.typecons;
import std.algorithm : max, min;

int n;
int k;
int[] a;

T sum(T)(T[] l ...){
	T s;
	foreach(v; l){
		s += v;
	}
	return s;
}

void main(){
	n.next;
	k.next;
	a = a.init;
	foreach(i; n.iota){
		a ~= next!int();
	}

	/+
	foreach(ref v; a){
		if( v > 0 ){ break; }
		if( k <= 0 ){ break; }
		v *= -1;
		--k;
	}
	+/
	bool flag = true;
	for(int i=0; 0<k; --k){
		if( a[i] >= 0 ){
			a.sort;
			for(int j=0; j<k%2; ++j){
				a[0]*=-1;
			}
			break;
		}else{
			a[i] *= -1;
			if( i+1 < n ) ++i;
		}

	}

	a.sum.writeln;
}

import std.stdio : readln;
import std.conv : to;
import std.string : split, chomp;
shared string[] input;
shared string delim = " ";
T next(T)()
in
{
	assert(hasNext());
}
out
{
	input.popFront;
}
body
{
	return input.front.to!T;
}

void next(T)(ref T v){
	v = next!T();
}

bool hasNext(){
	if(input.length > 0){
		return true;
	}

	string str = readln;
	if(str.length > 0){
		input ~= str.chomp.split(delim);
		return true;
	}else{
		return false;
	}
}

void dbg(T...)(T vs)
{
	import std.stdio : stderr;
	foreach(v; vs)
		stderr.write(v.to!string ~ " ");
	stderr.write("\n");
}

T clone(T)(T v){
	T v_;
	static if(isInputRange!(T)){
		foreach(ite; v){
			v_ ~= ite.clone;
		}
	}else{
		v_ = v;
	}

	return v_;
}

import std.stdio : write, writeln, writefln, stdin;
import std.array;
import std.range;
import std.typecons;
import std.algorithm : max, min;

int n;
int k;
int[] a;

T sum(T)(T[] l ...){
	T s;
	foreach(v; l){
		s += v;
	}
	return s;
}

void main(){
	n.next;
	k.next;
	a = a.init;
	foreach(i; n.iota){
		a ~= next!int();
	}

	a.sort;
	foreach(ref v; a){
		if( v > 0 ){ break; }
		if( k <= 0 ){ break; }
		v *= -1;
		--k;
	}
	a.sort;
	foreach(ref v; a){
		if( k <= 0 ){ break; }
		v *= -1;
		--k;
	}

	a.sum.writeln;
}

import std.stdio : readln;
import std.conv : to;
import std.string : split, chomp;
shared string[] input;
shared string delim = " ";
T next(T)()
in
{
	assert(hasNext());
}
out
{
	input.popFront;
}
body
{
	return input.front.to!T;
}

void next(T)(ref T v){
	v = next!T();
}

bool hasNext(){
	if(input.length > 0){
		return true;
	}

	string str = readln;
	if(str.length > 0){
		input ~= str.chomp.split(delim);
		return true;
	}else{
		return false;
	}
}

void dbg(T...)(T vs)
{
	import std.stdio : stderr;
	foreach(v; vs)
		stderr.write(v.to!string ~ " ");
	stderr.write("\n");
}

T clone(T)(T v){
	T v_;
	static if(isInputRange!(T)){
		foreach(ite; v){
			v_ ~= ite.clone;
		}
	}else{
		v_ = v;
	}

	return v_;
}

import std.stdio : write, writeln, writefln, stdin;
import std.array;
import std.range;
import std.typecons;
import std.algorithm : max, min;

int n;
int k;
int[] a;

T sum(T)(T[] l ...){
	T s;
	foreach(v; l){
		s += v;
	}
	return s;
}

void main(){
	n.next;
	k.next;
	a = a.init;
	foreach(i; n.iota){
		a ~= next!int();
	}

	/+
	foreach(ref v; a){
		if( v > 0 ){ break; }
		if( k <= 0 ){ break; }
		v *= -1;
		--k;
	}
	+/
	bool flag = true;
	for(int i=0; 0<k; --k){
		if( a[i] >= 0){
			flag = false;
		}

		a[i] *= -1;

		if( flag && i+1<n ){
			++i;
		}
	}

	assert( k == 0 );
	a.sum.writeln;
}

import std.stdio : readln;
import std.conv : to;
import std.string : split, chomp;
shared string[] input;
shared string delim = " ";
T next(T)()
in
{
	assert(hasNext());
}
out
{
	input.popFront;
}
body
{
	return input.front.to!T;
}

void next(T)(ref T v){
	v = next!T();
}

bool hasNext(){
	if(input.length > 0){
		return true;
	}

	string str = readln;
	if(str.length > 0){
		input ~= str.chomp.split(delim);
		return true;
	}else{
		return false;
	}
}

void dbg(T...)(T vs)
{
	import std.stdio : stderr;
	foreach(v; vs)
		stderr.write(v.to!string ~ " ");
	stderr.write("\n");
}

T clone(T)(T v){
	T v_;
	static if(isInputRange!(T)){
		foreach(ite; v){
			v_ ~= ite.clone;
		}
	}else{
		v_ = v;
	}

	return v_;
}


module _;
void main() {
	import std.stdio;
	import std.string, std.conv;
	int n = readln.strip.to!int;
	auto s = readln.strip;

	bool check(int limit, ref string co, bool doco) {
		int r = 0, b = 0;
		foreach(ch; s) {
			if (ch == '(') {
				if (r < limit) {
					r++;
					if (doco) co ~= '0';
				} else if (b < limit) {
					b++;
					if (doco) co ~='1';
				} else {
					return false;
				}
			} else {
				if (r > 0) {
					r--;
					if (doco) co ~= '0';
				} else if (b > 0) {
					b--;
					if (doco) co ~='1';
				} else {
					// impossible
					return false;
				}
			}
		}
		return true;
	}

	string ans = "";
	int l = 0, r = n;
	while (l < r-1) {
		int mid = (l+r)/2;
		if (check(mid, ans, false)) {
			r = mid;
		} else {
			l = mid;
		}
	}
	check(r, ans, true);
	writeln(ans);
}

//prewritten code: https://github.com/antma/algo
import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.traits;
import std.random;
void main() {
  immutable n = readln.strip.to!int;
  auto s = readln.take(n).array;
  int[] st;
  auto x = new int[n];
  auto ans = new char[n];
  foreach (i; 0 .. n) {
    if (s[i] == '(') {
      st ~= i;
      ans[i] = (st.length & 1) ? '0' : '1';
    } else {
      int j = st.back;
      st.popBack ();
      x[i] = j;
      x[j] = i;
      ans[i] = ans[j];
    }
  }
  writeln (ans);
}

import std.stdio, std.conv, std.string;
import std.array, std.range, std.algorithm, std.container;
import std.math, std.random, std.bigint, std.datetime, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }

int DEBUG_LEVEL = 0;
void print()(){ writeln(""); }
void print(T, A ...)(T t, lazy A a){ write(t), print(a); }
void print(int level, T, A ...)(T t, lazy A a){ if(level <= DEBUG_LEVEL) print(t, a); }

void main(string[] args){
	if(args.length > 1 && args[1] == "-debug"){
		if(args.length > 2 && args[2].isNumeric) DEBUG_LEVEL = args[2].to!int;
		else DEBUG_LEVEL = 1;
	}

	int n = read.to!int;
	string s = readln.chomp;

	int d = 0;
	int dmax = 0;
	foreach(c; s){
		if(c == '(') d += 1;
		else d -= 1;
		if(d > dmax) dmax = d;
	}

	char[] ans;
	foreach(c; s){
		if(c == '('){
			d += 1;
			if(d > dmax / 2) ans ~= "1";
			else ans ~= "0";
		}
		else{
			if(d > dmax / 2) ans ~= "1";
			else ans ~= "0";
			d -= 1;
		}
	}

	ans.writeln;
}

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

void main() {
    auto N = readln.chomp.to!int;
    auto S = readln.chomp;
    auto ans = new int[](N);
    int tmp = 0;
    foreach (i; 0..N) {
        if (S[i] == '(') {
            ans[i] = tmp % 2;
            tmp += 1;
        } else {
            tmp -= 1;
            ans[i] = tmp % 2;
        }
    }
    ans.map!(to!string).join("").writeln;
}


import std.algorithm,std.stdio,core.bitop;void main(){int n,m,w,k;scanf("%d",&n);auto s=new int[n];foreach(ref x;s)scanf("%d",&x);sort!"a>b"(s);scanf("%d ",&m);int[]t;foreach(i;0..m){auto r=readln;t~=(r[0]=='p')*2+(r[2]=='2');}w=1<<m;auto f=new int[w];foreach(u;1..w){k=m-u.popcnt;f[u]=(t[k]&1)?int.max:int.min;foreach(j;0..m)if(u&(1<<j)){int v=u^(1<<j);switch(t[k]){case 0:f[u]=max(f[u],f[v]);break;case 1:f[u]=min(f[u],f[v]);break;case 2:f[u]=max(f[u],f[v]+s[j]);break;case 3:f[u]=min(f[u],f[v]-s[j]);break;default:}}}f[w-1].writeln;}

import std.algorithm;
import std.stdio;
import std.string;
import core.bitop;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto s = new int [n];
		foreach (ref x; s)
		{
			readf (" %s", &x);
		}
		sort !("a > b") (s);

		int m;
		readf (" %s ", &m);
		int [] t;
		foreach (i; 0..m)
		{
			auto r = readln.strip;
			t ~= (r[0] == 'p') * 2 + (r[2] == '2');
		}
		s = s[0..m];

		int m2 = 1 << m;
		auto f = new int [m2];
		f[m2 - 1] = 0;

		foreach_reverse (u; 0..m2 - 1)
		{
			int i = popcnt (u);
			f[u] = (t[i] & 1) ? int.max : int.min;
			foreach (j; 0..m)
			{
				if (!(u & (1 << j)))
				{
					int v = u ^ (1 << j);
					switch (t[i])
					{
						case 0: // b 1
							f[u] = max (f[u],
							    f[v]);
							break;
						case 1: // b 2
							f[u] = min (f[u],
							    f[v]);
							break;
						case 2: // p 1
							f[u] = max (f[u],
							    f[v] + s[j]);
							break;
						case 3: // p 2
							f[u] = min (f[u],
							    f[v] - s[j]);
						        break;
					        default:
							assert (false);
					}
				}
			}
		}

		writeln (f[0]);
	}
}

import std.algorithm, std.stdio, std.string, core.bitop;
void main ()
{
	int n;
	readf (" %s", &n);
	auto s = new int [n];
	foreach (ref x; s) readf (" %s", &x);
	sort !("a > b") (s);
	int m;
	readf (" %s ", &m);
	int [] t;
	foreach (i; 0..m)
	{
		auto r = readln.strip;
		t ~= (r[0] == 'p') * 2 + (r[2] == '2');
	}
	int m2 = 1 << m;
	auto f = new int [m2];
	foreach (u; 1..m2)
	{
		int i = m - popcnt (u);
		f[u] = (t[i] & 1) ? int.max : int.min;
		foreach (j; 0..m) if (u & (1 << j))
		{
			int v = u ^ (1 << j);
			switch (t[i])
			{
				case 0: f[u] = max (f[u], f[v]); break;
				case 1: f[u] = min (f[u], f[v]); break;
				case 2: f[u] = max (f[u], f[v] + s[j]); break;
				case 3: f[u] = min (f[u], f[v] - s[j]); break;
				default: assert (false);
			}
		}
	}
	writeln (f[$ - 1]);
}

import std.algorithm, std.stdio, std.string, core.bitop;
void main ()
{
	int n;
	scanf (" %d", &n);
	auto s = new int [n];
	foreach (ref x; s) scanf (" %d", &x);
	sort !("a > b") (s);
	int m;
	scanf (" %d ", &m);
	int [] t;
	foreach (i; 0..m)
	{
		auto r = readln.strip;
		t ~= (r[0] == 'p') * 2 + (r[2] == '2');
	}
	int m2 = 1 << m;
	auto f = new int [m2];
	foreach_reverse (u; 0..m2 - 1)
	{
		int i = popcnt (u);
		f[u] = (t[i] & 1) ? int.max : int.min;
		foreach (j; 0..m)
		{
			if (u & (1 << j)) continue;
			int v = u ^ (1 << j);
			switch (t[i])
			{
				case 0: f[u] = max (f[u], f[v]); break;
				case 1: f[u] = min (f[u], f[v]); break;
				case 2: f[u] = max (f[u], f[v] + s[j]); break;
				case 3: f[u] = min (f[u], f[v] - s[j]); break;
				default: assert (false);
			}
		}
	}
	writeln (f[0]);
}

import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import core.bitop;

immutable int INF = int.max / 2;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto s = new int [n];
		foreach (ref x; s)
		{
			readf (" %s", &x);
		}
		sort !("a > b") (s);

		int m;
		readf (" %s ", &m);
		int [] t;
		foreach (i; 0..m)
		{
			auto r = readln.strip;
			t ~= (r[0] == 'p') * 2 + (r[2] == '2');
			debug {writeln (r, ' ', t[i]);}
		}
		s = s[0..m];

		int m2 = 1 << m;
		auto f = new int [] [] (m + 1, m2);
		foreach (ref g; f)
		{
			g[] = int.max;
		}

		int fun (int i, int u)
		{
			if (i == m)
			{
				return 0;
			}
			if (f[i][u] != int.max)
			{
				return f[i][u];
			}
			int res = (t[i] & 1) ? +INF : -INF;
			foreach (j; 0..m)
			{
				if (!(u & (1 << j)))
				{
					int v = u ^ (1 << j);
					switch (t[i])
					{
						case 0: // b 1
							res = max (res,
							    fun (i + 1, v));
							break;
						case 1: // b 2
							res = min (res,
							    fun (i + 1, v));
							break;
						case 2: // p 1
							res = max (res,
							    fun (i + 1, v) + s[j]);
							break;
						case 3: // p 2
							res = min (res,
							    fun (i + 1, v) - s[j]);
						        break;
					        default:
							assert (false);
					}
				}
			}
			debug {writeln (i, ' ', u, ' ', res);}
			f[i][u] = res;
			return res;
		}

		writeln (fun (0, 0));
	}
}

import std.algorithm, std.stdio, std.string, core.bitop;
void main ()
{
	int n;
	readf (" %s", &n);
	auto s = new int [n];
	foreach (ref x; s) readf (" %s", &x);
	sort !("a > b") (s);
	int m;
	readf (" %s ", &m);
	int [] t;
	foreach (i; 0..m)
	{
		auto r = readln.strip;
		t ~= (r[0] == 'p') * 2 + (r[2] == '2');
	}
	int m2 = 1 << m;
	auto f = new int [m2];
	foreach_reverse (u; 0..m2 - 1)
	{
		int i = popcnt (u);
		f[u] = (t[i] & 1) ? int.max : int.min;
		foreach (j; 0..m) if (!(u & (1 << j)))
		{
			int v = u ^ (1 << j);
			switch (t[i])
			{
				case 0: f[u] = max (f[u], f[v]); break;
				case 1: f[u] = min (f[u], f[v]); break;
				case 2: f[u] = max (f[u], f[v] + s[j]); break;
				case 3: f[u] = min (f[u], f[v] - s[j]); break;
				default: assert (false);
			}
		}
	}
	writeln (f[0]);
}

import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import core.bitop;

immutable int INF = int.max / 2;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto s = new int [n];
		foreach (ref x; s)
		{
			readf (" %s", &x);
		}
		sort !("a > b") (s);

		int m;
		readf (" %s ", &m);
		int [] t;
		foreach (i; 0..m)
		{
			auto r = readln.strip;
			t ~= (r[0] == 'p') * 2 + (r[2] == '2');
			debug {writeln (r, ' ', t[i]);}
		}
		s = s[0..m];

		int m2 = 1 << m;
		auto f = new int [m2];
		f[] = int.max;

		int fun (int u)
		{
			int i = popcnt (u);
			if (i == m)
			{
				return 0;
			}
			if (f[u] != int.max)
			{
				return f[u];
			}
			int res = (t[i] & 1) ? +INF : -INF;
			foreach (j; 0..m)
			{
				if (!(u & (1 << j)))
				{
					int v = u ^ (1 << j);
					switch (t[i])
					{
						case 0: // b 1
							res = max (res,
							    fun (v));
							break;
						case 1: // b 2
							res = min (res,
							    fun (v));
							break;
						case 2: // p 1
							res = max (res,
							    fun (v) + s[j]);
							break;
						case 3: // p 2
							res = min (res,
							    fun (v) - s[j]);
						        break;
					        default:
							assert (false);
					}
				}
			}
			f[u] = res;
			return res;
		}

		writeln (fun (0));
	}
}

import std.algorithm, std.stdio, std.string, core.bitop;
void main () {
	int n;
	readf (" %s", &n);
	auto s = new int [n];
	foreach (ref x; s) readf (" %s", &x);
	sort !("a > b") (s);
	int m;
	readf (" %s ", &m);
	int [] t;
	foreach (i; 0..m) {
		auto r = readln.strip;
		t ~= (r[0] == 'p') * 2 + (r[2] == '2');
	}
	int m2 = 1 << m;
	auto f = new int [m2];
	foreach (u; 1..m2) {
		int i = m - u.popcnt;
		f[u] = (t[i] & 1) ? int.max : int.min;
		foreach (j; 0..m) if (u & (1 << j)) {
			int v = u ^ (1 << j);
			switch (t[i]) {
				case 0: f[u] = max (f[u], f[v]); break;
				case 1: f[u] = min (f[u], f[v]); break;
				case 2: f[u] = max (f[u], f[v] + s[j]); break;
				case 3: f[u] = min (f[u], f[v] - s[j]); break;
				default: assert (false);
	}	}	}
	f[$ - 1].writeln;
}

import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import core.bitop;

immutable int INF = int.max / 2;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto s = new int [n];
		foreach (ref x; s)
		{
			readf (" %s", &x);
		}
		sort !("a > b") (s);

		int m;
		readf (" %s ", &m);
		int [] t;
		foreach (i; 0..m)
		{
			auto r = readln.strip;
			t ~= (r[0] == 'p') * 2 + (r[2] == '2');
			debug {writeln (r, ' ', t[i]);}
		}
		s = s[0..m];

		int m2 = 1 << m;
		auto f = new int [] [] (m + 1, m2);
		f[0][0] = 0;
		foreach (i; 1..m + 1)
		{
			foreach (u; 0..m2)
			{
				if (popcnt (u) != i)
				{
					continue;
				}
				f[i][u] = (t[i - 1] & 1) ? -INF : +INF;
				foreach (j; 0..m)
				{
					if (u & (1 << j))
					{
						int v = u ^ (1 << j);
						switch (t[i - 1])
						{
							case 0: // b 1
								f[i][u] = min (f[i][u],
								    f[i - 1][v]);
								break;
							case 1: // b 2
								f[i][u] = max (f[i][u],
								    f[i - 1][v]);
								break;
							case 2: // p 1
								f[i][u] = min (f[i][u],
								    f[i - 1][v] + s[j]);
								break;
							case 3: // p 2
								f[i][u] = max (f[i][u],
								    f[i - 1][v] - s[j]);
							        break;
						        default:
								assert (false);
						}
					}
				}
				debug {writeln (i, ' ', u, ' ', f[i][u]);}
			}
		}
		writeln (f[m][m2 - 1]);
	}
}

import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.exception;
import std.functional;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
import core.bitop;

immutable int INF = int.max / 2;

void main ()
{
	int n;
	while (readf (" %s", &n) > 0)
	{
		auto s = new int [n];
		foreach (ref x; s)
		{
			readf (" %s", &x);
		}
		sort !("a > b") (s);

		int m;
		readf (" %s ", &m);
		int [] t;
		foreach (i; 0..m)
		{
			auto r = readln.strip;
			t ~= (r[0] == 'p') * 2 + (r[2] == '2');
		}
		s = s[0..m];

		int m2 = 1 << m;
		auto f = new int [] [] (m + 1, m2);
		f[0][0] = 0;
		foreach (i; 1..m + 1)
		{
			foreach (u; 0..m2)
			{
				if (popcnt (u) != i)
				{
					continue;
				}
				f[i][u] = (t[i - 1] & 1) ? +INF : -INF;
				foreach (j; 0..m)
				{
					if (u & (1 << j))
					{
						int v = u ^ (1 << j);
						switch (t[i - 1])
						{
							case 0:
								f[i][u] = max (f[i][u],
								    f[i - 1][v]);
								break;
							case 1:
								f[i][u] = min (f[i][u],
								    f[i - 1][v]);
								break;
							case 2:
								f[i][u] = max (f[i][u],
								    f[i - 1][v] - s[j]);
								break;
							case 3:
								f[i][u] = min (f[i][u],
								    f[i - 1][v] + s[j]);
							        break;
						        default:
								assert (false);
						}
					}
				}
			}
		}
		writeln (-f[m][m2 - 1]);
	}
}


import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

void main() {
  try {
    for (; ; ) {
      const M = readInt();
      const N = readInt();
      const Q = readInt();
      auto R = new int[Q];
      auto C = new int[Q];
      foreach (q; 0 .. Q) {
        R[q] = readInt() - 1;
        C[q] = readInt() - 1;
      }

      auto uf = new int[M + N];
      uf[] = -1;
      foreach (q; 0 .. Q) {
        uf.connect(R[q], M + C[q]);
      }
      int num, isoRow, isoCol;
      foreach (u; 0 .. M + N) {
        if (uf[u] < 0) {
          if (-uf[u] >= 2) {
            ++num;
          } else {
            (u < M) ? ++isoRow : ++isoCol;
          }
        }
      }
      debug {
        writeln(num, " ", isoRow, " ", isoCol);
      }
      int ans;
      if (num == 0) {
        ans = M + N - 1;
      } else {
        ans += num - 1;
        ans += isoRow;
        ans += isoCol;
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

import core.bitop, std.bitmanip;
import core.checkedint;
import std.algorithm, std.functional, std.meta;
import std.array, std.container;
import std.bigint;
import std.conv;
import std.math, std.numeric;
import std.range, std.range.interfaces;
import std.stdio, std.string;
import std.typecons;

void main()
{
    int n, m, q;
    readf("%s %s %s", &n, &m, &q);
    readln;

    Tuple!(int, int)[] prs;

    foreach (_; 0 .. q) {
        int x, y;
        readf("%s %s", &x, &y);
        readln;

        prs ~= tuple(x, y);
    }

    struct uf {
        int[] p;
        int[] r;
        int sets;

        this(int sz) {
            p = new int[] (sz+1);
            foreach (i; 0 .. sz+1) { p[i] = i; }
            r = new int[] (sz+1);
            r[] = 0;
            sets = sz;
        }

        int fnd(int x) {
            if (p[x] != x) { p[x] = fnd(p[x]); }

            return p[x];
        }

        void uni(int a, int b) {
            a = fnd(a);
            b = fnd(b);

            if (a == b) { return; }

            if (r[a] > r[b]) { swap(a, b); }

            p[a] = b;
            if (r[a] == r[b]) { r[b] += 1; }

            --sets;
        }
    }

    uf u = uf(m);

    prs.sort();

    foreach (i; 1 .. q) {
        if (prs[i-1][0] == prs[i][0]) {
            u.uni(prs[i-1][1], prs[i][1]);
        }
    }

    int groups = u.sets;

    debug { groups.writeln; }

    int emptyRows = n - prs.map!(t => t[0]).array.uniq.array.length.to!int;

    auto ans = groups - 1 + emptyRows;

    ans.writeln;
}

import std.range;
import std.stdio;

void main ()
{
	int n, m, q;
	while (readf (" %s %s %s", &n, &m, &q) > 0)
	{
		int res = n + m - 1;
		auto p = (n + m).iota.array;

		int root (int w)
		{
			if (p[w] != w)
			{
				p[w] = root (p[w]);
			}
			return p[w];
		}

		bool unite (int u, int v)
		{
			u = root (u);
			v = root (v);
			if (u == v)
			{
				return false;
			}
			p[u] = v;
			return true;
		}

		foreach (i; 0..q)
		{
			int u, v;
			readf (" %s %s", &u, &v);
			u -= 1;
			v -= 1;
			v += n;
			if (unite (u, v))
			{
				res -= 1;
			}
		}
		writeln (res);
	}
}

import std.stdio;
import std.uni;
import std.conv;
import std.container;
import std.functional;
import std.algorithm;
import std.array;
import std.typecons;
import std.range;
import std.numeric;
import std.traits;

void main(string[] args)
{
  auto n = next!int;
  auto m = next!int;
  auto adjr = new int[][](n);
  auto visr = new bool[](n);
  auto adjc = new int[][](m);
  auto visc = new bool[](m);
  auto q = next!int;
  foreach(qi; 0 .. q)
    {
      auto r = next!int - 1;
      auto c = next!int - 1;
      adjr[r] ~= c;
      adjc[c] ~= r;
    }
  void dfs(int i, int roc)
  {
    if (roc)
      {
	visr[i] = true;
	foreach(nei; adjr[i])
	  if (!visc[nei])
	    dfs(nei, 0);
      }
    else
      {
	visc[i] = true;
	foreach(nei; adjc[i])
	  if (!visr[nei])
	    dfs(nei, 1);
      }
  }
  int cmps = 0;
  foreach(i; 0 .. n)
    {
      if (!visr[i])
	{
	  dfs(i, 1);
	  cmps++;
	}
    }

  foreach(i; 0 .. m)
    {
      if (!visc[i])
	{
	  dfs(i, 0);
	  cmps++;
	}
    }
  (cmps - 1).writeln;
}

static this()
{
  popChar;
}

struct Any(T)
{
  static auto read()
  {
    static if (is(T == char))
      {
	auto res = frontChar;
	popChar;
	return res;
      }
  }
}

struct Unsigned(T)
{
  auto read()
  {
    auto res = T(0);
    while(!frontChar.isDigit) popChar;
    do
      {
	res = res * T(10) + T(frontChar - '0');
	popChar;
      }
    while(frontChar.isDigit);
    return res;
  }
}

struct Matrix(T, alias rows, alias cols)
{
  T[][] _cell;
  alias _cell this;
  // Ring constructor
  this(int n)
  {
    final switch(n)
      {
      case 0:
	_cell = new T[][](rows, cols);
	break;
      case 1:
	debug assert(rows == cols);
	_cell = new T[][](rows, cols);
	foreach(i; 0 .. rows)
	  _cell[i][i] = T(1);
	break;
      }
  }
  // Copy constructor
  this(Matrix!(T, rows, cols) other)
  {
    _cell = new T[][](rows, cols);
    foreach(i; 0 .. rows)
      _cell[i][] = other[i][];
  }
  auto opBinary(string op)(Matrix!(T, rows, cols) other)
       if (op == "+" || op == "-")
	 {
	   auto res = Matrix!(T, rows, cols)(0);
	   foreach(i; 0 .. rows)
	     foreach(j; 0 .. cols)
	       res[i][j] = mixin(q{this[i][j]}, op, q{other[i][j]});
	   return res;
	 }
  auto opBinary(string op, alias otherRows, alias otherCols)(Matrix!(T, otherRows, otherCols) other)
       if (op == "*")
	 {
	   debug assert(cols == otherRows);
	   auto res = Matrix!(T, rows, otherCols)(0);
	   foreach(i; 0 .. rows)
	     foreach(k; 0 .. cols)
	       foreach(j; 0 .. otherCols)
		 res[i][j] += this[i][k] * other[k][j];
	   return res;
	 }
  ref auto opOpAssign(string op)(Matrix!(T, rows, cols) other)
  {
    static if (op == "*")
      {
	debug assert(rows == cols);
	alias n = rows;
	auto res = Matrix!(T, n, n)(0);
	T factor;
	foreach(i; 0 .. n)
	  foreach(k; 0 .. n)
	    foreach(j; 0 .. n)
	      res[i][j] += this[i][k] * other[k][j];
	foreach(i; 0 .. n)
	  this[i][] = res[i][];
      }
    else
      {
	foreach(i; 0 .. rows)
	  foreach(j; 0 .. cols)
	    mixin(q{this[i][j] }, text(op, q{=}), q{ other[i][j];});
      }
    return this;
  }
  mixin powMethod;
}

struct Mod(T, alias mod)
{
  alias This = Mod!(T, mod);
  T _rep;
  this(T t)
  {
    _rep = t % mod;
    if (_rep < 0) _rep += mod;
  }
  static auto plain(T t)
  {
    auto res = Mod!(T, mod)();
    res._rep = t;
    return res;
  }
  static auto fromPositive(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod);
  }
  static auto fromNegative(T t)
  {
    pragma(inline, true);
    return Mod!(T, mod).plain(t % mod + mod);
  }
  string toString()
  {
    return to!string(_rep);
  }
  debug invariant
  {
    assert(_rep >= 0 && _rep < mod);
  }
  auto opBinary(string op)(This b)
  {
    static if (op == "+")
      {
	T resrep = _rep + b._rep;
	if (resrep >= mod) resrep -= mod;
	return This.plain(resrep);
      }
    else static if (op == "-")
      {
	T resrep = _rep - b._rep;
	if (resrep < 0) resrep += mod;
	return This.plain(resrep);
      }
    else static if (op == "*")
      {
	return This.fromPositive(_rep * b._rep);
      }
  }
  auto opOpAssign(string op)(This b)
  {
    mixin(q{_rep }, text(op, "="), q{b._rep;});
    static if (op == "+")
      {
	if (_rep >= mod) _rep -= mod;
      }
    else static if (op == "-")
      {
	if (_rep < 0) _rep += mod;
      }
    else static if (op == "*")
      {
	_rep %= mod;
      }
    return this;
  }

  auto opBinary(string op)(long exp)
       if (op == "^^")
	 {
	   return pow(exp);
	 }
  mixin powMethod;
}

mixin template powMethod()
{
  auto pow(this ThisType)(long exp)
    {
      auto res = ThisType(1);
      auto pow = ThisType(this);
      while(exp)
	{
	  if (exp & 1) res *= pow;
	  pow *= pow;
	  exp >>= 1;
	}
      return res;
    }
}
// INPUT
char frontChar;
void popChar()
{
  import core.stdc.stdio;
  frontChar = cast(char) getchar;
  if (feof(stdin)) frontChar = ' ';
}
auto makeArray(T, S...)(S s)
{
  enum brackets = () {
    string res;
    foreach(i; 0 .. s.length)
      res ~= "[]";
    return res;
  } ();
  mixin(q{alias Type = T}, brackets, q{;});
  return new Type(s);
}
template isNumberLike(T)
{
  enum isNumberLike = is(typeof((){T test; test = test + test * test; test *= test;}()));
}
void popWhite()
{
  import std.ascii;
  while (frontChar.isWhite) popChar;
}
void print(T...)(T t)
{
  static foreach(ti; t)
    {
      static if (is(typeof(ti) == string))
	{
	  write(ti, " ");
	}
      else static if (isArray!(typeof(ti)))
	{
	  foreach(e; ti) print(e);
	}
      else static if (isTuple!(typeof(ti)))
	{
	  static foreach(i; ti.length) writesp(ti[i]);
	}
      else
	{
	  write(ti, " ");
	}
    }
}
void println(T...)(T t)
{
  static if (t.length == 0)
    writeln;
  static foreach(ti; t)
    {
      print(ti);
      writeln;
    }
}
auto next(alias T, S...)(S s)
{
  pragma(inline, true);
  static if (s.length > 0)
    {
      auto res = makeArray!(typeof(next!T()))(s);
      S t;
      enum loops = () {
	string res;
	foreach(i; 0 .. s.length)
	  {
	    auto ti = text(q{t[}, i, q{]});
	    res ~= text(q{for(}, ti, q{ = 0;}, ti , q{ < s[}, i, q{];}, ti, q{++)});
	  }
	return res;
      } ();
      enum indexing = () {
	string res = "res";
	foreach(i; 0 .. s.length)
	  res ~= text(q{[t[}, i, q{]]});
	return res;
      } ();
      mixin(loops, indexing, q{ = next!T;});
      return res;
    }
  else
    {
      static if (isNumberLike!T)
	{
	  import std.ascii: isDigit;
	  T res;
	  while(frontChar.isWhite) popChar;
	  T multiplier = T(1);
	  if (frontChar == '-')
	    {
	      multiplier = T(-1);
	      popChar;
	    }
	  else if (frontChar == '+')
	    {
	      popChar;
	    }
	  debug assert(frontChar.isDigit);
	  do
	    {
	      res = res * T(10) + T(frontChar - '0');
	      popChar;
	    }
	  while(frontChar.isDigit);
	  return multiplier * res;
	}
      else static if (is(T == string))
	{
	  import std.ascii: isWhite;
	  string res;
	  while(frontChar.isWhite)
	    popChar;
	  while(!frontChar.isWhite)
	    {
	      res ~= frontChar;
	      popChar;
	    }
	  return res;
	}
      else static if (is(T == char))
	{
	  while(frontChar.isWhite) popChar;
	  auto res = frontChar;
	  popChar;
	  return res;
	}
      else
	{
	  return T.read;
	}
    }
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

void main() {
  try {
    for (; ; ) {
      const M = readInt();
      const N = readInt();
      const Q = readInt();
      auto R = new int[Q];
      auto C = new int[Q];
      foreach (q; 0 .. Q) {
        R[q] = readInt() - 1;
        C[q] = readInt() - 1;
      }

      auto uf = new int[M + N];
      uf[] = -1;
      foreach (q; 0 .. Q) {
        uf.connect(R[q], M + C[q]);
      }
      int num, isoRow, isoCol;
      foreach (u; 0 .. M + N) {
        if (uf[u] < 0) {
          if (-uf[u] >= 2) {
            ++num;
          } else {
            (u < M) ? ++isoRow : ++isoCol;
          }
        }
      }
      debug {
        writeln(num, " ", isoRow, " ", isoCol);
      }
      int ans = num - 1;
      ans += max(isoRow, isoCol);
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

void main() {
  try {
    for (; ; ) {
      const M = readInt();
      const N = readInt();
      const Q = readInt();
      auto R = new int[Q];
      auto C = new int[Q];
      foreach (q; 0 .. Q) {
        R[q] = readInt() - 1;
        C[q] = readInt() - 1;
      }

      auto uf = new int[M + N];
      uf[] = -1;
      foreach (q; 0 .. Q) {
        uf.connect(R[q], M + C[q]);
      }
      int num, isoRow, isoCol;
      foreach (u; 0 .. M + N) {
        if (uf[u] < 0) {
          if (-uf[u] >= 2) {
            ++num;
          } else {
            (u < M) ? ++isoRow : ++isoCol;
          }
        }
      }
      debug {
        writeln(num, " ", isoRow, " ", isoCol);
      }
      int ans;
      if (num == 0) {
        ans = M + N - 1;
      } else {
        ans += num - 1;
        ans += max(isoRow, isoCol);
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}


import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;

void main ()
{
	auto tests = readln.strip.to !(int);
	foreach (test; 0..tests)
	{
		auto n = readln.strip.to !(int);
		auto a = readln.splitter.map !(to !(int)).array;
		auto p = n.iota.array;
		p.schwartzSort !(i => a[i], SwapStrategy.stable);
		auto s = p.map !(i => a[i]).group.map !(i => i[1]).maxElement;
		debug {writeln ("s = ", s);}
		auto q = p[s..$] ~ p[0..s];
		auto b = new int [n];
		foreach (i; 0..n)
		{
			b[q[i]] = a[p[i]];
		}
		writefln !("%(%s %)") (b);
	}
}

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

void main() {
  try {
    for (; ; ) {
      const numCases = readInt;
      foreach (caseId; 0 .. numCases) {
        const N = readInt;
        auto A = new int[N];
        foreach (i; 0 .. N) {
          A[i] = readInt;
        }

        auto xss = new int[][N + 1];
        foreach (i; 0 .. N) {
          xss[A[i]] ~= i;
        }
        auto lens = new int[N + 1];
        foreach (a; 1 .. N + 1) {
          lens[a] = cast(int)(xss[a].length);
        }

        auto as = iota(1, N + 1).array;
        as.sort!((a, b) => (lens[a] > lens[b]));
        debug {
          writeln("xss = ", xss);
          writeln("lens = ", lens);
          writeln("as = ", as);
        }

        auto ans = new int[N];
        for (; lens[as[0]] >= 1; ) {
          int[] ys;
          foreach (a; as) {
            if (lens[a] == 0) {
              break;
            }
            ys ~= xss[a][--lens[a]];
          }
          debug {
            writeln("ys = ", ys);
          }
          const ysLen = cast(int)(ys.length);
          foreach (j; 0 .. ysLen - 1) {
            ans[ys[j]] = A[ys[j + 1]];
          }
          ans[ys[ysLen - 1]] = A[ys[0]];
        }

        foreach (i; 0 .. N) {
          if (i) write(" ");
          write(ans[i]);
        }
        writeln;
      }
    }
  } catch (EOFException e) {
  }
}


import std.stdio, std.conv, std.functional, std.string;
import std.algorithm, std.array, std.container, std.range, std.typecons;
import std.bigint, std.numeric, std.math, std.random;
import core.bitop;

string FMT_F = "%.10f";

static File _f;
void file_io(string fn) { _f = File(fn, "r"); }
static string[] s_rd;
T _RD(T = long)() { while(!s_rd.length) s_rd = readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T _RD(T = long)(File f) { while(!s_rd.length) s_rd = f.readln.chomp.split; string res = s_rd[0]; s_rd.popFront; return res.to!T; }
T[] _RDA(T = long)(T fix = 0) { auto r = readln.chomp.split.to!(T[]); r[] += fix; return r; }
T[] _RDA(T = long)(File f, T fix = 0) { auto r = f.readln.chomp.split.to!(T[]); r[] += fix; return r; }
T RD(T = long)() { if (_f.isOpen) return _RD!T(_f); else return _RD!T; }
T[] RDA(T = long)(T fix = 0) { if (_f.isOpen)  return _RDA!T(_f, fix); else return _RDA!T(fix); }

size_t[] MAKE_IDX(alias less = "a < b", Range)(Range range) { auto idx = new size_t[](range.length); makeIndex!(less)(range, idx); return idx;}
size_t MIN_POS(alias less = "a < b", Range)(Range range) { auto r = minPos!(less)(range); return range.length - r.length; }

void chmin(T)(ref T x, T y) { x = min(x, y); } void chmax(T)(ref T x, T y) { x = max(x, y); }
bool inside(T)(T x, T b, T e) { return x >= b && x < e; }
T lcm(T)(T x, T y) { return x * (y / gcd(x, y)); }
double[] rotate(double[] vec, double rad) { return [cos(rad)*vec[0] - sin(rad)*vec[1], sin(rad)*vec[0] + cos(rad)*vec[1]]; }

long mod = 10^^9 + 7;
//long mod = 998_244_353;
//long mod = 1_000_003;
void moda(ref long x, long y) { x = (x + y) % mod; }
void mods(ref long x, long y) { x = ((x + mod) - (y % mod)) % mod; }
void modm(ref long x, long y) { x = (x * y) % mod; }
void modpow(ref long x, long y) { if (!y) { x = 1; return; } auto t = x; x.modpow(y>>1); x.modm(x); if (y&1) x.modm(t); }
void modd(ref long x, long y) { y.modpow(mod - 2); x.modm(y); }

void main()
{
	auto t = RD!int;
	auto ans = new long[](t);
	foreach (ti; 0..t)
	{
		auto n = RD!int;
		auto a = RDA;

		auto tot = a.sum;
		auto rem = tot % n;
		ans[ti] = rem * (n-rem);
	}

	foreach (e; ans)
		writeln(e);
	stdout.flush;
	debug readln;
}

// Vicfred
// https://codeforces.com/contest/1543/problem/B
// math, greedy
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;

void main() {
    int t = readln.chomp.to!int;

while(t--) {
    int n = readln.chomp.to!int;
    long[] a = readln.split.map!(to!long).array;
    long A = a.sum % n;
    writefln("%s", A * (n - A));
}
}

// code by cutx64

module main;

import std.string, std.array, std.container, std.typecons;
import std.stdio, std.format, std.file, std.outbuffer;
import std.algorithm, std.conv, std.functional, std.range, core.bitop;
// import std.bigint, std.complex, std.bitmanip;
import std.math, std.mathspecial, std.numeric;

void main() {

  int tests; readf!"%s\n"(tests);
  while(tests--) {
    int n; readf!"%s\n"(n);
    auto a = readln.splitter.map!(to!long).array;
    long s = a.sum;
    long r = s % n;
    (r * (n - r)).writeln;
  }

} // main

// Cheese-Cracker: cheese-cracker.github.io

void theCode(){
    int n = scan!int;
    auto arr = scanArray;
    arr.sort;
    ll summ = arr.sum;
    ll av = summ / n;
    ll rem = summ % n;
    writeln(rem * (n - rem));
}

void main(){
    long tests = scan;        // Toggle!
    while(tests--) theCode();
}

/*********  That's All Folks!  *********/
import std.stdio, std.random, std.functional, std.container, std.algorithm;
import std.numeric, std.range, std.typecons, std.string, std.math, std.conv;
string[] tk; T scan(T=long)(){while(!tk.length)tk = readln.split; string a=tk.front; tk.popFront; return a.to!T;}
T[] scanArray(T=long)(){ auto r = readln.split.to!(T[]); return r; }
void show(A...)(A a){ debug{ foreach(t; a){stderr.write(t, "| ");} stderr.writeln; } }
alias ll = long, tup = Tuple!(long, "x",  long, "y");

import std.algorithm, std.array, std.container, std.conv, std.math, std.numeric, std.random, std.range, std.stdio, std.string, std.typecons, core.bitop;

void main()
{
  long t;
  readf!" %d "(t);
  while (t--) {
    long n;
    readf!" %d "(n);
    auto a = readln.strip.split.map!(to!long).array.sort.array;
    auto sum = a.sum;
    if (sum % n == 0) {
      writeln(0);
      continue;
    }
    auto avgru = (sum + n - 1) / n;
    auto avgrd = (sum) / n;
    auto rem = sum - avgrd * n;
//    writefln("n=%d rem=%d", n, rem);
    long result = rem * abs(n - rem);
    writeln(result);
  }
}


module cf_276B;

import std.stdio;

void main() {
    immutable MAX_LETTER_COUNT = 26;
    immutable FIRST_LETTER = 'a';

    string text;
    int[MAX_LETTER_COUNT] counters;

    readf("%s\n", &text);

    for (int i = 0; i < text.length; ++i) {
        ++counters[text[i] - FIRST_LETTER];
    }

    int odds = 0;
    for (int i = 0; i < MAX_LETTER_COUNT; ++i) {
        if (counters[i] % 2 != 0) {
            ++odds;
        }
    }

    if (odds == 0 || odds % 2 != 0) {
        writeln("First");
    } else {
        writeln("Second");
    }
}