1.
/*
* FWP, Ausgew¦hlte Probleme aus dem ACM Programming Contest, SS10 Problem:
* 11890 calculus simplified
*
* @author Martin Lambeck
* @version 1.0, 30.10.2010
*
* Method : adhoc
* Status : Accepted
* Runtime: 0.412
*/

#include <stdio.h>
#include <algorithm>

using namespace std;

long sum = 0;
char exp[100005];
int nums[100005];
int left, right;

void testcase();
int work(int p, bool outneg);

int main()
{
int cases;

scanf("%d", &cases);

for (int i = 0; i < cases; i++)
testcase();
}

void testcase()
{
scanf("%100000s", exp);

int n, m;


scanf("%d", &n);

for (int i = 0; i < n; i++)
{
scanf("%d", &m);

nums[i] = m;
}

sort(nums, nums+n);

sum = 0;
left = 0;
right = n-1;

work(0, false);

printf("%ld\n", sum);
}

int work(int p, bool outneg)
{
bool neg = false;

char c;

while (true)
{
c = exp[p];

if (c == '\0')
return p;

if (c == 'x')
{
if ((neg == outneg))
sum += nums[right--];
else
sum -= nums[left++];

neg = false;

} else if (c == '(')
{
p = work(p+1, neg != outneg);
neg = false;

} else if (c == ')')
{
return p;

} else if (c == '+')
{
//

} else if (c == '-')
{
neg = !neg;
}

p++;
}
}