**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Billetes MX (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

Input: The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output: For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Examples:
input:
3
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
output:
1
0
489373567
651321892
769740174
489373567

Note: In the first test case of the example, we initially have the following grid:

$0$| $6$| $10$   ---|---|---   $6$| $0$| $12$   $10$| $12$| $?$      It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the answer is $0$.

Buggy Code:
#include <cstdio>
#include <map>
#include <iostream>
#include <algorithm>
#include <bitset>
#include <queue>
#include <stack>
#include <vector>
#include <random>
#include <cstring>
#include <ctime>
#include <cmath>
#include <assert.h>
#include <unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define LL long long
#define pp pair<LL,LL>
#define mp make_pair
#define ull unsigned long long
namespace IO{
	const int sz=1<<22;
	char a[sz+5],b[sz+5],*p1=a,*p2=a,*t=b,p[105];
	inline char gc(){
	//	return p1==p2?(p2=(p1=a)+fread(a,1,sz,stdin),p1==p2?EOF:*p1++):*p1++;
		return getchar();
	}
	template<class T> void gi(T& x){
		x=0; int f=1;char c=gc();
		if(c=='-')f=-1;
		for(;c<'0'||c>'9';c=gc())if(c=='-')f=-1;
		for(;c>='0'&&c<='9';c=gc())
			x=x*10+(c-'0');
		x=x*f;
	}
	inline void flush(){fwrite(b,1,t-b,stdout),t=b; }
	inline void pc(char x){*t++=x; if(t-b==sz) flush(); }
	template<class T> void pi(T x,char c='\n'){
		if(x<0)pc('-'),x=-x;
		if(x==0) pc('0'); int t=0;
		for(;x;x/=10) p[++t]=x%10+'0';
		for(;t;--t) pc(p[t]); pc(c);
	}
	struct F{~F(){flush();}}f;
}
using IO::gi;
using IO::pi;
using IO::pc;
const int mod=1e9+7;
inline int add(int x,int y){
	return x+y>=mod?x+y-mod:x+y;
}
inline int dec(int x,int y){
	return x-y<0?x-y+mod:x-y;
}
inline int mul(int x,int y){
	return 1ll*x*y%mod;
}
inline int qkpow(int a,int b){
	if(b<0)return 0;
	int ans=1,base=a%mod;
	while(b){
		if(b&1)ans=1ll*ans*base%mod;
		base=1ll*base*base%mod;
		b>>=1;
	}
	return ans;
}
int fac[1000005],inv[1000005],Invn[600005];
inline int binom(int n,int m){
	if(n<m||m<0)return 0;
	return 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod;
}
void init_C(int n){
	fac[0]=1;
	for(int i=1;i<=n;i++)fac[i]=1ll*fac[i-1]*i%mod;
	inv[0]=1;
	inv[n]=qkpow(fac[n],mod-2);
	for(int i=n-1;i>=1;i--)inv[i]=1ll*inv[i+1]*(i+1)%mod;
	Invn[0]=Invn[1]=1;
	for(int i=1;i<=200000;i++)Invn[i]=(LL)(mod-mod/i)*Invn[mod%i]%mod;
}
int t,n,m,k,q,cnt,fa[200005],c[200005],fl,sz[200005];
vector<int>G[200005];
inline int findSet(int u){
	return fa[u]==u?u:fa[u]=findSet(fa[u]);
}
inline void merge(int u,int v,int w){
	if(!fl)return ;
	int x=findSet(u),y=findSet(v);
	if(x==y){
		if((c[u]^w)!=c[v]){
			fl=0;
			return ;
		}
	}else{
		if(sz[x]<sz[y])swap(x,y),swap(u,v);
		cnt--;
		int val=c[u]^c[v]^w;
		while(!G[y].empty()){
			int p=G[y].back();
			G[y].pop_back();
			G[x].push_back(p);
			c[p]^=val;
		}
		fa[y]=x;
	}
}
inline void solve(){
	gi(n),gi(m),gi(k),gi(q);
	for(int i=1;i<=n+m;i++)fa[i]=i,c[i]=0,sz[i]=1,G[i].push_back(i);
	fl=1;cnt=n+m;
	for(int i=1;i<=k;i++){
		int x,y,v;gi(x),gi(y),gi(v);y+=n;
		merge(x,y,v);
	}
	pi(fl?qkpow(1<<30,cnt-1):0);
	while(q--){
		int x,y,v;gi(x),gi(y),gi(v);y+=n;
		merge(x,y,v);
		pi(fl?qkpow(1<<30,cnt-1):0);
	}
}
signed main(){
	srand(time(0));
	gi(t);
	while(t--)solve();
	return 0;
}
/*
*/

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
```plaintext
1
3 3 2 2
1 1 1
2 2 1
3 3 0
1 3 1
```


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Billetes MX (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

Input: The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output: For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Examples:
input:
3
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
output:
1
0
489373567
651321892
769740174
489373567

Note: In the first test case of the example, we initially have the following grid:

$0$| $6$| $10$   ---|---|---   $6$| $0$| $12$   $10$| $12$| $?$      It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the answer is $0$.

Buggy Code:
import sys
from collections import deque
input = sys.stdin.readline
def fg():
    return int(input())
def fgh():
    return [int(xx) for xx in input().split()]
def tt():
    sys.stdout.flush()
def sd():
    print('Yes')
def df():
    print('No')
M = 10 ** 9 + 7
for __ in range(fg()):
    n, m, k, q = fgh()
    u = 2 ** 30
    step = [1]
    for i in range(n + m):
        step.append(step[-1] * u % M)
    ks = [[i] for i in range(n + m)]
    z = [i for i in range(n + m)]
    d = [0] * (n + m)
    vvv = [0] * (n + m)
    T = [0] * (n + m)
    bd = n + m - 1
    if k == 0:
        print(step[bd])
    do = {}
    for _ in range(k + q):
        x, y, h = fgh()
        x -= 1
        y -= 1
        if bd == -1 and _ >= k - 1:
            print(0)
            continue
        if x + y == 0:
            if (0, 0) not in do:
                bd -= 1
            else:
                if bd != do[(0, 0)]:
                    bd = -1
        else:
            u = x
            v = y + n
            if x == 0:
                #print(x, y)
                #print(*vvv)
                #print(vvv[z[v]])
                #print(*z)
                #print(v)
                if vvv[z[v]] == 0:
                    bd -= 1
                    st = d[v] ^ h
                    for uu in ks[z[v]]:
                        d[uu] ^= st
                        z[uu] = v
                    vvv[v] = 1
                else:
                    if d[v] != h:
                        #print(222)
                        bd = -1
            elif y == 0:
                if vvv[z[u]] == 0:
                    bd -= 1
                    st = d[u] ^ h
                    for uu in ks[z[u]]:
                        d[uu] ^= st
                        z[uu] = u
                    vvv[u] = 1
                else:
                    if d[u] != h:
                        #print(111)
                        bd = -1
            else:
                if (z[u] == z[v]) and ((d[u] ^ d[v]) != h):
                    #print(444)
                    bd = -1
                elif z[u] != z[v]:
                    if len(ks[z[u]]) > len(ks[z[v]]):
                        u, v = v, u
                    if vvv[z[u]] + vvv[z[v]] <= 1:
                        bd -= 1
                    p = d[v] ^ h ^ d[u]
                    if vvv[z[u]] and vvv[z[v]] and p:
                        bd = -1
                        #print(333)
                        continue
                    if vvv[z[u]] and vvv[z[v]]:
                        if (1, 1) not in do:
                            do[(1, 1)] = d[v] ^ d[u] ^ h
                        elif (1, 1) in do and do[(1, 1)]:
                            bd = -1
                    if vvv[u] == 1 and vvv[v] == 0:
                        u, v = v, u
                    oo = z[u]
                    for j in ks[z[u]]:
                        z[j] = z[v]
                        d[j] ^= p
                        ks[z[v]].append(j)
                    vvv[z[v]] = max(vvv[oo], vvv[z[v]])
        #print(*z)
        #print(bd)
        if _ >= k - 1:
            if bd >= 0:
                print(step[bd])
            else:
                print(0)

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
```plaintext
1
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
```


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Billetes MX (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

Input: The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output: For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Examples:
input:
3
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
output:
1
0
489373567
651321892
769740174
489373567

Note: In the first test case of the example, we initially have the following grid:

$0$| $6$| $10$   ---|---|---   $6$| $0$| $12$   $10$| $12$| $?$      It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the answer is $0$.

Buggy Code:
import sys
from collections import deque
input = sys.stdin.readline
def fg():
    return int(input())
def fgh():
    return [int(xx) for xx in input().split()]
def tt():
    sys.stdout.flush()
def sd():
    print('Yes')
def df():
    print('No')
M = 10 ** 9 + 7
for __ in range(fg()):
    n, m, k, q = fgh()
    u = 2 ** 30
    step = [1]
    for i in range(n + m):
        step.append(step[-1] * u % M)
    ks = [[i] for i in range(n + m)]
    z = [i for i in range(n + m)]
    d = [0] * (n + m)
    vvv = [0] * (n + m)
    T = [0] * (n + m)
    bd = n + m - 1
    if k == 0:
        print(step[bd])
    for _ in range(k + q):
        x, y, h = fgh()
        x -= 1
        y -= 1
        if bd == -1 and _ >= k - 1:
            print(0)
            continue
        if x + y == 0:
            bd -= 1
        else:
            u = x
            v = y + n
            if x == 0:
                #print(x, y)
                #print(*vvv)
                #print(vvv[z[v]])
                #print(*z)
                #print(v)
                if vvv[z[v]] == 0:
                    bd -= 1
                    st = d[v] ^ h
                    for uu in ks[z[v]]:
                        d[uu] ^= st
                        z[uu] = v
                    vvv[v] = 1
                else:
                    if d[v] != h:
                        #print(222)
                        bd = -1
            elif y == 0:
                if vvv[z[u]] == 0:
                    bd -= 1
                    st = d[u] ^ h
                    for uu in ks[z[u]]:
                        d[uu] ^= st
                        z[uu] = u
                    vvv[u] = 1
                else:
                    if d[u] != h:
                        #print(111)
                        bd = -1
            else:
                if (z[u] == z[v]) and ((d[u] ^ d[v]) != h):
                    #print(444)
                    bd = -1
                elif z[u] != z[v]:
                    if len(ks[z[u]]) > len(ks[z[v]]):
                        u, v = v, u
                    if vvv[z[u]] + vvv[z[v]] <= 1:
                        bd -= 1
                    p = d[v] ^ h ^ d[u]
                    if vvv[z[u]] and vvv[z[v]] and p:
                        bd = -1
                        #print(333)
                        continue
                    if vvv[u] == 1 and vvv[v] == 0:
                        u, v = v, u
                    oo = z[u]
                    for j in ks[z[u]]:
                        z[j] = z[v]
                        d[j] ^= p
                        ks[z[v]].append(j)
                    vvv[z[v]] = max(vvv[oo], vvv[z[v]])
        #print(*z)
        #print(bd)
        if _ >= k - 1:
            if bd >= 0:
                print(step[bd])
            else:
                print(0)

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
```plaintext
1
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1 0
2 2 1
```


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Billetes MX (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

Input: The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output: For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Examples:
input:
3
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
output:
1
0
489373567
651321892
769740174
489373567

Note: In the first test case of the example, we initially have the following grid:

$0$| $6$| $10$   ---|---|---   $6$| $0$| $12$   $10$| $12$| $?$      It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the answer is $0$.

Buggy Code:
import sys
from collections import deque
input = sys.stdin.readline
def fg():
    return int(input())
def fgh():
    return [int(xx) for xx in input().split()]
def tt():
    sys.stdout.flush()
def sd():
    print('Yes')
def df():
    print('No')
M = 10 ** 9 + 7
for __ in range(fg()):
    n, m, k, q = fgh()
    u = 2 ** 30
    step = [1]
    for i in range(n + m):
        step.append(step[-1] * u % M)
    ks = [[i] for i in range(n + m)]
    z = [i for i in range(n + m)]
    d = [0] * (n + m)
    vv = [0] * (n + m)
    vvv = [0] * (n + m)
    T = [0] * (n + m)
    bd = n + m - 1
    if k == 0:
        print(step[bd])
    for _ in range(k + q):
        x, y, h = fgh()
        x -= 1
        y -= 1
        if bd == -1 and _ >= k - 1:
            print(0)
            continue
        if x + y == 0:
            bd -= 1
        else:
            u = x
            v = y + n
            if x == 0:
                if vvv[z[v]] == 0:
                    bd -= 1
                    st = d[v] ^ h
                    for uu in ks[z[v]]:
                        d[uu] ^= st
                    z[v] = v
                    vvv[v] = 1
                else:
                    if d[v] != h:
                        bd = -1
            elif y == 0:
                if vvv[z[u]] == 0:
                    bd -= 1
                    st = d[u] ^ h
                    for uu in ks[z[u]]:
                        d[uu] ^= st
                    z[u] = u
                    vvv[u] = 1
                else:
                    if d[u] != h:
                        bd = -1
            else:
                vv[u] = 1
                vv[v] = 1
                if z[u] == z[v] and d[u] ^ d[v] != h:
                    bd = -1
                if z[u] != z[v]:
                    if len(ks[z[u]]) > len(ks[z[v]]):
                        u, v = v, u
                    if vvv[u] + vvv[v] == 0:
                        bd -= 1
                    p = d[v] ^ h ^ d[u]
                    if vvv[u] == 1 and vvv[v] == 0:
                        u, v = v, u
                    vvv[z[v]] = max(vvv[z[u]], vvv[z[v]])
                    for j in ks[z[u]]:
                        z[j] = z[v]
                        d[j] ^= p
                        ks[z[v]].append(j)
        if _ >= k - 1:
            if bd >= 0:
                print(step[bd])
            else:
                print(0)

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
```plaintext
1
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
```

### Explanation:
1. **Test Case 1:**
   - Grid size: 3x3
   - Initial fixed cells: 8
   - Updates: 1

   The initial grid is:
   ```
   0  6  10
   6  0  12
   10 12 ?
   ```
   - The only valid value for the cell (3, 3) is 0, so the first answer is 1.
   - After the update, the grid becomes:
   ```
   0  6  10
   6  0  12
   10 12 1
   ```
   - The grid does not satisfy the condition, so the answer is 0.

2. **Test Case 2:**
   - Grid size: 2x5
   - Initial fixed cells: 2
   - Updates: 0

   The initial grid is:
   ```
   10 30 ? ? ?
   ? ? ? ? ?
   ```
   - The number of ways to fill the grid is 489373567.

3. **Test Case 3:**
   - Grid size: 2x5
   - Initial fixed cells: 0
   - Updates: 2

   The initial grid is:
   ```
   ? ? ? ? ?
   ? ? ? ? ?
   ```
   - After the first update, the grid becomes:
   ```
   10 ? ? ? ?
   ? ? ? ? ?
   ```
   - After the second update, the grid becomes:
   ```
   10 30 ? ? ?
   ? ? ? ? ?
   ```
   - The number of ways to fill the grid is 651321892.

This test input is designed to cover various edge cases and scenarios to maximize the failure probability in faulty solutions.


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Billetes MX (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

Input: The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output: For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Examples:
input:
3
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
output:
1
0
489373567
651321892
769740174
489373567

Note: In the first test case of the example, we initially have the following grid:

$0$| $6$| $10$   ---|---|---   $6$| $0$| $12$   $10$| $12$| $?$      It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the answer is $0$.

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

#define LL long long
#define mod 998244353ll
#define PI acos(-1.0)
#define set0(ar) memset(ar, 0, sizeof ar)
#define setinf(ar) memset(ar, 126, sizeof ar)
#define DBG cerr << "line[" << __LINE__ << "]: "

inline constexpr LL bigmod(LL p, LL e, LL M) {
  LL r = 1;
  for (; e > 0; e >>= 1, p = (p * p) % M)
    if (e & 1) r = (r * p) % M;
  return r;
}
inline constexpr LL modinverse(LL a, LL M) { return bigmod(a, M - 2, M); }

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int pre[31][100001];
int Find(int b, int x) {
  if(pre[b][x] == x) return pre[b][x];
  return pre[b][x] = Find(b, pre[b][x]);
}
int zero[31][100001], one[31][100001];
set<int> S, Scol[31];
int p2[200001];

bool update(int x, int y, int v) {
  if(x != 1) S.erase(x);
  for(int i = 0; i < 30; i++) {
    if(x == 1) Scol[i].erase(y);
    int py = Find(i, y);

    if(v & 1<<i) {
      if(zero[i][x] != -1) {
        if(Find(i, zero[i][x]) == py) return false;
      }

      if(one[i][x] != -1) {
        int px = Find(i, one[i][x]);
        if(px != py) {
          pre[i][py] = px;
          Scol[i].erase(py);
        }
      } else one[i][x] = y;

    } else {
      if(one[i][x] != -1) {
        if(Find(i, one[i][x]) == py) return false;
      }

      if(zero[i][x] != -1) {
        int px = Find(i, zero[i][x]);
        if(px != py) {
          pre[i][py] = px;
          Scol[i].erase(py);
        }
      } else zero[i][x] = y;

    }
  }
  return true;
}

int query() {
  LL cnt = S.size();
  LL res = 1;
  for(int i = 0; i < 30; i++) {
    res = (res * p2[cnt + Scol[i].size()]) % 1000000007;
  }
  return res;
}

pair<int, pair<int,int>> queries[310001];

void solve() {
  int n, m, k, q; cin >> n >> m >> k >> q;
  for(int i = 0; i < 30; i++) Scol[i].clear();
  for(int i = 1; i <= m; i++) {
    for(int j = 0; j < 30; j++) {
      pre[j][i] = i;
      zero[j][i] = one[j][i] = -1;
      Scol[j].insert(i);
    }
  }
  S.clear();
  for(int i = 2; i <= n; i++) S.insert(i);
  int cc = 0;
  for(int i = 0; i < k; i++) {
    int x, y, v; cin >> x >> y >> v;
    queries[cc++] = {v, {x, y}};
  }
  queries[cc++] = {-1, {0, 0}};
  for(int i = 0; i < q; i++) {
    int x, y, v; cin >> x >> y >> v;
    queries[cc++] = {v, {x, y}};
    queries[cc++] = {-1, {0, 0}};
  }

  bool bad = false;
  for(int i = 0; i < cc; i++) {
    auto [v, pp] = queries[i];
    auto [x, y] = pp;
    if(v != -1) {
      if(bad) continue;
      if(!update(x, y, v)) bad = true;
    }else {
      if(bad) cout << "0\n";
      else cout << query() << "\n";
    }
  }
}

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

  p2[0] = 1;
  for(int i = 1; i <= 200000; i++) p2[i] = (p2[i-1] * 2ll) % 1000000007;

  int T = 1;
  cin >> T;
  for (int ts = 1; ts <= T; ts++) {
    //cout << "Case #" << ts << ": ";
    solve();
  }
}

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
```plaintext
3
3 3 1 2
1 1 1
2 2 2
3 3 3
```


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Billetes MX (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

Input: The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output: For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Examples:
input:
3
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
output:
1
0
489373567
651321892
769740174
489373567

Note: In the first test case of the example, we initially have the following grid:

$0$| $6$| $10$   ---|---|---   $6$| $0$| $12$   $10$| $12$| $?$      It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the answer is $0$.

Buggy Code:
/**
 *    author: sunkuangzheng
 *    created: 14.10.2024 09:08:36
**/
#include<bits/stdc++.h>
#ifdef DEBUG_LOCAL
#include <mydebug/debug.h>
#endif
using ll = long long;
const int N = 2e5+5,mod = 1e9 + 7,inv = (1e9 + 8) / 2;
using namespace std;
int T,n,m,k,q,x,y,z,fg[30][2],ct[N],nm[N]; vector<pair<int,int>> g[N];
struct dsu{
    int fa[N],as,fg,dt[N];
    void init(){
        as = 1,fg = 0;
        for(int i = 1;i <= 2 * (n + m);i ++) fa[i] = i,dt[i] = 1;
        for(int i = 2;i <= n + m;i ++) if(i != m + 1) as = 2ll * as % mod;
    }int fd(int x){return x == fa[x] ? x : fa[x] = fd(fa[x]);}
    void mg(int u,int v) {u = fd(u),v = fd(v),fa[v] = u,dt[u] &= dt[v];}
    void mg(int u,int v,int w){
        int n = ::n + ::m;
        if(dt[fd(u)] && dt[fd(u + n)]) as = 1ll * as * inv % mod;
        if(fd(u) != fd(v) && fd(u) != fd(v + n) && dt[fd(v)] && dt[fd(v + n)])
            as = 1ll * as * inv % mod;
        if(w) mg(u,v + n),mg(v,u + n);
        else mg(u,v),mg(u + n,v + n);
        if(fd(u) == fd(u + n) || fd(v) == fd(v + n)) fg = 1;
        if(dt[fd(u)] && dt[fd(u + n)]) as = 2ll * as % mod;
        if(fd(u) != fd(v) && fd(u) != fd(v + n) && dt[fd(v)] && dt[fd(v + n)])
            as = 2ll * as % mod;
    }void tt(int x,int y){
        int px = x; x += (!y) * (n + m);
        if(dt[fd(x)]) as = 1ll * inv * as % mod;
        dt[fd(x)] = 0;
        if(!dt[fd(px)] && !dt[fd(px + n + m)]) fg = 1;
    }
}d[30][2];
int calc(int x,int y,int z){
    int res = 1;
    for(int i = 0;i < 30;i ++){
        int sm = 0;
        for(int j = 0;j < 2;j ++){
            z = (z >> i) & 1;
            if(x == 1 && y == 1) {if(z != j) {fg[i][j] = 1;break;}}
            else if(y == 1) d[i][j].tt(m + x,z);
            else if(x == 1) d[i][j].tt(y,z);
            else d[i][j].mg(x + m,y,j ^ z);
            if(fg[i][j] || d[i][j].fg) continue;
            sm = (sm + d[i][j].as) % mod;
        }res = 1ll * res * sm % mod;
    }return res;
}void los(){
    cin >> n >> m >> k >> q;  int as = 1;
    for(int i = 0;i < 30;i ++) for(int j = 0;j < 2;j ++) d[i][j].init(),fg[i][j] = 0;
    for(int i = 1;i < n + m;i ++) as = 1ll * as * (1ll << 30) % mod;
    for(int i = 1;i <= k;i ++) cin >> x >> y >> z,as = calc(x,y,z);
    cout << as << "\n";
    for(int i = 1;i <= q;i ++) cin >> x >> y >> z,as = calc(x,y,z),cout << as << "\n";
}int main(){
   ios::sync_with_stdio(0),cin.tie(0);
    for(cin >> T;T --;) los();
}

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
To generate a challenging test input that exposes the bug in the provided code, we need to focus on edge cases and scenarios that can maximize the failure probability. Here are the steps to think through the problem:

1. **Understand the Bug**: The provided code uses a Disjoint Set Union (DSU) structure to manage the constraints of the grid. The bug might be in how it handles the XOR constraints or in the way it updates the grid.

2. **Identify Edge Cases**:
   - **Empty Grid**: A grid with no initial fixed cells.
   - **Single Fixed Cell**: A grid with only one fixed cell.
   - **Full Grid**: A grid where all cells are initially fixed.
   - **Conflicting Updates**: Updates that conflict with the initial fixed cells or with each other.
   - **Large Grid**: A grid with a large number of rows and columns but a small number of fixed cells.

3. **Generate the Test Input**:
   - **Small-scale Test Input**: We will create a small grid to ensure the test input is valid and complete.
   - **Include Conflicting Updates**: To maximize the failure probability, we will include updates that conflict with the initial fixed cells or with each other.

Here is a small-scale test input that includes a grid with a few fixed cells and conflicting updates:

```plaintext
1
3 3 2 3
1 1 1
2 2 2
1 2 3
2 3 4
3 3 5
```

**Explanation**:
- **Test Case**: 1
- **Grid Size**: 3x3
- **Initial Fixed Cells**: 2
  - (1,1) = 1
  - (2,2) = 2
- **Updates**: 3
  - (1,2) = 3
  - (2,3) = 4
  - (3,3) = 5

This test input is designed to:
- Ensure the grid is small enough to be manageable.
- Include initial fixed cells that set a baseline for the XOR constraints.
- Include updates that conflict with the initial fixed cells to test how the code handles such conflicts.

This should help expose any issues in the code's handling of the XOR constraints and updates.


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Billetes MX (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

Input: The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output: For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Examples:
input:
3
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
output:
1
0
489373567
651321892
769740174
489373567

Note: In the first test case of the example, we initially have the following grid:

$0$| $6$| $10$   ---|---|---   $6$| $0$| $12$   $10$| $12$| $?$      It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the answer is $0$.

Buggy Code:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e5+5,M=2e5+5;
const ll mod=1e9+7;
int qx[M],qy[M],qw[M],fa[N],d[N],tp[N],tc[N],n,m,q,K,op[N];
int num,all;
ll ans[M],rec,pw[N];
int find(int x){
	if(x==fa[x])return x;
	int f=find(fa[x]);
	d[x]^=d[fa[x]];return fa[x]=f;
}
void merge(int x,int v,int w){
	all-=op[find(x)]+op[find(v)];
	op[find(v)]&=op[find(x)];
	all+=op[find(v)];
	fa[x]=v;d[x]=w;
}
void add(int x,int y,int w){
	if(!tp[y]){
		tp[y]=x;tc[y]=w;
		if(d[x]==-1)d[x]=0;
		if(y>1)num--;
		else all-=op[find(x)],op[find(x)]=0;
		return;
	}int v=tp[y],c=tc[y];
	// cout<<x<<' '<<v<<' '<<w<<' '<<c<<"shabi\n";
	if(y==1)all-=op[find(x)],op[find(x)]=0;
	if(d[x]==-1){merge(x,v,c^w);return;}
	if(c==w){
		if(find(v)==find(x)){
			if(d[v]^d[x])rec=0;//,cout<<v<<' '<<x<<' '<<d[v]<<' '<<d[x]<<"shabi\n";
			return;
		}merge(x,v,d[x]);
	}else{
		if(find(v)==find(x)){
			if(d[v]==d[x])rec=0;//,cout<<v<<' '<<x<<' '<<d[v]<<' '<<d[x]<<"uozhi\n";
			return;
		}merge(x,v,d[x]^1);
	}
}
void solve(int p){
	// cout<<p<<"----------------\n";
	for(int i=1;i<=m;i++)tp[i]=tc[i]=0;
	for(int i=1;i<=n;i++)fa[i]=i,d[i]=-1,op[i]=1;
	num=m-1;all=n;rec=1;
	ans[0]=ans[0]*pw[num]%mod*pw[all]%mod;
	for(int i=1;i<=q;i++){
		// puts("---------------------");
		// cout<<qx[i]<<' '<<qy[i]<<' '<<(qw[i]>>p&1)<<"add\n";
		add(qx[i],qy[i],qw[i]>>p&1);
		// for(int i=1;i<=n;i++)cout<<(d[i]==-1?-1:find(i),d[i])<<' ';cout<<"d\n";
		// for(int i=1;i<=n;i++)
		// 	if(d[find(i)]>0){puts("shabi");exit(0);}
		// cout<<num<<' '<<all<<' '<<rec<<"???\n";
		if(all<0){puts("shabi");exit(0);}
		ans[i]=ans[i]*rec%mod*pw[num]%mod*pw[all]%mod;
		// if(i==q){
		// 	cout<<rec<<' '<<num<<' '<<all<<"???\n";
		// }
		// cout<<qx[i]<<' '<<i<<' '<<all<<"|";
		// for(int i=1;i<=n;i++)cout<<op[find(i)]<<' ';cout<<"|";
		// for(int i=1;i<=n;i++)cout<<find(i)<<' ';cout<<"\n";
		// if(!rec)exit(0);
	}//for(int i=1;i<=n;i++)cout<<op[find(i)]<<' ';cout<<"sb\n";
		// if(!ans[q])exit(0);
	// exit(0);
}
void solve(){
	scanf("%d%d%d%d",&n,&m,&K,&q);q+=K;
	for(int i=0;i<=q;i++)ans[i]=1;
	for(int i=1;i<=q;i++)scanf("%d%d%d",&qx[i],&qy[i],&qw[i]);
	for(int i=0;i<=29;i++)solve(i);
	for(int i=K;i<=q;i++)printf("%lld\n",ans[i]);
}
int main(){
	pw[0]=1;
	for(int i=1;i<N;i++)pw[i]=pw[i-1]*2%mod;
	int T;scanf("%d",&T);
	while(T--)solve();
}

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
```plaintext
1
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
```


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Billetes MX (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

Input: The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output: For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Examples:
input:
3
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
output:
1
0
489373567
651321892
769740174
489373567

Note: In the first test case of the example, we initially have the following grid:

$0$| $6$| $10$   ---|---|---   $6$| $0$| $12$   $10$| $12$| $?$      It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the answer is $0$.

Buggy Code:
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll M=1000000007;
int fa[100010],xr[100010];
ll pw[6000010];
int get(int x){
    if(x==fa[x])    return x;
    int ans=get(fa[x]);
    xr[x]^=xr[fa[x]];fa[x]=ans;
    return ans;
}
void solve(){
    int n,m,k,q;
    cin>>n>>m>>k>>q;
    vector<pair<int,int>> ct(n+1);
    for(int i=1;i<=n;i++)   {fa[i]=i;xr[i]=0;}
    int ltk=m;int num=n;
    bool fm=1;
    auto ope=[&](int r,int c,int val)->void {
        if(ct[r].first==0)    num--;
        else{
            auto [c1,v1]=ct[r];
            if(get(c)==get(c1)){
                if((v1^val)!=(xr[c]^xr[c1]))    fm=0;
            }
            else{
                // cerr<<r<<' '<<c<<'\n';
                ltk--;
                int hp=(xr[c]^xr[c1]^(v1^val));
                xr[get(c1)]=hp;
                fa[get(c1)]=get(c);
            }
        }
        ct[r]={c,val};
    };
    auto get=[&]()->void {
        if(!fm) cout<<"0\n";
        else cout<<pw[(ltk-1+num)*30]<<'\n';
    };
    for(int i=1;i<=k;i++){
        int r,c,w;
        cin>>r>>c>>w;
        ope(r,c,w);
    }
    get();
    for(int i=1;i<=q;i++){
        int r,c,w;
        cin>>r>>c>>w;
        ope(r,c,w);
        get();
    }

}
int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    int _ =1;
    cin>>_;
    pw[0]=1;
    for(int i=1;i<=6000000;i++)  pw[i]=pw[i-1]*2%M;

    while(_--)  solve();
    return 0;
}

/*
1
3 3 8 0
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
*/

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
```plaintext
1
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
```


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Billetes MX (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

Input: The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output: For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Examples:
input:
3
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
output:
1
0
489373567
651321892
769740174
489373567

Note: In the first test case of the example, we initially have the following grid:

$0$| $6$| $10$   ---|---|---   $6$| $0$| $12$   $10$| $12$| $?$      It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the answer is $0$.

Buggy Code:
from collections import defaultdict

class UnionFind:
    def __init__(self,N,label=None,f=None,weighted=False,e_weighted=None,rollback=False):
        self.N=N
        self.parents=[None]*self.N
        self.size=[1]*self.N
        self.roots={i for i in range(self.N)}
        self.label=label
        if self.label!=None:
            self.label=[x for x in label]
        self.f=f
        self.weighted=weighted
        if self.weighted:
            self.e_weighted=e_weighted
            self.weight=[self.e_weighted]*self.N
        self.rollback=rollback
        if self.rollback:
            self.operate_list=[]
            self.operate_set=[]

    def Find(self,x):
        stack=[]
        while self.parents[x]!=None:
            stack.append(x)
            x=self.parents[x]
        if not self.rollback:
            if self.weighted:
                w=self.e_weighted
                for y in stack[::-1]:
                    self.parents[y]=x
                    w+=self.weight[y]
                    self.weight[y]=w
            else:
                for y in stack[::-1]:
                    self.parents[y]=x
        return x

    def Union(self,x,y,w=None):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if self.rollback:
            self.operate_list.append([])
            self.operate_set.append([])
        if root_x==root_y:
            if self.weighted:
                if self.weight[y]-self.weight[x]==w:
                    return True
                else:
                    return False
        else:
            if self.size[root_x]<self.size[root_y]:
                x,y=y,x
                root_x,root_y=root_y,root_x
                if self.weighted:
                    w=-w
            if self.rollback:
                self.operate_list[-1].append((self.parents,root_y,self.parents[root_y]))
                self.operate_list[-1].append((self.size,root_x,self.size[root_x]))
                self.operate_set[-1].append(root_y)
                if self.label!=None:
                    self.operate_list[-1]((self.label,root_x,self.label[root_x]))
                if self.weighted:
                    self.operate_list[-1].append((self.weight,root_y,self.weight[root_y]))
            self.parents[root_y]=root_x
            self.size[root_x]+=self.size[root_y]
            self.roots.remove(root_y)
            if self.label!=None:
                self.label[root_x]=self.f(self.label[root_x],self.label[root_y])
            if self.weighted:
                self.weight[root_y]=w+self.weight[x]-self.weight[y]

    def Size(self,x):
        return self.size[self.Find(x)]

    def Same(self,x,y):
        return self.Find(x)==self.Find(y)

    def Label(self,x):
        return self.label[self.Find(x)]

    def Weight(self,x,y):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if root_x!=root_y:
            return None
        return self.weight[y]-self.weight[x]

    def Roots(self):
        return list(self.roots)

    def Linked_Components_Count(self):
        return len(self.roots)

    def Linked_Components(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return linked_components

    def Rollback(self):
        assert self.rollback
        if self.operate_list:
            for lst,x,v in self.operate_list.pop():
                lst[x]=v
            for x in self.operate_set.pop():
                self.roots.add(x)
            return True
        else:
            return False

    def __str__(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return "\n".join(f"{r}: {linked_components[r]}" for r in sorted(list(linked_components.keys())))

class xor:
    def __init__(self,x):
        self.x=x
    def __add__(self,other):
        return xor(self.x^other.x)
    def __sub__(self,other):
        return xor(self.x^other.x)
    def __eq__(self,other):
        if type(other)!=xor:
            return False
        return self.x==other.x
    def __neg__(self):
        return self

T=int(input())
for t in range(T):
    N,M,K,Q=map(int,input().split())
    UF=UnionFind(N+M,weighted=True,e_weighted=xor(0))
    C=N+M
    a=None
    bl=1
    mod=10**9+7
    query=[tuple(map(int,input().split())) for q in range(K+Q)]
    P=[1]
    p=1<<30
    for i in range(1,N+M+1):
        P.append(P[-1]*p%mod)
    if K==0:
        ans=bl*P[C-1]
        print(ans)
    for q in range(K+Q):
        r,c,v=query[q]
        r-=1;c-=1
        c+=M
        if a==None:
            a=v
            v=0
            UF.Union(r,c,xor(v))
            C-=1
        else:
            v^=a
            w=UF.Weight(r,c)
            if w==None:
                UF.Union(r,c,xor(v))
                C-=1
            else:
                if w!=xor(v):
                    bl=0
        if q>=K-1:
            ans=bl*P[C-1]
            print(ans)

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
To generate a challenging test input that exposes the bug in the provided buggy code, we need to focus on edge cases and scenarios that are likely to cause issues. Here are the steps to think through this:

1. **Small Scale Test Case**: Ensure the test case is small enough to be manageable and understandable.
2. **Edge Cases**: Consider edge cases such as:
   - No initial fixed cells (K = 0).
   - All cells initially filled (K = n * m).
   - Updates that create conflicts.
   - Updates that do not create conflicts.
3. **Modulo Operation**: Ensure the test case can handle large numbers and the modulo operation.
4. **Valid and Invalid States**: Create states that are both valid and invalid to test the correctness of the algorithm.

Given these considerations, let's construct a small-scale test input:

### Test Input
```plaintext
1
3 3 4 2
1 1 0
1 2 1
2 1 1
2 2 0
2 3 2
3 3 3
```

### Explanation
- **Test Case 1**:
  - **Grid Size**: 3 rows and 3 columns.
  - **Initial Fixed Cells (K)**: 4 cells.
  - **Updates (Q)**: 2 updates.

- **Initial Fixed Cells**:
  - Cell (1,1) is 0.
  - Cell (1,2) is 1.
  - Cell (2,1) is 1.
  - Cell (2,2) is 0.
  - This setup ensures that the initial grid is valid.

- **Updates**:
  - Update cell (2,3) to 2.
  - Update cell (3,3) to 3.
  - These updates will test the ability of the algorithm to handle new assignments and check for conflicts.

### Expected Output
The expected output for this test case should be:
```plaintext
1
0
0
```

### Reasoning
- **Initial State**: The initial grid is valid, so there is 1 way to assign the remaining cells.
- **After First Update**: After setting (2,3) to 2, the grid becomes invalid because the XOR condition is not satisfied.
- **After Second Update**: Setting (3,3) to 3 does not change the validity of the grid, which remains invalid.

This test input is designed to be small and manageable while still challenging the algorithm to handle edge cases and ensure correctness.


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Billetes MX (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

Input: The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output: For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Examples:
input:
3
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
output:
1
0
489373567
651321892
769740174
489373567

Note: In the first test case of the example, we initially have the following grid:

$0$| $6$| $10$   ---|---|---   $6$| $0$| $12$   $10$| $12$| $?$      It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the answer is $0$.

Buggy Code:
from collections import defaultdict

class UnionFind:
    def __init__(self,N,label=None,f=None,weighted=False,e_weighted=None,rollback=False):
        self.N=N
        self.parents=[None]*self.N
        self.size=[1]*self.N
        self.roots={i for i in range(self.N)}
        self.label=label
        if self.label!=None:
            self.label=[x for x in label]
        self.f=f
        self.weighted=weighted
        if self.weighted:
            self.e_weighted=e_weighted
            self.weight=[self.e_weighted]*self.N
        self.rollback=rollback
        if self.rollback:
            self.operate_list=[]
            self.operate_set=[]

    def Find(self,x):
        stack=[]
        while self.parents[x]!=None:
            stack.append(x)
            x=self.parents[x]
        if not self.rollback:
            if self.weighted:
                w=self.e_weighted
                for y in stack[::-1]:
                    self.parents[y]=x
                    w+=self.weight[y]
                    self.weight[y]=w
            else:
                for y in stack[::-1]:
                    self.parents[y]=x
        return x

    def Union(self,x,y,w=None):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if self.rollback:
            self.operate_list.append([])
            self.operate_set.append([])
        if root_x==root_y:
            if self.weighted:
                if self.weight[y]-self.weight[x]==w:
                    return True
                else:
                    return False
        else:
            if self.size[root_x]<self.size[root_y]:
                x,y=y,x
                root_x,root_y=root_y,root_x
                if self.weighted:
                    w=-w
            if self.rollback:
                self.operate_list[-1].append((self.parents,root_y,self.parents[root_y]))
                self.operate_list[-1].append((self.size,root_x,self.size[root_x]))
                self.operate_set[-1].append(root_y)
                if self.label!=None:
                    self.operate_list[-1]((self.label,root_x,self.label[root_x]))
                if self.weighted:
                    self.operate_list[-1].append((self.weight,root_y,self.weight[root_y]))
            self.parents[root_y]=root_x
            self.size[root_x]+=self.size[root_y]
            self.roots.remove(root_y)
            if self.label!=None:
                self.label[root_x]=self.f(self.label[root_x],self.label[root_y])
            if self.weighted:
                self.weight[root_y]=w+self.weight[x]-self.weight[y]

    def Size(self,x):
        return self.size[self.Find(x)]

    def Same(self,x,y):
        return self.Find(x)==self.Find(y)

    def Label(self,x):
        return self.label[self.Find(x)]

    def Weight(self,x,y):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if root_x!=root_y:
            return None
        return self.weight[y]-self.weight[x]

    def Roots(self):
        return list(self.roots)

    def Linked_Components_Count(self):
        return len(self.roots)

    def Linked_Components(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return linked_components

    def Rollback(self):
        assert self.rollback
        if self.operate_list:
            for lst,x,v in self.operate_list.pop():
                lst[x]=v
            for x in self.operate_set.pop():
                self.roots.add(x)
            return True
        else:
            return False

    def __str__(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return "\n".join(f"{r}: {linked_components[r]}" for r in sorted(list(linked_components.keys())))

class xor:
    def __init__(self,x):
        self.x=x
    def __add__(self,other):
        return xor(self.x^other.x)
    def __sub__(self,other):
        return xor(self.x^other.x)
    def __eq__(self,other):
        return self.x==other.x
    def __neg__(self):
        return self

T=int(input())
for t in range(T):
    N,M,K,Q=map(int,input().split())
    UF=UnionFind(N+M,weighted=True,e_weighted=xor(0))
    C=N+M
    a=None
    bl=1
    mod=10**9+7
    query=[tuple(map(int,input().split())) for q in range(K+Q)]
    if K==0:
        ans=bl*pow(1<<30,C-1,mod)
        print(ans)
    for q in range(K+Q):
        r,c,v=query[q]
        r-=1;c-=1
        c+=M
        if a==None:
            a=v
            v=0
            UF.Union(r,c,xor(v))
            C-=1
        else:
            v^=a
            if UF.Same(r,c):
                if UF.Weight(r,c)!=xor(v):
                    bl=0
            else:
                UF.Union(r,c,xor(v))
                C-=1
        if q>=K-1:
            ans=bl*pow(1<<30,C-1,mod)
            print(ans)

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
```plaintext
1
3 3 1 2
1 1 0
2 2 1
3 3 1
```


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Billetes MX (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

Input: The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output: For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Examples:
input:
3
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
output:
1
0
489373567
651321892
769740174
489373567

Note: In the first test case of the example, we initially have the following grid:

$0$| $6$| $10$   ---|---|---   $6$| $0$| $12$   $10$| $12$| $?$      It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the answer is $0$.

Buggy Code:
#include <bits/stdc++.h>
#define _rep(i, x, y) for(int i = x; i <= y; ++i)
#define _req(i, x, y) for(int i = x; i >= y; --i)
#define _rev(i, u) for(int i = head[u]; i; i = e[i].nxt)
#define pb push_back
#define fi first
#define se second
#define mst(f, i) memset(f, i, sizeof f)
using namespace std;
#ifdef ONLINE_JUDGE
#define debug(...) 0
#else
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#endif
typedef long long ll;
typedef pair<int, int> PII;
namespace fastio{
    #ifdef ONLINE_JUDGE
    char ibuf[1 << 20],*p1 = ibuf, *p2 = ibuf;
    #define get() p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++
    #else
    #define get() getchar()
    #endif
    template<typename T> inline void read(T &t){
        T x = 0, f = 1;
        char c = getchar();
        while(!isdigit(c)){
            if(c == '-') f = -f;
            c = getchar();
        }
        while(isdigit(c)) x = x * 10 + c - '0', c = getchar();
        t = x * f;
    }
    template<typename T, typename ... Args> inline void read(T &t, Args&... args){
        read(t);
        read(args...);
    }
    template<typename T> void write(T t){
        if(t < 0) putchar('-'), t = -t;
        if(t >= 10) write(t / 10);
        putchar(t % 10 + '0');
    }
    template<typename T, typename ... Args> void write(T t, Args... args){
        write(t), putchar(' '), write(args...);
    }
    template<typename T> void writeln(T t){
        write(t);
        puts("");
    }
    template<typename T> void writes(T t){
        write(t), putchar(' ');
    }
    #undef get
};
using namespace fastio;
#define multitest() int T; read(T); _rep(tCase, 1, T)
namespace Calculation{
    const ll mod = 1e9 + 7;
    ll ksm(ll p, ll h){ll base = p % mod, res = 1; while(h){if(h & 1ll) res = res * base % mod; base = base * base % mod, h >>= 1ll;} return res;}
    void dec(ll &x, ll y){x = ((x - y) % mod + mod) % mod;}
    void add(ll &x, ll y){x = (x + y) % mod;}
    void mul(ll &x, ll y){x = x * y % mod;}
    ll sub(ll x, ll y){return ((x - y) % mod + mod) % mod;}
    ll pls(ll x, ll y){return ((x + y) % mod + mod) % mod;}
    ll mult(ll x, ll y){return x * y % mod;}
}
using namespace Calculation;
const int N = 2e5 + 5, M = 32, _ = 2e5;
int n, m, q, k;
ll pw[N];
struct node{
    int x, y, v;
}a[N];
vector<int> v[M][N][2];
int fa[M][N], cntr[M], cntc[M];
int find(int x, int t){return fa[t][x] == x ? x : fa[t][x] = find(fa[t][x], t);}
int merge(int x, int y, int t){
    int fx = find(x, t), fy = find(y, t);
    if(fx != fy) return fa[t][fx] = fy, 1;
    return 0;
}
ll ans[M];
ll work(){
    ll res = 1;
    _rep(i, 0, 29){
        if(!ans[i]) return 0;
        mul(res, pw[cntr[i] / 2 - 1 + cntc[i]]);
    }
    return res;
}
int main(){
    pw[0] = 1; _rep(i, 1, _) pw[i] = pw[i - 1] * 2 % mod;
    multitest(){
        read(n, m, k, q);
        _rep(i, 1, k) read(a[i].x, a[i].y, a[i].v);
        ll res = 1;
        _rep(i, 0, 29){
            ans[i] = 1, cntr[i] = 2 * n, cntc[i] = 0;
            _rep(j, 1, n * 2) fa[i][j] = j;
            _rep(j, 1, k) v[i][a[j].y][a[j].v >> i & 1].pb(a[j].x);
            _rep(j, 1, m){
                _rep(k, 0, 1){
                    int lst = 0;
                    for(auto &p : v[i][j][k]){
                        if(lst){
                            if(find(p, i) == find(lst + n, i)){ans[i] = 0; break;}
                            else cntr[i] -= merge(p, lst, i), cntr[i] -= merge(p + n, lst + n, i);
                        }
                        lst = p;
                    }
                }
                if(v[i][j][0].size() && v[i][j][1].size()){
                    int x = v[i][j][0][0], y = v[i][j][1][0];
                    if(find(x, i) == find(y, i)){ans[i] = 0; break;}
                    cntr[i] -= merge(x + n, y, i), cntr[i] -= merge(x, y + n, i);
                }
                if(v[i][j][0].empty() && v[i][j][1].empty()) cntc[i]++;
            }
        }
        writeln(work());
        while(q--){
            int x, y, val; read(x, y, val);
            _rep(i, 0, 29){
                int t = val >> i & 1;
                if(v[i][y][0].empty() && v[i][y][1].empty()) cntc[i]--;
                if(v[i][y][t].size()){
                    int u = v[i][y][t][0];
                    if(find(x, i) == find(u + n, i)){ans[i] = 0; break;}
                    if(find(x, i) != find(u, i)) cntr[i] -= merge(x, u, i), cntr[i] -= merge(x + n, u + n, i);
                }
                if(!ans[i]) break;
                if(v[i][y][t ^ 1].size()){
                    int u = v[i][y][t ^ 1][0];
                    if(find(x, i) == find(u, i)){ans[i] = 0; break;}
                    if(find(x + n, i) != find(y, i)) cntr[i] -= merge(x + n, u, i), cntr[i] -= merge(x, u + n, i);
                }
                if(!ans[i]) break;
                v[i][y][t].pb(x);
            }
            writeln(work());
        }
        _rep(i, 0, 29){
            ans[i] = 0;
            _rep(j, 1, m) v[i][j][0].clear(), v[i][j][1].clear();
            _rep(j, 1, 2 * n) fa[i][j] = 0;
        }
    }
    return 0;
}

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
To expose the bug in the provided code, we need to craft a test input that triggers the issue. The key points to consider are:

1. **Edge Cases**: Small values for \( n \), \( m \), \( k \), and \( q \) to make the input manageable.
2. **Special Conditions**: Ensure that the grid configuration and updates can lead to conflicts or edge conditions that the code might not handle correctly.
3. **XOR Condition**: Ensure that the initial grid and updates can violate the XOR condition for subgrids.

Given the constraints and the need to create a small-scale test, let's construct a test input that includes the following:
- A small grid with a few fixed cells.
- Updates that can potentially violate the XOR condition.
- Ensure the test input is valid and complete.

Here is the test input:

```plaintext
1
3 3 2 2
1 1 1
2 2 2
1 3 3
2 3 4
```

### Explanation:
- **Test Case**: 1
- **Grid Size**: 3x3
- **Initial Fixed Cells**:
  - \( (1, 1) \) with value 1
  - \( (2, 2) \) with value 2
- **Updates**:
  - Assign 3 to \( (1, 3) \)
  - Assign 4 to \( (2, 3) \)

This test input is designed to:
- Check the initial grid configuration.
- Ensure that the updates can potentially cause conflicts in the XOR condition.
- Verify the correctness of the code in handling updates and maintaining the XOR condition.

By using this test input, we can observe how the code handles the updates and whether it correctly computes the number of valid ways to assign integers to the unfilled cells.


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Billetes MX (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

Input: The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output: For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Examples:
input:
3
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
output:
1
0
489373567
651321892
769740174
489373567

Note: In the first test case of the example, we initially have the following grid:

$0$| $6$| $10$   ---|---|---   $6$| $0$| $12$   $10$| $12$| $?$      It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the answer is $0$.

Buggy Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <functional>
#include <stack>
#include <random>
#define all(arr) arr.begin(), arr.end()
#define ll long long
#define MS(arr, val) memset(arr, val, sizeof(arr))
#define vi vector<int>
#define pb emplace_back
#define PI pair<int, int>
#define PL pair<ll, ll>
#define lll __int128
#define ull unsigned long long
#define Kases int T; rd(T); rep(kase, 1, T)
#define rep(i, a, b) for(int i = (a), __ ## i = (b); i <= __ ## i; ++i)
#define per(i, b, a) for(int i = (b), __ ## i = (a); i >= __ ## i; --i)

using namespace std;

// static char buf[1000000],*p1=buf,*p2=buf,obuf[1000000],*p3=obuf;
// #define getchar() p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++
// #define putchar(x) (p3-obuf<1000000)?(*p3++=x):(fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=x)
inline void rd(char* x){scanf("%s", x);}
inline void rd(double &x){scanf("%lf", &x);}
// inline void rd(int &x){scanf("%d", &x);}
template<typename T> inline void rd(T &x){
    x = 0; bool is = 0; char ch = getchar();
    while(!isdigit(ch)) is |= (ch == '-'), ch = getchar();
    while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
    is && (x = -x);
}
// template<typename T>void write(T x){
//     x < 0 && (x = -x, putchar('-')); if(x >= 10) write(x / 10); putchar(x % 10 + '0');
// }
template<typename T, typename ...U> inline void rd(T &x, U &...y){rd(x), rd(y...);}

// std::random_device rnd;
// std::mt19937 rng(rnd());
// int gcd(int a, int b){return b == 0 ? a : gcd(b, a % b);}
const int maxn = 214514;
const ll mod = 1e9 + 7;
int par[maxn];
ll f[maxn], rf[maxn];
int fr(int u)
{
    if(par[u] == u) return u;
    int rt = fr(par[u]);
    f[u] ^= f[rt]; par[u] = rt;
    return rt;
}
ll pw[maxn];
int main()
{
    pw[0] = 1, pw[1] = (1 << 30) % mod;
    rep(i, 2, maxn - 1) pw[i] = (pw[i - 1] * pw[1]) % mod;
    Kases
    {
        int n, m, k, q; rd(n, m, k, q);
        ll can_11 = -1, can = 1;
        rep(i, 1, n + m) par[i] = i, f[i] = 0, rf[i] = -1;
        int cnt = (n - 1) + (m - 1);
        auto set_11 = [&](ll val)
        {
            if(can_11 != -1 && can_11 != val) can = 0;
            can_11 = val;
        };
        auto upd = [&](int x, int y, ll val)
        {
            if(can == 0) return;
            if(x == 1 && y == 1)
            {
                set_11(val);
                return;
            }
            val = (val << 1) | 1;
            if(x == 1 || y == 1)
            {
                int u = (x == 1 ? y : m + x);
                int rt = fr(u);
                if(rf[rt] == -1) --cnt, rf[rt] = (f[u] ^ val);
                else
                {
                    ll sum = rf[rt] ^ f[u] ^ val;
                    if(sum & 1) set_11(sum >> 1);
                    else can = 0;
                }
            }
            else
            {
                int u = y, v = m + x;
                int rtu = fr(u), rtv = fr(v);
                if(rf[rtv] != -1) swap(rtu, rtv), swap(u, v);
                if(rtu == rtv)
                {
                    auto sum = (f[u] ^ f[v] ^ val);
                    if(sum >> 1) can = 0; // 没得奇环
                    return;
                }
                if(rf[rtv] == -1) --cnt;
                if(rf[rtu] != -1 && rf[rtv] != -1)
                {
                    auto sum = rf[rtu] ^ rf[rtv] ^ f[u] ^ f[v] ^ val;
                    if(sum)
                    {
                        if(sum & 1) set_11(sum >> 1);
                        else can = 0;
                    }
                }
                else f[rtv] = (val ^ f[u] ^ f[v]); par[rtv] = rtu;
            }
        };

        rep(i, 1, k)
        {
            int x, y, v; rd(x, y, v); upd(x, y, v);
        }
        assert(cnt >= 0);
        printf("%lld\n", can == 0 ? 0 : pw[cnt + (can_11 == -1)]);
        while(q--)
        {
            int x, y, v; rd(x, y, v); upd(x, y, v);
            assert(cnt >= 0);
            printf("%lld\n", can == 0 ? 0 : pw[cnt + (can_11 == -1)]);
        }
    }
}

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
```plaintext
1
3 3 2 2
1 1 1
2 2 2
3 3 3
1 2 4
```


**Task:**
Generate a challenging test input that exposes the bug in the buggy code of the algorithm problem:
Algorithm Problem:
Title: Billetes MX (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.

An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:

  * All elements of the grid are integers between $0$ and $2^{30}-1$, and    * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).

There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.

However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.

For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.

Input: The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.

The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.

It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.

It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.

Output: For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.

Examples:
input:
3
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
output:
1
0
489373567
651321892
769740174
489373567

Note: In the first test case of the example, we initially have the following grid:

$0$| $6$| $10$   ---|---|---   $6$| $0$| $12$   $10$| $12$| $?$      It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the answer is $0$.

Buggy Code:
#include<bits/stdc++.h>
#include<map>
#include<queue>
#include<set>
#include<vector>
#define int long long
#define ull unsigned long long
#define endl '\n'
#define all(s) s.begin(),s.end()
#define xx first
#define yy second
#define inf 0x3f3f3f3f3f3f3f3f
#define N 200005
#define mod 1000000007//998244353
#define double long double
#define lowbit(x) ((x)&(-x))
//#define ll long long
#define ls(x) (x<<1)
#define rs(x) ((x<<1)|1)
#define pll pair<int,int>
#define __lcm(a,b) (a/__gcd(a,b)*b)
const double pi=acos(-1);
const int P=233333;
const int PP=1313131;
const int mod1=1000001011;
const int mod2=(1ll<<31)-1;
using namespace std;
vector<int> g[N];
//int num[5005][5005];
int vis[N],cnt[N],fa[N],f;
map<pll,int> ma;
vector<pair<pll,int> > v;
int pm[N];
    int T=1;
void init(int n)
{
	pm[0]=1;
	for(int i=1;i<=n;i++) pm[i]=pm[i-1]*2%mod;
}
int root(int x) {return fa[x]=(fa[x]==x?x:root(fa[x]));}
void dfs(int u,int n)
{
	vis[u]=1;
	for(auto i:g[u])
	{
		int a=min({u,i});
		int b=max({u,i});
		int w=ma[{a,b}];
		if(!vis[i])
		{
			fa[i]=root(u);
			cnt[i]=cnt[u]^w;
			dfs(i,n);
			if(!f) return ;
		}
		else
		{
			if((cnt[u]^w)!=cnt[i])
			{
				f=0;
				return ;
			}
		}
	}
}
void check(int n,int m,int k)
{
	f=1;
	for(int i=1;i<=n+m;i++) fa[i]=i,vis[i]=cnt[i]=0,g[i].clear();
	ma.clear();
	for(int i=1;i<=k;i++)
	{
		int x=v[i-1].xx.xx;
		int y=v[i-1].xx.yy;
		int z=v[i-1].yy;
		ma[{x,y+n}]=z;
		g[x].push_back(y+n);
		g[y+n].push_back(x);
	}
	for(auto i:ma)
	{
		if(!vis[i.xx.xx]) dfs(i.xx.xx,n);
	}
}
void solve()
{
	int n,m,k,q;cin>>n>>m>>k>>q;
	v.clear();
	for(int i=1;i<=k+q;i++)
	{
		int x,y,z;cin>>x>>y>>z;
		v.push_back({{x,y},z});
	}
	check(n,m,k);
	if(!f)
	{
		cout<<"0\n";
		while(q--) cout<<"0\n";
		return ;
	}
	int l=k,r=k+q;
	while(l<r)
	{
		int mid=(l+r+1)>>1;
		check(n,m,mid);
		if(f) l=mid;
		else r=mid-1;
	}
	check(n,m,k);
	set<int> st;
	int ans=0;
	for(int i=1;i<=n+m;i++)
	{
		st.insert(root(i));
	}
	ans=st.size()-1;
	cout<<pm[ans*30]<<endl;
	if(T==1) return ;
	for(int i=k+1;i<=l;i++)
	{
		int x=v[i-1].xx.xx;
		int y=v[i-1].xx.yy;
		if(root(x)!=root(y+n))
		{
			ans--;
			fa[root(y+n)]=root(x);
		}
		cout<<pm[ans*30]<<endl;
	}
	for(int i=l+1;i<=k+q;i++) cout<<"0\n";
}
void gettest(int t,int T)
{

}
signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>T;
    init(100000);
    if(T==-1)
    {
    	gettest(1664,T);
    	return 0;
	}
	for(int i=1;i<=T;i++)
    {
    	solve();
    	//cout<<"&\n";
	}
    return 0;
}
/*
1
3 3 4 0
1 1 0
1 2 6
1 3 10
3 2 10

1
10 7 13 0
4 1 655743427
8 1 883210408
5 1 953563688
3 7 1062124647
4 2 775138666
10 6 529268810
3 4 365232107
3 3 358450606
6 2 363142388
10 5 426823349
9 7 854216746
4 5 358541624
10 2 306732355

 0  6 10
-1 -1 -1
-1 12 -1

1
4 4 6 0
2 2 3
2 4 5
3 3 4
4 2 2
4 3 1
3 2 7
1 4 8
1 2 14

1
4 4 8 2
1 1 4
1 2 3
2 1 2
2 2 5
3 3 8
3 4 9
4 3 6
4 4 7
3 1 999
2 3 6

3
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30

1
2 5 0 2
1 1 10
1 2 30

*/

**Instructions:**
- Focus on edge cases or scenarios that maximize the failure probability in faulty solutions.
- Due to the output length limit, you should generate a **small-scale** test input that is **complete and valid**.
- Output the test input directly, not code to generate it.

**Output format:**
```plaintext
{test input}
```

Think step by step.

Response:
```plaintext
1
3 3 4 1
1 1 0
1 2 6
1 3 10
3 2 10
3 3 1
```