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.

bruceewayne | 175 |

manvirag982 | 158 |

Core_Ka_Bachha | 137 |

yash_sharan | 136 |

rajupraaa1234 | 133 |

mr_kksparrow | 433 |

manvirag982 | 374 |

rajupraaa1234 | 268 |

Exception_404 | 254 |

PranathiBhuvanagiri | 242 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.