Given Binary Search Tree. The task is to find sum of all elements smaller than and equal to Kth smallest element.

**Input:**

The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of three lines.

First line of each test case consist of integer N, denoting the number of elements in a BST.

Second line of each test case consists of N space separated integers denoting the elements in the BST.

Third line of each test case consists of an integer K, denoting the Kth smallest elements.

**Output:**

It should be single line output, Print the respective output in the respective line.

**Constraints:**

1<=T<=100

1<=N<=50

1<=K<=N

**Example:
Input:**

1

7

20 8 4 12 10 14 22

3

22

The tree for above input will look like this:

20

/ \

8 22

/ \

4 12

/ \

10 14

Sum of 3 smallest elements are: 4 + 8 + 10 = 22

Author: saksham seth

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.

sabka_din_aayega | 317 |

maggiiiii | 248 |

thanuvinu94 | 211 |

Savage_19 | 206 |

Anugrah_kumar | 172 |

Bishnu Dev Panda | 803 |

sabka_din_aayega | 628 |

Anugrah_kumar | 562 |

okayboss | 561 |

maggiiiii | 531 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4925 |

Quandray | 4547 |

Login to report an issue on this page.