Given an array **A** of **n** integers (always a power of 2) and **q **queries.

Every Query consists of two elements **index** and **value.** We need to write a program that assigns *the value* to

- At alternate steps perform
**bitwise OR**and**bitwise XOR**operations to the adjacent elements. - In first iteration select, select n/2 pairs moving from left to right, and do a bitwise OR of all the pair values. In second iteration select (n/2)/2 leftover pairs and do a bitwise XOR on them. In the third iteration select, select ((n/2)/2)/2 leftover pairs moving from left to right, and do a bitwise OR of all the pair values.
- Continue the above steps till we are left with a single element.

**Input:**

The first line contains an integer **T, ** the number of test cases. For each test case, the first line contains an integer **n, ** size of the array. Next line contains n space separated integers. Next line contains an integer **q**, number of queries. Next q line contains 2 integers **index** and **value.**

**Output:**

For each test case, the output is the element left after performing the query if possible else print **-1**.

**Constraints:**

1<=T<=100

1<=N<=10^5

0<=A[i]<=10^5

0<=index < N

1<=Q<=10^4

**Example:
Input**

1

4

1 4 5 6

2

0 2

3 5

1

3

Assigning 2 to index 0, the sequence is now [2, 4, 5, 6]. 1st iteration: There are 4/2=2 pairs (2, 4) and (5, 6) 2 OR 4 gives 6, and 5 OR 6 gives us 7.

So the sequence is now [6, 7]. 2nd iteration: There is 1 pair left now (6, 7) 6^7=1. Hence the last element left is 1 which is the answer to our first query.

Assigning 5 to index 3, the sequence is now [2, 4, 5, 5]. 1st iteration: There are 4/2=2 pairs (2, 4) and (5, 5) 2 OR 4 gives 6, and 5 OR 5 gives us 5. So the sequence is now [6, 5]. 2nd iteration: There is 1 pair left now (6, 5) 6^5=3. Hence the last element left is 3 which is the answer to our second query.

Author: Vanshika_pec

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

PM_Pranay | 38 |

m_raju | 22 |

Piyush_43 | 22 |

abducodes | 22 |

RamduttSharma | 18 |

Pulkit__Sharma__ | 626 |

Anirban166 | 503 |

abducodes | 468 |

Found_me | 430 |

Yo_Nehru | 404 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4559 |

Login to report an issue on this page.