Are you ungrouping the whole table at once? If yes, you can try to ungroup row by row:

If that doesn’t work, I think your only option is a java snippet:
String[] col1 = $Terms$;
String[] col2 = $terms q$;
int n1 = col1.length;
int n2 = col2.length;
int n0 = (n1 < n2 ? n1 : n2);
String[] col0 = new String[n0];
int t0 = 0;
for(int t1=0; t1<n1; t1++) {
for(int t2=0; t2<n2; t2++) {
if(col1[t1].equals(col2[t2])) {
col0[t0] = col1[t1];
t0++;
}
}
}
String[] colf = new String[t0];
for(int t1=0; t1<t0; t1++) {
colf[t1] = col0[t1];
}
return colf;
This code can probably be written in a smarter way than above, but it seems to work as long as the terms in the lists are unique.