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.

vamsikrishna55 | 162 |

Vagesh Verma | 158 |

UrGuru | 133 |

abducodes | 127 |

Akhil varma | 114 |

abducodes | 683 |

harshtyagi36 | 322 |

Akhil varma | 267 |

arpit_anshuman | 258 |

highway_to_hell | 252 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.