#索引树求第几大的数
import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
public class Main {
static int N, Q, offset;
static StreamTokenizer in;
static final int[] tree = new int[300000];
static final int[][] d = new int[100002][3];
static final int[][] q = new int[200002][4];
public static void main(String[] args) throws IOException {
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
int T = nextInt();
for (int tc = 1; tc <= T; tc++) {
readCase();
Arrays.sort(d, 0, N, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
return o1[1] == o2[1] ? o1[2] - o2[2] : o2[1] - o1[1];
}
});
for (int i = 1; i <= N; i++) q[d[i][2]][3] = i;//最终排名赋值给数组q
long ans = 0;
for (int i = 1; i < Q; i++) {
if (q[i][0] == 1) find(q[i][3]);//在最终排名的位置+1
else ans += query(q[i][1]);
}
System.out.println("#" + tc + " " + ans);
Arrays.fill(tree, 0, (offset << 1) + 3, 0);
}
}
static int find(int nth) {
int idx = 1;
while (idx <= offset) {
int left = idx << 1;
int right = left + 1;
if (nth > tree[left]) {
nth -= tree[left];
idx = right;
} else idx = left;
}
return d[idx - offset][0];
}
static void update(int pos) {
int idx = pos + offset;
while (idx > 0) {
tree[idx] += 1;//1表示该id的分数已出
idx >>= 1;
}
}
static void readCase() throws IOException {
N = nextInt();
Q = nextInt();
offset = 1;
while (offset < N) offset <<= 1;
offset--;
int idx = 0;
for (int i = 1; i <= Q; i++) {
q[i][0] = nextInt();
q[i][1] = nextInt();//id
if (q[i][0] == 1) {
q[i][2] = nextInt();//分数
d[idx][0] = q[i][1];//id
d[idx][1] = q[i][2];//分数
d[idx++][2] = i;//顺序号
}
}
}
static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
}