//B
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
void solve() {
const int D = 3;
vector<ll> sz(D);
for (int i = 0; i < D; i++) cin >> sz[i];
vector<ll> cut(D); // p, q, r
for (int i = 0; i < D; i++) {
cin >> cut[i];
cut[i]++;
}
ll n;
cin >> n;
vector<vector<ll>> pts(n, vector<ll>(D));
for (int i = 0; i < n; i++) {
for (int j = 0; j < D; j++) {
cin >> pts[i][j];
}
}
ll total = 1;
for (int w = 0; w < D; w++) total *= cut[w];
if (n % total != 0) {
cout << 0 << endl;
return;
}
ll each = n / total;
vector<vector<ll>> pos(D);
ll ans = 1;
for (int w = 0; w < D; w++) {
vector<ll> v(n);
for (int i = 0; i < n; i++) v[i] = pts[i][w];
sort(v.begin(), v.end());
ll step = n / cut[w];
for (int i = 0; i < n; i += step) {
pos[w].push_back(v[i]);
if (i != 0) {
ans = ans * (v[i] - v[i - 1]) % mod;
}
}
}
vector<ll> cnt(total, 0);
for (auto& p : pts) {
ll id = 0;
for (int w = 0; w < D; w++) {
ll j = upper_bound(pos[w].begin(), pos[w].end(), p[w]) - pos[w].begin() - 1;
id = id * cut[w] + j;
}
cnt[id]++;
}
for (ll x : cnt) {
if (x != each) ans = 0;
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
//G
#include <bits/stdc++.h>
#define int long long
using namespace std;
int T,a,b,c,dis[2010][2010],inq[2010][2010],md;
queue<pair<int,int> > q;
void dijkstra(){
md=(1<<((int)floor(log2(b))+1));
for(int i=0;i<md;i++){
for(int j=0;j<b;j++){
dis[i][j]=2e18;
}
}
dis[a%md][a%b]=a;
q.push({a%md,a%b});
while(!q.empty()){
int x=q.front().first,y=q.front().second;
q.pop();
inq[x][y]=0;
if(dis[(x+b)%md][y]>dis[x][y]+b){
dis[(x+b)%md][y]=dis[x][y]+b;
if(!inq[(x+b)%md][y]){
q.push({(x+b)%md,y});
inq[(x+b)%md][y]=1;
}
}
if(dis[x^b][(y-x+(x^b)+b)%b]>(dis[x][y]^b)){
dis[x^b][(y-x+(x^b)+b)%b]=dis[x][y]^b;
if(!inq[x^b][(y-x+(x^b)+b)%b]){
q.push({x^b,(y-x+(x^b)+b)%b});
inq[x^b][(y-x+(x^b)+b)%b]=1;
}
}
}
for(int i=0;i<md;i++){
if(dis[i][c%b]<=c){
cout<<"YES"<<endl;
return ;
}
}
cout<<"NO"<<endl;
}
signed main(){
ios::sync_with_stdio(0);
cin>>T;
while(T--){
cin>>a>>b>>c;
dijkstra();
}
return 0;
}
/*
5
1 6 7
7 5 13
8 3 16
7 6 17
2 7 8
*/
//I
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
const int maxn = 5010;
int n, k, ans;
int fact[maxn], inv_fact[maxn], v[maxn];
int inv(int a)
{
int res = 1;
int b = mod - 2;
while (b)
{
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int C(int n, int m)
{
if (n < m) return 0;
return fact[n] * inv_fact[m] % mod * inv_fact[n - m] % mod;
}
signed main()
{
cin >> n >> k;
fact[0] = 1;
for (int i = 1; i <= maxn - 10; i++)
fact[i] = fact[i - 1] * i % mod;
for (int i = 0; i <= maxn - 10; i++)
inv_fact[i] = inv(fact[i]);
for (int i = 1; i <= n; i++)
v[i] = i;
int base_poss = inv_fact[n];
int poss = 1;
for (int i = 1; i <= k; i++)
{
int res = 1ll;
for (int j = 1; j < n; j++)
{
res = res * (1ll + v[j]) % mod;
v[j] = v[j] * j % mod;
}
if (i % 2 == 0)
res = (mod - res) % mod;
res = res * C(k, i) % mod;
poss = poss * base_poss % mod;
ans = (ans + res * poss) % mod;
}
cout << ans;
return 0;
}
//J
#include <iostream>
#include<vector>
#include<map>
using namespace std;
using ll = long long;
const int BIT = 32;
const ll mod = 998244353;
ll qpow(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1) res = res * a % mod;
b >>= 1;
a = a * a % mod;
}
return res;
}
void solve() {
int n, m;
cin >> n >> m;
vector<ll> row(n), col(m);
for (ll& x : row)
cin >> x;
for (ll& x : col)
cin >> x;
vector<map<ll, ll>> mp(BIT);
for (int j = 0; j + 1 < m; j++) {
if ((col[j] ^ col[j + 1]) != 1) continue;
for (int d = 2; d < BIT; d++) {
ll mask = 1LL << d;
mp[d][col[j] % mask]++;
}
}
ll ans = 0;
for (int i = 0; i + 1 < n; i++) {
ll delta = row[i] ^ row[i + 1];
for (int d = 2; d < BIT; d++) {
ll mask = 1LL << d;
if (delta != mask - 2) continue;
ll tar = (mask - 2) - (1LL << (d - 1));
tar ^= row[i];
tar %= mask;
ans += mp[d][tar];
}
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--)
solve();
return 0;
}
//K
#include<bits/stdc++.h>
using namespace std;
const int inf=1e9;
int T,n,a[500050],st0[500050][30],st1[500050][30],vis[1000010],mx[1000010];
int ed0[500050][30],ed1[500050][30],mxr[1000010];
stack<int> s;
int Ask(int l,int r,int id){
if(l>r){
return inf;
}
int k=log2(r-l+1),x=r-(1<<k)+1;
if(id){
return min(st0[l][k],st0[x][k]);
}
return min(st1[l][k],st1[x][k]);
}
int Askr(int l,int r,int id){
if(l>r){
return inf;
}
int k=log2(r-l+1),x=r-(1<<k)+1;
if(id){
if(st0[l][k]<st0[x][k]){
return ed0[l][k];
}
return ed0[x][k];
}
if(st1[l][k]<st1[x][k]){
return ed1[l][k];
}
return ed1[x][k];
}
void solve(){
cin>>n;
while(!s.empty()){
s.pop();
}
for(int i=1;i<=n;i++){
vis[i]=mx[i]=mxr[i]=0;
}
for(int j=0;j<25;j++){
for(int i=1;i+(1<<(j+1))-1<=n;i++){
st0[i][j]=st1[i][j]=ed0[i][j]=ed1[i][j]=0;
}
}
for(int i=1;i<=n;i++){
cin>>a[i];
if(i%2==1){
ed0[i][0]=i;
st0[i][0]=a[i];
st1[i][0]=inf;
continue;
}
ed1[i][0]=i;
st1[i][0]=a[i];
st0[i][0]=inf;
}
for(int j=0;j<25;j++){
for(int i=1;i+(1<<(j+1))-1<=n;i++){
if(st0[i][j]<st0[i+(1<<j)][j]){
st0[i][j+1]=st0[i][j];
ed0[i][j+1]=ed0[i][j];
}
else{
st0[i][j+1]=st0[i+(1<<j)][j];
ed0[i][j+1]=ed0[i+(1<<j)][j];
}
if(st1[i][j]<st1[i+(1<<j)][j]){
st1[i][j+1]=st1[i][j];
ed1[i][j+1]=ed1[i][j];
}
else{
st1[i][j+1]=st1[i+(1<<j)][j];
ed1[i][j+1]=ed1[i+(1<<j)][j];
}
}
}
//cout<<777<<endl;
for(int i=1;i<=n;i++){
// cout<<i<<endl;
if(s.empty()){
s.push(i);
vis[i]=0;
mx[i]=Ask(i,n,(i+1)%2);
mxr[i]=Askr(i,n,(i+1)%2);
//cout<<777<<endl;
continue;
}
int u=s.top();
//cout<<778<<endl;
mx[i]=Ask(i,mxr[u]-1,(i+1)%2);
if(mx[i]>=a[u]&&mx[u]==a[i]){
vis[i]=1;
//cout<<777<<endl;
s.pop();
continue;
}
// cout<<777<<endl;
vis[i]=0;
mxr[i]=Askr(i,mxr[u]-1,(i+1)%2);
s.push(i);
}
for(int i=1;i<=n;i++){
if(vis[i]){
cout<<")";
continue;
}
cout<<"(";
}
cout<<endl;
}
int main(){
ios::sync_with_stdio(0);
cin>>T;
while(T--){
solve();
}
return 0;
}
/*
5
6
4 1 5 4 1 1
4
1 2 3 2
4
1 3 1 2
2
2 1
8
8 5 2 6 1 4 3 7
*/
//M
#include<bits/stdc++.h>
using namespace std;
int T;
string s;
string oplus(string a, string b)
{
int alen = a.size(), blen = b.size();
// cout <<"A:" << a << ' ' <<"B:" << b << '\n';
int i;
for (i = 0; i < alen && a[i] == '0'; i++);
if (i == alen)
a = "0";
else
a = a.substr(i);
for (i = 0; i < blen && b[i] == '0'; i++);
if (i == blen)
b = "0";
else b = b.substr(i);
alen = a.size(), blen = b.size();
string ans = "";
for (int j = 0; j < min(alen, blen); j++)
ans += (a[alen - j - 1] == b[blen - j - 1] ? '0' : '1');
if (alen > blen)
{
for (int j = alen - blen - 1; j >= 0; j--)
ans += a[j];
}
else if (alen < blen)
{
for (int j = blen - alen - 1; j >= 0; j--)
ans += b[j];
}
reverse(ans.begin(), ans.end());
int len = ans.size();
for (i = 0; i < len && ans[i] == '0'; i++);
if (i == len)
return "0";
return ans.substr(i);
}
string maxx(string a, string b)
{
int alen = a.size(), blen = b.size();
if (alen != blen)
return alen > blen ? a : b ;
for (int i = 0; i < alen; i++)
{
if (a[i] > b[i])
return a;
else if (a[i] < b[i])
return b;
}
return a;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> T;
while (T--)
{
cin >> s;
int n = s.size();
string cuta, cutb;
if (n == 3)
{
cuta = s[0], cutb = s[n - 1];
cout << oplus(cuta, cutb) << '\n';
continue;
}
cuta = s[0], cutb = s.substr(2);
string ans1 = oplus(cuta, cutb);
cuta += s[1];
cutb = s.substr(3);
string ans2 = oplus(cuta, cutb);
cuta = s.substr(0, n - 2);
cutb = s[n - 1];
string ans3 = oplus(cuta, cutb);
cout << maxx(ans1, maxx(ans2, ans3)) << '\n';
}
return 0;
}