#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef long double ld;
const string file = "matrice5";
const ll INF = 9223372036854775807ll;
const int inf = 2147483647, M = 10007;
int pw(int x, int y)
{
x %= M;
int p = 1;
while(y)
if(y%2==0){
x = (x*x)%M;
y /= 2;
}else{
p = (p*x)%M;
--y;
}
return p;
}
int main()
{
ifstream fin (file+".in");
ofstream fout (file+".out");
int t;
fin >> t;
while(t--){
int n, m, p, k;
fin >> n >> m >> p >> k;
fout << (pw(p*k, (n-1)*(m-1))*pw(p, n+m-1))%M << "\n";
}
return 0;
}